• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 182
  • Last Modified:

ClassCastException - Custom Class in Session

I have a custom class called Vendor.  When a vendor logs in to my web app, I retrieve an instance of the Vendor class for that Vendor.  I want to store the instance in the Session so I don't have to query the datbase repeatedly, and also so I don't have to store multiple Sesion variable for all the properties of the Vendor class.

Storing the Vendor class in session (in the following way) doesn't seem to be a problem (at least no errors arre thrown)

Vendor TempVendor = new Vendor();
TempVendor.Name = "Some Vendor Name";
session.setAttribute("VendorObject", TempVendor);


Retrieving the Vendor instance in it's proper form IS the problem.  I receive a ClassCastException when attempting the following:

Vendor TempVendor = (Vendor)session.getAttribute("VendorObject");

I've also tried casting it to an Object, and then to Vendor with the same results:

Object O = session.getAttribute("VendorObject");
Vendor TempVendor = (Vendor)O;

Any help would be greatly appreciated.  500 PTS.



0
mmarksbury
Asked:
mmarksbury
  • 9
  • 9
  • 2
2 Solutions
 
fargoCommented:
check if the object exists in the session
Vendor TempVendor = null;
if(session.getAttribute("VendorObject")!=null){
TempVendor = (Vendor)session.getAttribute("VendorObject");
}

otherwise all seems correct.

0
 
mmarksburyAuthor Commented:
I am doing that already.  The object is there. I can cast the session value to an object just fine like this....

Object O = (Object)session.getAttribute("TempVendor");

That works fine.  Getting it from Object to Vendor though does not work.
0
 
fargoCommented:
do the following..and let us know the name

Object O = (Object)session.getAttribute("TempVendor");
System.out.println("The class name is " + O.getClass().getName());

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
mmarksburyAuthor Commented:
The class name is _mtest._process._login$Vendor
0
 
fargoCommented:
is Vendor a inner class?? what is the package of the Vendor package?
0
 
mmarksburyAuthor Commented:
Well, that's the interesting part....

The Vendor class is not part of a package, nor is it within a compiled .class file.

My classes (at the moment) are declared in an include file for the web app.  Our server guy apparently has had some trouble getting compiled .class files to work on the app server.

Rather than wait for him to figure it out, I've proceeded this way with the plan to package at a later time.

The custom classes work just fine everywhere else...it's only when I try to retrieve them from session that I run into issues.
0
 
fargoCommented:
>> nor is it within a compiled .class file.
AND
>> My classes (at the moment) are declared in an include file for the web app.

what does both the above mean??

better way is to use packages. But it may/should work even without packages.

0
 
mmarksburyAuthor Commented:
I am simply writing my JAVA code between the <%! %> tags.  Thatr code is in multiple files that are included in other JSP files whnever needed.

The code is compiled on the fly, and is not part of any package. I eventually will move all this code into packages, but for the moment, that is not an option.

The question here is not how the classes are written or used, but why they do not work when retrieved through the session.  If there was a problem with the classes they wouldn't work period and also, I wouldn't be able to even store them in session.

We need to look at other possible reasons why this would happen.  I'm new to Java and JSP, which is why I posted.

Any more ideas out there?
0
 
fargoCommented:
well, i have never done that. And don't think u can use them these class code written in jsp to be used as normal java classes. The jsp code is always compiled as Servlet..and can't be used the way u are willing to use.

Only way is to use java classes..
0
 
mmarksburyAuthor Commented:
The classes are working in ALL other aspects.  Retrieving them from Session is the only failure occurring, therfore, I believe there is something else going on, be it a bug in my app server or something else, but I'd like to hear a few more theories before I close the question.
0
 
mmarksburyAuthor Commented:
I've tried something else which has yielded a strange result.

First, I used the following in a standalone page and this worked perfectly.

Vendor Temp = new Vendor();
Temp.Contact = "Some Contact Name";
session.setAttribute("Vend", Temp);


Then I split the above code into two files and the ClassCastException was thrown.

File1.jsp
------------
<%
Vendor Temp = new Vendor();
Temp.Contact = "Some Contact Name";
session.setAttribute("Vend", Temp);
%>
<a href="File2.jsp">Go to File 2</a>

File2.jsp
--------------
<%
Vendor New = (Vendor)session.getAttribute("Temp");
out.print(New.Contact);
%>

So what would cause this behavior?  Calling the object in session from the same page is not a problem.  This indicates that there is not an issue with how the classes are created and used.  Calling it from a second page (new request) throws the error.  So, this leads me to believe that there is some issue with the object passing.

Any suggestions given this new information?
0
 
fargoCommented:
>>The classes are working in ALL other aspects
can u please elaborate?

I don't think it's possible..the way u r trying to attempt. BTW, which app server u r using?
0
 
mmarksburyAuthor Commented:
Ooops, I meant to say, that I tried storing AND retrieving in the same page and it worked fine.
0
 
mmarksburyAuthor Commented:
I'm not sure of the version on the App server, but it is an Oracle app server, and the Java version is 1.4.  
0
 
fargoCommented:
is the Vendor class is defined in File1.jsp? and the instance is made in same jsp??
0
 
mmarksburyAuthor Commented:
No.

The Vendor class is defined in an external JSP which is included in File1.jsp.  That same file is also included in File2.jsp.
0
 
fargoCommented:
hmm..no idea man.
0
 
rrzCommented:
fargo>is Vendor a inner class??
I think that it is a inner class of the JSP.  
Here is a small JSP that demonstrates what I think we have here.  
<%!
  public class Vendor{
                      String Contact = "Inner Class!";
  }
%>
<%
  Vendor vendor = new Vendor();
  vendor.Contact = "Some Contact Name";
  session.setAttribute("myVendor", vendor);
%>
<%=vendor.Contact + "<br/>"%>
This Servlet name is <%=this.getClass().getName() + "<br/>"%>
Vendor class name is <%=session.getAttribute("myVendor").getClass().getName()%> ( fargo posted this above)

But this will never work for what you want here. Looking at your code in your question.
>Vendor TempVendor = new Vendor();
TempVendor is member of the Servlet instance that was created from the trnslated JSP file.  All users would share it.     rrz

0
 
fargoCommented:
But this will never work for what you want here. Looking at your code in your question.
>Vendor TempVendor = new Vendor();
TempVendor is member of the Servlet instance that was created from the trnslated JSP file.  All users would share it.  

rrz..correct explaination.
0
 
rrzCommented:
Thanks for the points.
Fargo thanks for the confirmation.       rrz
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 9
  • 9
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now