-->

Sturts 2 session invalidation with setting request

2019-01-26 00:44发布

问题:

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?

回答1:

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.