Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Session attribute access and converting to int?

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
like image 249
a k Avatar asked May 17 '11 13:05

a k


1 Answers

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

like image 77
Mayank Avatar answered Sep 28 '22 09:09

Mayank