Can anyone help me get these JSP pages to work?

Hello All,


I am trying to learn about Beans in Java, after creating a two JSP pages and a "User" bean. When I try to run the first JSP page (handoff1.jsp), I keep receiving this error in Tomcat(see below).


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


An error occurred at line: 0 in the jsp file: /handoff1.jsp

Generated servlet error:

[javac] Compiling 1 source file

C:\Program Files\Apache Software

Foundation\Tomcat 4.1

\work\Standalone\localhost\cartapp\handoff1_jsp.java:41: cannot find symbol symbol : class User

location: package com.cartapp.user

com.cartapp.user.User handoff = null;

^


An error occurred at line: 0 in the jsp file: /handoff1.jsp

Generated servlet error:

C:\Program Files\Apache Software

Foundation\Tomcat 4.1

\work\Standalone\localhost\cartapp\handoff1_jsp.java:43: cannot find symbol symbol : class User

location: package com.cartapp.user handoff = (com.cartapp.user.User)

pageContext.getAttribute("handoff", PageContext.SESSION_SCOPE);

^


An error occurred at line: 0 in the jsp file: /handoff1.jsp

Generated servlet error:

C:\Program Files\Apache Software Foundation\Tomcat 4.1\work\Standalone\localhost\cartapp\handoff1_jsp.java:46: cannot find symbol symbol : class User location: package com.cartapp.user

handoff = (com.cartapp.user.User) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "com.cartapp.user.User");

^


An error occurred at line: 5 in the jsp file: /handoff1.jsp

Generated servlet error:

C:\Program Files\Apache Software Foundation\Tomcat 4.1
\work\Standalone\localhost\cartapp\handoff1_jsp.java:58: cannot find symbol symbol : variable e location: class
org.apache.jsp.handoff1_jsp e.printStackTrace();


^


4 errors


at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:85) at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:248) at org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:343) at org.apache.jasper.compiler.Compiler.compile(Compiler.java:356) at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:427) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:142) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:240) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:187) at javax.servlet.http.HttpServlet.service(HttpServlet.java:809) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:198) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:144) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:209) at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:595) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:432) at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:954) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:138) at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:595) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:432) at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:954) at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2459) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:132) at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:595) at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:118) at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:593) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:116) at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:593) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:432) at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:954) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:126) at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:595) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:432) at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:954) at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:152) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869) at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664) at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527) at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80) at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684) at java.lang.Thread.run(Thread.java:595)


There are two jsp pages handoff1 and handoff2


handoff1.jsp


<jsp:useBean id="handoff" scope="session" >

class="com.cartapp.user.User" />

<HTML>

<BODY>

This is the setting page!

<% handoff.setFirstName("George"); e.printStackTrace(); %>

</BODY>

</HTML>


handoff2.jsp-


<jsp:useBean id="handoff" scope="session" >

class="com.cartapp.user.User" />

<HTML>

<BODY>

<H1> This is geting the page!

handoff= <%= handoff.getFirstName() %></H1>

</BODY>

</HTML>


Attempting to use a bean called User


User.java


package com.cartapp.user;

public class User

{

protected String lastName;

protected String firstName;

public String getLastName()

{

return lastName;

}

public void setLastName(String lname)

{

lastName = lname;

}

public String getFirstName()

{

return firstName;

}

public void setFirstName(String fname)

{

firstName = fname;

}

public boolean isValidUserData()

{

return ((firstName != null) && (firstName.length() > 0) && (lastName != null) && (lastName.length() > 0));

}

}


All of the files are stored in C:\Cartapp and I have created a new context for that path, could anyone tell me what I am doing wrong?


I have correctly set JAVA_HOME to the path to the location of my JDK installation and I have added the C:\<JDKPATH>\BIN to my path environment variable.


I also attempted to import my class into JSP using <%@ pageimport="com.cartapp.user.User"%>


Does any have any ideas?

[7575 byte] By [Nogravitia] at [2007-11-27 9:47:50]
# 1

do you iterate user bean?

<logic:iterate id="myId" name="result" type="class.USER">

Title: <c:out value="${myId.name}"/>

</logic:iterate>

mention the class name for the USER bean.

sorry it seems you did it.

Message was edited by:

skp71

Did you import USER bean in your jsp

<%@page import="..................."%>

Message was edited by:

skp71

skp71a at 2007-7-13 0:00:49 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
I think your bean should be here/%TOMCAT_HOME%/..path../WEB-INF/classes
skp71a at 2007-7-13 0:00:49 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

You may want to start out with an even simpler jsp, unless you have already done that, like helloworld.

What you need to do is first check if the bean is even making it to the form. Use some logging or system.out.printlns to see what is coming through.

Try:

<code> <% (if session != null){ </code>

<code> User user = (User) session.getValue("user"); </code>

<code> if(user != null) etc... <>

jamesEstona at 2007-7-13 0:00:49 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Thanks James, I know that the bean is not reaching the JSP page. I think it is a sort of classpath issue?


The main issue is that Tomcat ignores the classpath and I dont really understand how you get Tomcat to pick up the relevant class.


All I have done is create a C:\Cartapp context, then I have added handoff1.jsp and handoff.jsp to that directory. Then packaged User.java within a folder structure of C:\CARTAPP\com\cartapp\user.


Which is why when I use the syntax <jsp:useBean id="handoff" scope="session" class="com.cartapp.user.User" />, I would expect it to find the User.java class.


As I already explained even when I added an import statement into the JSP <%@ pageimport="com.cartapp.user.User"%>, it still doesnt work

Nogravitia at 2007-7-13 0:00:49 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Okay then. You are almost there. I'm not sure what version of Tomcat you have, but I would google that version and look for tomcat setup or tomcat configuration. Here is some info and a link: .

Set Your CLASSPATH

Since servlets and JSP are not part of the Java 2 platform, standard edition, you have to identify the servlet classes to the compiler. The server already knows about the servlet classes, but the compiler (i.e., javac) you use for development probably doesn't. So, if you don't set your CLASSPATH, attempts to compile servlets, tag libraries, or other classes that use the servlet and JSP APIs will fail with error messages about unknown classes. Here are the standard Tomcat locations:

Tomcat 4: c:\tomcat4\common\lib\servlet.jar

in addition to the servlet JAR file, you also need to put your development directory in the CLASSPATH. Although this is not necessary for simple packageless servlets, once you gain experience you will almost certainly use packages. Compiling a file that is in a package and that uses another class in the same package requires the CLASSPATH to include the directory that is at the top of the package hierarchy. In this case, that's the development directory I just discussed. Forgetting this setting is perhaps the most common mistake made by beginning servlet programmers!

Finally, you should include "." (the current directory) in the CLASSPATH. Otherwise, you will only be able to compile packageless classes that are in the top-level development directory.

Here's that link for the article:

http://www.yorku.ca/jhuang/examples/tomcat-install.html

I've found when you correctly identify the problem, you are more than halfway to the solution!

HTH,

James

jamesEstona at 2007-7-13 0:00:49 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Thanks James I solved the problem, simple matter of adding the compiled class file package to C:/<myAppDirectory>/WEB-INF/classes/ It seems to act as a flag for Tomcat to them pick up the class file
Nogravitia at 2007-7-13 0:00:49 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
That's what I suggested yesterday, I guess. 10 duke stars goes to who?
skp71a at 2007-7-13 0:00:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
You are correct. I added some additional comments and reworded your original suggestion, but apparently Nograviti took the credit for the fix.
jamesEstona at 2007-7-13 0:00:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9
no problem buddy. Take it easy!!!
skp71a at 2007-7-13 0:00:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...