Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Java Session in JSP

0 views
Skip to first unread message

Thiago Jorge

unread,
Nov 2, 2005, 4:21:13 AM11/2/05
to
Hello, I am having some trouble when trying to use java session to save
information and use in a different JSP.

I have a JSP called analyzeSourceCode.jsp that has

//Creates a Session
HttpSession sess = request.getSession();
//Save the front pointer to the JSP Session so it can be used in
another JSP
sess.setAttribute("front",front);
//Make the session timer not expire, user may need long time before
saving file
sess.setMaxInactiveInterval( -1 );

On my index.jsp file there are 2 forms, one of them calls
analizeSourceCode.jsp and another one that calls saveFile.jsp that
contains the following code:

HttpSession sess = request.getSession(true);
ListItem iterator = (ListItem) sess.getAttribute("front");

THe class ListItem has been defined in a file that was included in both
jsp. ListItem should have the address to the head of a linked list. If
I print out the address on the jsp in which I saved it to the session I
can see it is not NULL, because the print returns
org.apache.jsp.index_jsp$1ListItem@19fe451

However, when I hit the save button that calls saveFile.jsp I get the
following error on Tomcat. At first I thought it is trying to access a
null variable but looks like it has been saved to the session but I am
not being able to retrieve it.


HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented
it from fulfilling this request.

exception

org.apache.jasper.JasperException
at
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:207)
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:200)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:146)
.....
.....
root cause

java.lang.ClassCastException
at org.apache.jsp.save_file_jsp._jspService(save_file_jsp.java:77)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:92)
....
....

If anyone could help me, I would appreciate.

Thanks,
Thiago

Shorty

unread,
Nov 2, 2005, 4:53:48 PM11/2/05
to
Seems the "front" object is not the class you expect it to be (we don't
se where it is created...)

try printing the class of "front" :
- remove the line where you cast
- print in the page the class (I didn't check the actual code needed,
should look like that) :
out.print(sess.getAttribute("front").getClass().getName());
(Of course, that needs sess.getAttribute("front") to be not null so you
might want to check it first)

Georg Henzler

unread,
Nov 3, 2005, 1:41:25 AM11/3/05
to

Hi Jorge,

this is JSP, not PHP... it looks like you've done some PHP before ;-)

I think you just include the class definition by something like
<%@include file="ClassName.inc.jsp" %> and therefore you define
a DIFFERENT class for EVERY JSP file you include it (because they
are created as inner clsses of the outer class before the $):
org.apache.jsp.index_jsp$1ListItem
org.apache.jsp.someOtherFile_jsp$1ListItem

These two classes are obviously not the same, and that's why you get
the class cast exception...

To solve this you should create a ClassName.java file, you have to
compile it and put it in the WEB-INF/classes directory of your web
application. In the JSP you have to use the <%@page
import="mypackage.ClassName" %> directive.

Hope that helps
Georg


org.apache.jsp.index_jsp$1ListItem

Thiago Jorge

unread,
Nov 4, 2005, 6:39:27 PM11/4/05
to
Hey Georg thanks a lot for the help...As I am a new user of Java/JSP I
may be asking some dumb questions. But hopefully with you guys help I
will learn how to do it.

Ok, so basically I did what you said.

I created a file called ListItem.java in my WEB-INF/classes directory.
Here are the contents of this file (it is pretty simple now, no
methods, just the structure definition):

package MyPackage;
//Linked List Structure
class ListItem {
public int numberTk; // Number of Token on the linked list
public int numberLn; // Number Line that token is located
public String oritk; // Original Token from source file
public String sugtk; // Seggested Token to be changed
public String token; // Token data field.
public String before; // Code to be added before the token when
necessary
public String after; // Code to be added after the token when
necessary
ListItem next; // Pointer for next node
};

Then I compiled it in order to create the .class file. Here is the
output of the javac command:

C:\j2sdk1.4.2_08\bin>javac -verbose "C:\Program Files\Apache
Group\Tomcat 4.1\webapps\ROOT\masters\WEB-INF\classes\ListItem.java"

[parsing started C:\Program Files\Apache Group\Tomcat
4.1\webapps\ROOT\masters\WEB-INF\classes\ListItem.java]
[parsing completed 31ms]
[loading C:\j2sdk1.4.2_08\jre\lib\rt.jar(java/lang/Object.class)]
[loading C:\j2sdk1.4.2_08\jre\lib\rt.jar(java/lang/String.class)]
[checking MyPackage.ListItem]
[wrote C:\Program Files\Apache Group\Tomcat
4.1\webapps\ROOT\masters\WEB-INF\classes\ListItem.class]
[total 343ms]


Now when I execute my code this is the error I get:
Generated servlet error:
[javac] Compiling 1 source file

C:\Program Files\Apache Group\Tomcat
4.1\work\Standalone\localhost\_\masters\index_jsp.java:9: package
MyPackage does not exist
import MyPackage.ListItem;
^
C:\Program Files\Apache Group\Tomcat
4.1\work\Standalone\localhost\_\masters\index_jsp.java:80: cannot
resolve symbol
symbol : class ListItem
location: class org.apache.jsp.index_jsp
ListItem front;
^

Weird thing is the compiler says "checking MyPackage.ListItem" so
apparently that package does exist, however Tomcat says it does not
exist.
I placed the "WEB-INF/classes" folder inside my "masters" folder, I am
not sure this is correct but I am thinking that is what I should do
since my JSPs are not placed directly in my ROOT folder.

Thanks again

Thiago

Shorty

unread,
Nov 7, 2005, 2:43:57 PM11/7/05
to
Your ListItem class belongs to the MyPackage package and should
therefore be inside a MyPackage directory (under classes)

Thiago Jorge

unread,
Nov 9, 2005, 9:58:18 AM11/9/05
to
Hey!!!

So I did what you said. I put the ListItem class in a folder called
MyPackage inside WEB-INF/classes.
However, my WEB-INF folder is inside a folder called masters and not
directly inside the ROOT directory. Does that make a difference? Do I
have to tell Tomcat where to find WEB-INF folder? If so, how do I do
that.

Here is what I am doing to compile and my error results.

C:\Program Files\Apache Group\Tomcat

4.1\webapps\ROOT\masters\WEB-INF\classes>ja
vac -verbose -d . MyPackage/*.java
[parsing started MyPackage/ListItem.java]
[parsing completed 32ms]


[loading C:\j2sdk1.4.2_08\jre\lib\rt.jar(java/lang/Object.class)]
[loading C:\j2sdk1.4.2_08\jre\lib\rt.jar(java/lang/String.class)]
[checking MyPackage.ListItem]

[wrote .\MyPackage\ListItem.class]
[total 250ms]

An error occurred at line: 4 in the jsp file:
/masters/analyze_sourcecode.jsp

Generated servlet error:
[javac] Compiling 1 source file

C:\Program Files\Apache Group\Tomcat
4.1\work\Standalone\localhost\_\masters\index_jsp.java:9: package
MyPackage does not exist
import MyPackage.ListItem;
^
C:\Program Files\Apache Group\Tomcat
4.1\work\Standalone\localhost\_\masters\index_jsp.java:80: cannot
resolve symbol
symbol : class ListItem
location: class org.apache.jsp.index_jsp
ListItem front;
^

Sorry but I am really new in Java/JSP and it s getting kind of
frustrating since I am not moving anywhere!!
Thanks.

Thiago

Andrew Thompson

unread,
Nov 9, 2005, 5:59:33 PM11/9/05
to
Thiago Jorge wrote:

> So I did what you said. I put the ListItem class in a folder called
> MyPackage inside WEB-INF/classes.
> However, my WEB-INF folder is inside a folder called masters and not
> directly inside the ROOT directory. Does that make a difference?

I expect it would. Tomcat looks for the WEB-INF at the root
of the site, as a top-level directory.

E.G. http://www.thesite.com/WEB-INF/

0 new messages