Sturts 2会话失效,将请求会话设置为新会话

Har*_*ana 2 java struts2 httpsession

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

getHttpServletRequest().getSession().invalidate();
Run Code Online (Sandbox Code Playgroud)

我创建了一个新会话

getHttpServletRequest().getSession(true);
Run Code Online (Sandbox Code Playgroud)

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

getSession()返回一个地图,在我的动作类中我实现了SessionAware哪个有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
Run Code Online (Sandbox Code Playgroud)

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

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

Rom*_*n C 9

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

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();
Run Code Online (Sandbox Code Playgroud)

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

  • @Harshana不,你不应该使servlet会话无效,你应该使struts会话无效,这样做1)清理struts会话2)使servlet会话无效.你应该替换代码.而不是`getHttpServletRequest().getSession().invalidate();`放置我的代码,而不是`getHttpServletRequest().getSession(true)`. (3认同)