a k
a k

Reputation: 827

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

Upvotes: 10

Views: 70262

Answers (10)

Dimitris Maniatis
Dimitris Maniatis

Reputation: 177

Multiplying two strings from session:

int z = Integer.parseInt((String)session.getAttribute("sintelestis"));
int y = Integer.parseInt((String)session.getAttribute("_embadon_akinitou"));
System.out.println("Ο Συνολικός Φόρος είναι: "+ (z*y));

Upvotes: 0

vikash
vikash

Reputation: 455

Try this code :

 int userId=Integer.parseInt((String)session.getAttribute("user"));

Upvotes: -1

Jasser Saanoun
Jasser Saanoun

Reputation: 1

try this, it worked for me: HttpSession session = request.getSession(); if (session.getAttribute("user") != null) { userid = ((Integer) session.getAttribute("user")).intValue(); } else { userid = 0; }

Upvotes: 0

mansoor Sntli
mansoor Sntli

Reputation: 19

try this

int userid = Integer.parseInt(session.getAttribute("user").toString());

Upvotes: 0

antmog
antmog

Reputation: 1

I used this:

Integer.parseInt(session.getAttribute("String").toString())

Upvotes: 0

Costi Ciudatu
Costi Ciudatu

Reputation: 38205

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.

Upvotes: 14

Jigar Joshi
Jigar Joshi

Reputation: 240918

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");

Upvotes: 2

Rob Parker
Rob Parker

Reputation: 513

Integer userid = Integer.parseInt(session.getAttribute("user"));

Upvotes: 1

Bala R
Bala R

Reputation: 108957

Try int userid = (Integer) session.getAttribute("user");

Upvotes: 1

Mayank
Mayank

Reputation: 5728

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

Upvotes: 2

Related Questions