Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java HttpSession .getAttribute(String name)

I have a simple, short question but not found the answer anywhere. I created an HttpSession and want to get an attribute from it, for example a User object.

HttpSession session = request.getSession(true);
Object userObject = session.getAttribute("name");
if ((userObject != null) && (userObject instanceof User)) {
    User currentUser = (User) userObject;
    ...
}

The question is the following: .getAttribute function gets a String name as parameter - what is the name? From where do I know the name? Is it predefined somewhere? - then where to define another one?

Thank you!

like image 493
Display Name Avatar asked Mar 12 '23 14:03

Display Name


1 Answers

Usually, you add attributes to the session yourself like so:

User someObject = new User();
session.setAttribute("pickaName", someObject);

Then you can get the session and pull off this attribute using that same name you used earlier like so:

User sameObject = (User) session.getAttribute("pickaName");
like image 197
LeHill Avatar answered Mar 16 '23 10:03

LeHill