Forums Register Login

Session variables...java.lang.object?

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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).]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.


+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Jason,
Did your code work? 'Can't convert String to Object' one. You code as such should work.
regds
maha anna
+Pie Number of slices to send: Send
Not sure what I was doing before, but that code did work.
Thanks for all the input.
Jason.
Normally trees don't drive trucks. Does this tiny ad have a license?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 4842 times.
Similar Threads
session.Attribute returns null
setting the request attributes from Javascript
problem with method
Trouble setting an integer to a session ID
Passing a javascript variable to JSP
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 07:02:52.