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:
I'm not good at JAVA but I used to do it likeInteger.parseInt(session.getAttribute("user").toString())
Try once, but just be sure to check null
for session.getAttribute("user")
before calling toString
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With