Daredevil
Daredevil

Reputation: 1782

java- session.getattribute returns null

I have a JSP pages that validate a user when he logs in and I would like to capture and store his username for some other uses. So here is the code when the user logs in:

  String username = request.getParameter("username");
                String password = request.getParameter("password");

session.setAttribute("username", username);

I debug the session value and it does indeed return the correct username value. So right now,I want to access this value from another page to see the username it returns.I call this value like below:

Value:<%= session.getAttribute("username") %>

The value returns is Value:null instead of the username (root). I can't figure out what went wrong. Did I do any mistake?

Upvotes: 1

Views: 1615

Answers (2)

HS447
HS447

Reputation: 81

Are you invalidating session anywhere before the control forwarded to resulting page where you are accessing.

Upvotes: 1

mm6
mm6

Reputation: 94

Are you setting the attribute to the current session, if not use like this:

request.getSession(true).setAttribute("username", username);

Upvotes: 2

Related Questions