File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Passing object from Servlet to JSP throws Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Passing object from Servlet to JSP throws Exception" Watch "Passing object from Servlet to JSP throws Exception" New topic
Author

Passing object from Servlet to JSP throws Exception

Ashraf Fouad
Ranch Hand

Joined: Oct 07, 2001
Posts: 80
Dear all,
I'm facing a strange problem in my project.
I'm using Visual Age 3.5.2 with IBM tomcat test environment 3.1 as my Test environment so that I can support servlet 2.2 & JSP 1.1 in my application.
I developed the application & it works fine in the test environment, but yesterday I just deployed it on the WebSphere Application Server 3.5.5 that supports ( servlet 2.2 & JSP 1.1 ) and some strange behaviour happened :
I was testing the application before deployment before passing it to the QA team for testing, and I found a failure in a certain page that displays empty.
The page is used for either creation or modification in my application, depeneding in the parameters sent to it, in the creation it works fine but in the modification mode it throws exception. I traced the error till I found out that this error occures when I pass certain object in the request object from the servlet to the JSP, by the way I'm using MVC model implementaion recommended by IBM.
The object that is passed is just a class with member fields & accessors & implements Comparable, I also added implements java.io.Serializable but it didn't get me anywhere, its code is like the following:
public class FolderBrief
implements Comparable, java.io.Serializable
{
protected int m_nId_o;
protected String m_szName_o;
protected String m_szDescription_o;
protected String m_szKeyword_o;
protected String m_szEditable_o;
protected int m_nParentId_o;
protected String m_szIfsName_o;
public int compareTo( Object p_objFolderBrief )
{
FolderBrief l_FolderBrief = (FolderBrief) p_objFolderBrief;
String l_szFolderName = this.getName().toLowerCase(),
l_szOtherFolderName = l_FolderBrief.getName().toLowerCase();

return l_szFolderName.compareTo( l_szOtherFolderName );
}
public boolean equals( Object p_objObject )
{
//Comparison is made on the ID, which is the unique id
FolderBrief l_Folder = (FolderBrief)p_objObject;
int l_FolderId = l_Folder.getId();

if ( l_FolderId == this.m_nId_o )
return true;
else
return false;
}
public String toString()
{
return "Id:" + this.m_nId_o + ", Name:"+ this.m_szName_o;
}
}
The exception happens when I use:
getServletContext().getRequestDispatcher( "/admin/create_modify.jsp" ).forward( req, res );
Not when I add the object to the request:
req.setAttribute( "folder_modified", l_Folder );
the exception:
com.ibm.servlet.engine.webapp.UncaughtServletException: Server caught unhandled exception from servlet [jsp11]
Any suggestion


Ashraf Fouad
SCJP 1.2, SCBCD 5
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

I don't want to get too far ahead and say it sounds like a ClassLoader problem and you should start by searching this forum, so let's start with...
Do you know what the specific exception that is causing it to fail is? The server is returning 'uncaught exception', what was it and its message?
Dave
Ashraf Fouad
Ranch Hand

Joined: Oct 07, 2001
Posts: 80
No, I don't, do u have any ideas how to get it?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

quickest easiest nastiest way is to put a big try/catch in your servlet and dump the exception data

Just is case anyone sees this: printStackTrace does not count as handling an exception!
Ashraf Fouad
Ranch Hand

Joined: Oct 07, 2001
Posts: 80
I traced the exception & I found out that the exception is thrown in the JSP page when using getAttribute:
FolderBrief l_CurrentFolder = (FolderBrief)request.getAttribute( "folder_modified" );
it throws ClassCastException, I tried commening the this line and evrything goes right in the page, so the problem in getting the folder information from the request, I also tried using the full qualified name of the class com.mega.vi.core.FolderBrief and didn't get me anywhere
I also tried any another object in the application used as a test and it worked fine using the getAttribute, so I think the problem is in the class passed itself, what do u think.
the full code of the class is:
package com.mega.vi.core;
public class FolderBrief
implements Comparable, java.io.Serializable
{
protected int m_nId_o;
protected String m_szName_o;
protected String m_szDescription_o;
protected String m_szKeyword_o;
protected String m_szEditable_o;
protected int m_nParentId_o;
protected String m_szIfsName_o;
public int getId()
{
return m_nId_o;
}
public String getDescription()
{
return m_szDescription_o;
}
public String getKeyword()
{
return m_szKeyword_o;
}
public String getEditable()
{
return m_szEditable_o;
}
public int getParentId()
{
return m_nParentId_o;
}
public String getIfsName()
{
return m_szIfsName_o;
}
public void setId( int p_nId )
{
this.m_nId_o = p_nId;
}

public void setDescription( String p_szDescription )
{
this.m_szDescription_o = p_szDescription;
}
public void setKeyword( String p_szKeyword )
{
this.m_szKeyword_o = p_szKeyword;
}
public void setEditable( String p_szEditable )
{
this.m_szEditable_o = p_szEditable;
}
public void setParentId( int p_nParentId )
{
this.m_nParentId_o = p_nParentId;
}
public void setIfsName( String p_szIfsName )
{
this.m_szIfsName_o = p_szIfsName;
}

public int compareTo( Object p_objFolderBrief )
{
FolderBrief l_FolderBrief = (FolderBrief) p_objFolderBrief;
String l_szFolderName = this.getName().toLowerCase(),
l_szOtherFolderName = l_FolderBrief.getName().toLowerCase();

return l_szFolderName.compareTo( l_szOtherFolderName );
}
public boolean equals( Object p_objObject )
{
//Comparison is made on the ID, which is the unique id
FolderBrief l_Folder = (FolderBrief)p_objObject;
int l_FolderId = l_Folder.getId();

if ( l_FolderId == this.m_nId_o )
return true;
else
return false;
}
public String getName()
{
return m_szName_o;
}
public void setName( String p_szName )
{
this.m_szName_o = p_szName;
}
public String toString()
{
return "Id:" + this.m_nId_o + ", Name:"+ this.m_szName_o;
}
}
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

I'd have to say ClassCastException was what I was expecting...
try this:

My guess is that it'll be exactly the type you though it would be (and the same type you are casting to)
Rather than covering Web-app ClassLoaders from scratch, have a search in this forum for 'ClassCastException'. I'll have a look and paste some threads too.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

like this one.
Ashraf Fouad
Ranch Hand

Joined: Oct 07, 2001
Posts: 80
I don't beleive what I'm getting:
I tried:
Object l_Object = request.getAttribute( "modified_folder" );
if ( l_Object != null )
out.println( "type: " + l_Object.getClass().getName() );
else
out.println( "null" );
Also a class cast exception is thrown
I tried another approach by passing the same object in the session & return it from the JSP again from session & then remove it from session.
It worked fine without any errors and I got my object, I didn't c yr other thread, but I wanted to tell u what happened
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

Hi
Try use the session object.

and in your JSP page:

/Ren´┐Ż


Regards, Rene Larsen
Dropbox Invite
Ashraf Fouad
Ranch Hand

Joined: Oct 07, 2001
Posts: 80
I have two work around for this problem:
1- The session solution worked ok.
2- I put the FolderBrief class in a Vector & I passed it in the request & retrieved it from the JSP & it worked fine.
I applied the second solution, and the module is ready for QA.
I don't know where was the problem, if anyone knows how to contact IBM technical staff, plz tell me, thankx for u all.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

If it has nothing to do with the session, I recommend not using it. ie if it's meant to be 'request scope', keep it on the request and not the session.
You have a chance at getting an answer from Kyle Brown in the Websphere forum. Two pointers though - don't post the same question in multiple forums and don't address your questions to him directly or no-one else will answer it...
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi all,
i guess i know what is going wrong. there isn't a bug in the IBM server but its just the way the server works...
i read a similar problem in Oreilly's "Java Servlet Programming" book in the Chapter-11: Interservlet Communication" on page no:338.
and i'm sure something like that, was happening in the above code. else i tried to pass a request attribute from the servlet to JSp and it worked fine for me (i passed an object of a class i created and retrieved from the JSP page w/o errors)...
if you don't have the Oreilly's book i'd try to explain what i understood from the book and will let you know...
sorry..i didnt write whats there in Oreilly's book..i thought it would b difficult for me to explain...but surely i can try if you want..
regards
maulin.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
well,
i thought little about the problem described in the Oreilly's book and the one described here.
but i fear its not similar thing to the book example. because if that would have been a case then the second approach of using a Vector would also fail...
i'm also confused..
regards
maulin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing object from Servlet to JSP throws Exception