aspose file tools*
The moose likes Servlets and the fly likes Session variables...java.lang.object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Session variables...java.lang.object?" Watch "Session variables...java.lang.object?" New topic
Author

Session variables...java.lang.object?

Jason Allen
Greenhorn

Joined: Jan 25, 2001
Posts: 26
I am trying to store some variables in a session object in order to have them available between jsp pages. I am having problems with using these correctly, I keep getting errors that say I can't convert and must cast to/from java.lang.Object.
For example, if I want to create a session variable called name, and store "Joe" in it, what code would I use?
I tried
<%
session.setAttribute("Name", "Joe");
%>
It gave me an error like can't convert String to java.lang.Object
So, I created a class that I thought I would use for storing these miscellaneous variables. I created a class called backpack, that just has properties. I then use the following code:
<%
backpack myBackpack = new backpack();
myBackpack.Name = "Joe";
session.setAttribute("backpack", myBackpack);
%>
This part works fine. However, when I try to retrieve the session variable with this code:
<%
backpack myBackpack = new backpack();
myBackpack = session.getAttribute("backpack");
%>
I get the following error:
Incompatible type for =. Explicit cast needed to convert java.lang.Object to backpack.
What am I doing wrong? And how would you recommend doing what I'm trying to get accomplished.
The above code is all contained in JSP scriptlets.
Thanks,
Jason
Paul Wetzel
Ranch Hand

Joined: Nov 02, 1999
Posts: 107
When you get an object back from the session you need to cast it to the object that was stored in the session. That is, in your example you need to do the following:
You can think of the session object as a glorified HashTable (kinda)
hope this helps
paul
[This message has been edited by Paul Wetzel (edited February 02, 2001).]
Jason Allen
Greenhorn

Joined: Jan 25, 2001
Posts: 26
Paul, thanks for the reply. I no longer get the cast error
But now I'm getting another error
When I first try to check a value in that object I get a NullPointerException
Here is a code sample:
<%
backpack myBackpack = new backpack();
myBackpack = (backpack) session.getAttribute("backpack");
myBackpack.UserID = request.getParameter("UserID");
myBackpack.Pass = request.getParameter("Pass");
if (myBackpack.PassVerified == false) {
if (myBackpack.UserID == null | | myBackpack.Pass == null){
response.sendRedirect("retrylogin.jsp");
}
else{
passed = db2Connect.Connect(myBackpack.UserID, myBackpack.Pass);
if (passed != true){
response.sendRedirect("retrylogin.jsp");
}
myBackpack.SelectVerified = true;
}
}
%>
It seems to not like the first reference to the myBackpack.UserID property.
Thanks in advance.
Jason Allen
Greenhorn

Joined: Jan 25, 2001
Posts: 26
Ok, got that one figured out...
I think that was stupidity error. I hadn't started a new session in a while, so I assume the Session had timedout and it no longer had the backpack session variable.
I restarted my browser and it worked
Thanks again,
Jason.
Mirko Froehlich
Ranch Hand

Joined: Aug 21, 2000
Posts: 114
That's right. In general, you should always check if an object is null after you try to retrieve it from the session in order to avoid NullPointerExceptions.
-Mirko

Originally posted by Jason Allen:
Ok, got that one figured out...
I think that was stupidity error. I hadn't started a new session in a while, so I assume the Session had timedout and it no longer had the backpack session variable.
I restarted my browser and it worked
Thanks again,
Jason.

Jason Allen
Greenhorn

Joined: Jan 25, 2001
Posts: 26
Thanks Mirko,
I assume if the session variable is null, then I would send them back to logon? On my login screen I'm doing a setAttribute, would that reinitialize that particular session variable?
Also, when I am doing the setAttribute, how do I convert a primitive data type to an object? For example...
<% String Name;
Name = "Joe";
session.setAttribute("Name", Name);
%>
That particular code would tell me it can't convert a String to java.lang.Object How would I write the code to do this?
Thanks,
Jason.
Mirko Froehlich
Ranch Hand

Joined: Aug 21, 2000
Posts: 114
Jason, see my answers below:

I assume if the session variable is null, then I would send them back to logon? On my login screen I'm doing a setAttribute, would that reinitialize that particular session variable?

That is correct - as long as the variable you are passing in setAttribute is not null.

Also, when I am doing the setAttribute, how do I convert a primitive data type to an object? For example...
<% String Name;
Name = "Joe";
session.setAttribute("Name", Name);
%>
That particular code would tell me it can't convert a String to java.lang.Object How would I write the code to do this?

I am not sure why you are getting this error; this code should work fine. String extends Object, so you can store Strings in the session using "session.setAttribute". Does anybody else have any ideas?
-Mirko
Jason Allen
Greenhorn

Joined: Jan 25, 2001
Posts: 26
Thanks for the response.
I'll give it a try again. I've been known to do stupid things in the past...maybe this was one of them
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Jason,
Did your code work? 'Can't convert String to Object' one. You code as such should work.
regds
maha anna
Jason Allen
Greenhorn

Joined: Jan 25, 2001
Posts: 26
Not sure what I was doing before, but that code did work.
Thanks for all the input.
Jason.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Session variables...java.lang.object?