Session attribute access and converting to int?
Solution 1
Even if you saved an int
, that method expects an Object so your int
will become an Integer
due to auto-boxing. Try to cast it back to Integer
and it should be fine:
int userid = (Integer) session.getAttribute("user");
However, if the attribute is null you will get a NullPointerException
here, so maybe it's better to go with Integer
all the way:
Integer userid = (Integer) session.getAttribute("user");
After this, you can safely check if userid
is null
.
EDIT: In response to your comments, here's what I mean by "check for null".
Integer userid = (Integer) session.getAttribute("user");
User user = null;
if (userid != null) {
user = new UserDAO().getUser(userid);
}
// here user will be null if no userid has been stored on the session,
// and it wil be loaded from your persistence layer otherwise.
Solution 2
I'm not good at JAVA but I used to do it like
Integer.parseInt(session.getAttribute("user").toString())
Try once, but just be sure to check null
for session.getAttribute("user")
before calling toString
Solution 3
Java has Integer
wrapper class , you can store int value in an Object of Integer
//setting
Integer intObj = new Integer(intVal);
session.setAttribute("key",intObj);
//fetching
Integer intObj = (Integer) session.getAttribute("key");
Solution 4
Try int userid = (Integer) session.getAttribute("user");
Solution 5
Integer userid = Integer.parseInt(session.getAttribute("user"));
Comments
-
a k almost 2 years
I have stored user id in Session using following command in Servlet:
HttpSession session = request.getSession(); session.setAttribute("user", user.getId());
Now, I want to access that user id from another Servlet:
HttpSession session = request.getSession(false); int userid = (int) session.getAttribute("user"); // This is not working OR User user = new User(); user.setId(session.getAttribute("user")); This ain't possible (Object != int)
Question:
- How can I cast to int and send the id to DAO for SELECT statement
-
Costi Ciudatu about 13 yearsThere's an
Integer
saved as session attribute. Converting it toString
and parsing back into anint
/Integer
doesn't get you much, does it ? -
Mayank about 13 yearsI'm not sure how
Object to int
conversion works. If Object isconverted to Integer
and thenintValue()
is used, results might be good. As already told, I'm not good at JAVA. -
Mayank about 13 yearsIn C++ it is surely not going to work, because
Object class
will not be of same size in stack asint
-
Costi Ciudatu about 13 yearsIt's not really
Object
toint
; autoboxing is actuallyInteger
toint
(orint
toInteger
), the same forlong
andLong
and basically all the primitive wrappers. It means you can assign anInteger
value to anint
variable, without caring ofintValue()
or you can assign anint
value to anInteger
reference, without wrapping asnew Integer(value)
. When a supertype ofInteger
is expected (Object
in this case),int
will be passed asInteger
. It's just syntax sugar added in Java 1.5. How else would it be possible to pass anint
tosetAttribute(String, Object)
? -
a k about 13 yearsI am getting NullPointerException after second statement too.
-
Costi Ciudatu about 13 yearsAfter it ? You mean, you have the
Integer userid
assigned and then what ? Do you check it for null ? If not and you try call anything on it or you try to assign it to someint
or pass it to some method expectingint
, the NPE is natural. But the line itself cannot really throw a NPE. If the userid is null after reading it from the session it means nobody has stored it there yet. -
a k about 13 yearsgetting java.lang.NullPointerException
-
Bala R about 13 years@a k Not sure why it's null but you can do
int userid = session.getAttribute("user") == null ? (Integer) session.getAttribute("user") : 0 ;
where0
would be the default value if sesson returns null. but you still need to figure out why it's null if it's not supposed to be null. -
a k about 13 yearsI am passing it in UserDAO userDAO = new UserDAO(); and userDAO.getUser(userid);
-
Costi Ciudatu about 13 yearsI suppose your
getUser()
expects anint
. Check theuserid
for null and if it is (you have nothing stored on the session as "user), avoid that call (or use a default value instead, if it makes sense: -1 or 0 or whatever). -
Michele d'Amico about 9 yearsWhy
"String"
asgetAttribute()
arg? Should be"user"
instead.