通过将请求会话设置为新会话来使 2 会话失效

2022-01-16 00:00:00 java struts2 httpsession

在我的 Struts 应用程序中,一旦用户登录,我需要使当前会话无效并创建一个新会话.我使会话无效

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();

我创建了一个新会话

getHttpServletRequest().getSession(true);

这里的问题是在上面我尝试访问 getSession() 之后它给出了状态无效异常;HttpSession 无效.

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

getSession() 返回一个映射,在我的操作类中我实现了 SessionAware,它具有 setSession(Map session).

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

以下是例外情况

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

所以,我认为问题在于 Struts getSession() 仍然引用我已失效的会话.

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

如何让 Struts getSession() 引用我创建的新会话?

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

推荐答案

如果要在使 servlet 会话无效后访问 struts 会话,则应更新或更新 struts 会话.例如

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();

现在 struts 会话已准备好使用新的 servlet 会话,并且您已准备好重用 struts 会话.

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

相关文章