The Artima Developer Community
Sponsored Link

Java Answers Forum
Class Cast Exception

7 replies on 1 page. Most recent reply: Oct 6, 2002 8:11 AM by Don Hill

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 7 replies on 1 page
viji m

Posts: 5
Nickname: vimwise
Registered: Oct, 2002

Class Cast Exception Posted: Oct 2, 2002 1:14 PM
Reply to this message Reply
Advertisement
Hi: Im using IPlanet WEb Server 6.0. The following code gives me a ClassCastException. Could somebody help?

session = req.getSession(true);
sessdata = (TempStorage)session.getAttribute(session.getId());
if(sessdata == null){
//No Session.Redirect to home page
res.sendRedirect("HomePage");
}
else{
//the servlet code.


TempStorage is a user defined class that is passed to the HttpSesssion object using the setAttribute method.
session.setAttribute(session.getId(),sessdata);

No matter what object is passed to the session, it gives me the same error.
Thanks.


Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: Class Cast Exception Posted: Oct 2, 2002 7:34 PM
Reply to this message Reply
This line is the source of your problem...

sessdata = (TempStorage)session.getAttribute(session.getId());


The reason it is like that is because you are probably not setting the value in the session properly or not retrieving it properly. The second option seems more likely to be the reason for the error.

eg. if you are binding a value to the name, "myName", then retrieve it by passing "myName" to getAttribute method. That is what you are not doing, I think.

viji m

Posts: 5
Nickname: vimwise
Registered: Oct, 2002

Re: Class Cast Exception Posted: Oct 3, 2002 6:50 AM
Reply to this message Reply
Singh M:
I am storing the object as given below.

TempStorage sessdata = new TempStorage();
session = req.getSession(true);
session.setAttribute(session.getId(),sessdata);


And this is the code that I use to retrieve the object.

TempStorage sessdata;
session = req.getSession(true);
sessdata = (TempStorage)session.getAttribute(session.getId());

In this code, I tried substituting other classes for TempStorage. It still gives me the same error.

Thanks/

Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: Class Cast Exception Posted: Oct 3, 2002 5:06 PM
Reply to this message Reply
I think the source of your problem is the way you are setting the value in the session...

session.setAttribute(session.getId(),sessdata);

because a unique identifier is assigned to every session, above statement of yours is trying to overwrite that value attached with that identifier with some value of yours. I think the server would not do it. Just give a name defined by you to set the value in session.

session.setAttribute("myObject",sessdata);

Surya

Posts: 1
Nickname: adityaaa
Registered: Oct, 2002

Re: Class Cast Exception Posted: Oct 4, 2002 4:26 AM
Reply to this message Reply
If the session is already created and you are trying to retrieve the value from the created session,you should be using req.getSession(false) instead of saying
req.getSession(true)

viji m

Posts: 5
Nickname: vimwise
Registered: Oct, 2002

Re: Class Cast Exception Posted: Oct 4, 2002 7:07 AM
Reply to this message Reply
Hi:
I tried it the way you said, it still gives me the same error.
But instead of using my own class, I tried storing my values in a Vector and put that in the Session object. This seems to be working fine for now. So I am going ahead with this.

But coming back to the original problem, I have used this method numerous times and its never given me a problem. It used to give me the same error on Java Web Server occassionally. But I read somewhere that the reason is, if we recompile the user defined class(TempStorage) sometimes, the server still refers to the old version and therefore when it casts it back, it gives an error. If you empty the cache manually, this becomes ok. I tried this on iPlanet, but it doesnt seem to work.
Anyway, thanks for your time. I really appreciate it.

Have a good weekend.

viji m

Posts: 5
Nickname: vimwise
Registered: Oct, 2002

Re: Class Cast Exception Posted: Oct 4, 2002 7:12 AM
Reply to this message Reply
Surya:
True. But I believe, if we give 'true' it will start a new session only if a session does not exist. So I guess its not entirely wrong, since I am checking if the session object contains my TempStorage object -To make sure its not a new session.

Thanks.

Don Hill

Posts: 70
Nickname: ssswdon
Registered: Jul, 2002

Re: Class Cast Exception Posted: Oct 6, 2002 8:11 AM
Reply to this message Reply
Why no just check the session with sess.isNew() to detrmine what if it is indeed a new session.

if you use the req.getSession(true) and then use the isNew() you will indeed know if the session was created.


HTH

Flat View: This topic has 7 replies on 1 page
Topic: Tabbed Pane with AWT Previous Topic   Next Topic Topic: taking directory as a command line argument

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use