Count
Count

Reputation: 1423

session variables in case of url redirections

I have a application running on tomcat server , which is redirected to another website for password authentication ( and store some data) , and then redirected back to my tomcat server

EX

1 to A
A to B
B to 2

Where 1 and 2 are pages on my application
A and B are pages on other applications

I am setting a session variable on my page 1 using

HttpSession session = request.getSession(true);
session.setAttribute("loginUser", "loginUser");

and using it on page 2 using

String loginUser= session.getAttribute("loginUser");

but getting a null pointer exception on 2

Upvotes: 1

Views: 2178

Answers (2)

Buhake Sindi
Buhake Sindi

Reputation: 89169

If you're doing a redirect in the same context (Application), rather use RequestDispatcher to do a forward (passing your request and response). That way, you pass your session through.

Otherwise, my suggestion is to not store the loginUser on the session but pass user id variable as some intelligent (and confused) string to the other application, write a mechanism to retrieve the user logged in session state and carry on from there. This is called, Single-Sign On.

Upvotes: 1

Hemant Metalia
Hemant Metalia

Reputation: 30648

it may be happening because of

'A session is transient, and its lifetime ends when one of the following occurs:

A user leaves your site and the user's browser does not accept cookies.

A user quits the browser.

The session is timed out due to inactivity.

The session is completed and invalidated by the servlet.

The user logs out and is invalidated by the servlet.'

refer http://docs.oracle.com/cd/E15523_01/web.1111/e13712/sessions.htm

Upvotes: 0

Related Questions