Session attribute access and converting to int?

66,675

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"));
Share:
66,675
a k
Author by

a k

Finding the Eternal Peace in coding.

Updated on July 09, 2022

Comments

  • a k
    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:

    1. How can I cast to int and send the id to DAO for SELECT statement
  • Costi Ciudatu
    Costi Ciudatu about 13 years
    There's an Integer saved as session attribute. Converting it to String and parsing back into an int/Integer doesn't get you much, does it ?
  • Mayank
    Mayank about 13 years
    I'm not sure how Object to int conversion works. If Object is converted to Integer and then intValue() is used, results might be good. As already told, I'm not good at JAVA.
  • Mayank
    Mayank about 13 years
    In C++ it is surely not going to work, because Object class will not be of same size in stack as int
  • Costi Ciudatu
    Costi Ciudatu about 13 years
    It's not really Object to int; autoboxing is actually Integer to int (or int to Integer), the same for long and Long and basically all the primitive wrappers. It means you can assign an Integer value to an int variable, without caring of intValue() or you can assign an int value to an Integer reference, without wrapping as new Integer(value). When a supertype of Integer is expected (Object in this case), int will be passed as Integer. It's just syntax sugar added in Java 1.5. How else would it be possible to pass an int to setAttribute(String, Object) ?
  • a k
    a k about 13 years
    I am getting NullPointerException after second statement too.
  • Costi Ciudatu
    Costi Ciudatu about 13 years
    After 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 some int or pass it to some method expecting int, 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
    a k about 13 years
    getting java.lang.NullPointerException
  • Bala R
    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 ; where 0 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
    a k about 13 years
    I am passing it in UserDAO userDAO = new UserDAO(); and userDAO.getUser(userid);
  • Costi Ciudatu
    Costi Ciudatu about 13 years
    I suppose your getUser() expects an int. Check the userid 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
    Michele d'Amico about 9 years
    Why "String" as getAttribute() arg? Should be "user" instead.