Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Struts 2 session invalidation with setting request session to a new session

In my Struts application once an user login I need to invalidate the current session and create a new session. I invalidate the session with

getHttpServletRequest().getSession().invalidate();

And I create a new session as

getHttpServletRequest().getSession(true);

The problem here is after above I try to access getSession() it gives the state invalid exception; HttpSession is invalid.

getSession() returns a map where in my action class I implements SessionAware which has the setSession(Map session).

EDIT: Below is the exception

Error creating HttpSession due response is commited to client. You can use the CreateSessionInterceptor or create the HttpSession from your action before the result is rendered to the client: HttpSession is invalid
java.lang.IllegalStateException: HttpSession is invalid

So, what I assume the problem is the Struts getSession() still reference the session which I've invalidated.

How to make the Struts getSession() to reference the new session which I've created?

like image 485
Harshana Avatar asked Oct 28 '25 05:10

Harshana


1 Answers

If you want to access the struts session after you invalidated the servlet session you should update or renew the struts session. For example

SessionMap session = (SessionMap) ActionContext.getContext().getSession();

//invalidate
session.invalidate();

//renew servlet session
session.put("renewServletSession", null);
session.remove("renewServletSession");

//populate the struts session
session.entrySet();

now struts session is ready to use the new servlet session and you ready to reuse the struts session.

like image 143
Roman C Avatar answered Oct 29 '25 19:10

Roman C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!