views:

562

answers:

6
+1  Q: 

JavaBean problem

Hello. I have two JSPs and a JavaBean that aren't working. This is a problem that's probably very common to newbies, so please forgive me if this has been answered in this forum. I've Googled all over the place for an answer to this, and although I'm not the only one who's ever had this problem, I can't seem to figure out what my problem is.

I'm using Tomcat 6.0. The first JSP is GetName.jsp, located at C:\Tomcat\webapps\app1\GetName.jsp:

<HTML>
<BODY>
<FORM METHOD=POST ACTION="NextPage.jsp">
What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20><BR>
What's your e-mail address? <INPUT TYPE=TEXT NAME=email SIZE=20><BR>
What's your age? <INPUT TYPE=TEXT NAME=age SIZE=4>
<P><INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>

The second JSP is NextPage.jsp, located at C:\Tomcat\webapps\app1\NextPage.jsp:

<jsp:useBean id="user" class="classes.UserData" scope="session"/> 
<HTML>
<BODY>
You entered<BR>
Name: <jsp:getProperty name="user" property="username" /><BR>
Email: <jsp:getProperty name="user" property="email" /><BR>
Age: <jsp:getProperty name="user" property="age" /><BR>
</BODY>
</HTML>

My JavaBean, UserData, compiles correcty, and the class file is located at C:\Tomcat\webapps\app1\WEB-INF\classes:

package classes;

import java.io.Serializable;

public class UserData implements Serializable {
    String username;
    String email;
    int age;

    public UserData() {
    }

    public void setUsername( String value )
    {
        username = value;
    }

    public void setEmail( String value )
    {
        email = value;
    }

    public void setAge( int value )
    {
        age = value;
    }

    public String getUsername() { return username; }

    public String getEmail() { return email; }

    public int getAge() { return age; }
}

I also have the following in my web.xml file, located at C:\Tomcat\webapps\app1\WEB-INF:

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
  PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
  "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"&gt;

<web-app>
</web-app>

My Google searches have suggested something to do with the classpath. My classpath is currently .;C:\Tomcat\lib\servlet-api.jar.

When I enter information in GetName.jsp and click on the button, Tomcat gives me the following for NextPage.jsp:

org.apache.jasper.JasperException: /NextPage.jsp(1,1) The value for the useBean class attribute classes.UserData is invalid.
    org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
    org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
    org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:148)
    org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1203)
    org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1160)
    org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2343)
    org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2393)
    org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2399)
    org.apache.jasper.compiler.Node$Root.accept(Node.java:489)
    org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2343)
    org.apache.jasper.compiler.Generator.generate(Generator.java:3365)
    org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:199)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:315)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:295)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:282)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:586)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

I could swear I'm doing everything right, but apparently I'm not. Could someone please tell me what's wrong before I tear all my hair out? Thanks in advance.

A: 

The problem appears to be your referencing classes as a package. It is not, it is the root directory of the classes in the war.

remove the package line from your class, and refer to your bean as class="UserData" and see what happens.

Yishai
I have tried this. Here is what Tomcat is telling me.org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 1 in the jsp file: /NextPage.jspUserData cannot be resolved to a type1: <jsp:useBean id="user" class="UserData" scope="session"/> 2: <HTML>3: <BODY>4: You entered<BR>
A: 

The problem is that your UserData.class file is in wrong directory, which means UserData.class should actually be in webapps/app1/WEB-INF/classes/classes/UserData.class.

William Brendel
A: 

I have tried Yishai's suggestion. Here is what Tomcat is telling me:

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 1 in the jsp file: /NextPage.jsp
UserData cannot be resolved to a type
1: <jsp:useBean id="user" class="UserData" scope="session"/> 
2: <HTML>
3: <BODY>
4: You entered<BR>


An error occurred at line: 1 in the jsp file: /NextPage.jsp
UserData cannot be resolved to a type
1: <jsp:useBean id="user" class="UserData" scope="session"/> 
2: <HTML>
3: <BODY>
4: You entered<BR>


An error occurred at line: 1 in the jsp file: /NextPage.jsp
UserData cannot be resolved to a type
1: <jsp:useBean id="user" class="UserData" scope="session"/> 
2: <HTML>
3: <BODY>
4: You entered<BR>


An error occurred at line: 5 in the jsp file: /NextPage.jsp
UserData cannot be resolved to a type
2: <HTML>
3: <BODY>
4: You entered<BR>
5: Name: <jsp:getProperty name="user" property="username" /><BR>
6: Email: <jsp:getProperty name="user" property="email" /><BR>
7: Age: <jsp:getProperty name="user" property="age" /><BR>
8: </BODY>


An error occurred at line: 6 in the jsp file: /NextPage.jsp
UserData cannot be resolved to a type
3: <BODY>
4: You entered<BR>
5: Name: <jsp:getProperty name="user" property="username" /><BR>
6: Email: <jsp:getProperty name="user" property="email" /><BR>
7: Age: <jsp:getProperty name="user" property="age" /><BR>
8: </BODY>
9: </HTML>


An error occurred at line: 7 in the jsp file: /NextPage.jsp
UserData cannot be resolved to a type
4: You entered<BR>
5: Name: <jsp:getProperty name="user" property="username" /><BR>
6: Email: <jsp:getProperty name="user" property="email" /><BR>
7: Age: <jsp:getProperty name="user" property="age" /><BR>
8: </BODY>
9: </HTML>


Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:423)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:317)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:295)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:282)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:586)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
I guess you need to use the fully-qualified classname, i.e. class="classes.UserData"
harto
This gets the original error.
Put your NextPage.jsp outside the classes directory. What it is doing over there.
Adeel Ansari
A: 

Here is my current directory structure:

C:\
   Tomcat\
      app1\
          GetName.jsp
          NextPage.jsp
          WEB-INF\
             web.xml
             classes\
                 UserData.class
put UserData.class under WEB-INF\classes\classes
martinatime
I think you understood the classes directory wrongly. It doesn't need to be the package name, it works as a container of all your classes. So, if the fully qualified class name is com.test.bean.UserName, it will go under WEB-INF/classes/com/test/bean/UserName.class
Adeel Ansari
A: 

I tried martinatime's suggestion, putting the package name back into the Java code. The JSP is not returning error messages. Instead, it returns this:

You entered
Name: null
Email: null
Age: 0
Sounds like your JSPs and JavaBean are working but no data is being populated into the bean.Try hardcoding values into the bean to test things out.
martinatime
Read in my post, "secondly", and "thirdly".
Adeel Ansari
A: 

You need to set the bean properties in your NextPage.jsp file.

Add the following line after your useBean statement like this.

<jsp:useBean id="user" class="UserData" scope="session"/>
<jsp:setProperty name="user" property="*" />
Mr. Will
Adding this finally made everything work. Thank you, Mr. Will.