Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
510 views
in Technique[技术] by (71.8m points)

java - Sturts 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...