request.getSession(false)导致java.lang.IllegalStateException [英] request.getSession(false) causes java.lang.IllegalStateException

查看:125
本文介绍了request.getSession(false)导致java.lang.IllegalStateException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试从请求中获取会话时,如果会话过期,则会导致空指针异常。以下是代码的一部分。我在第三行得到例外。

When I try to get the session from the request, it causes a null pointer exception if the session is expired. Given below is a part of the code. I get exception on the third line.

public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpReq = (HttpServletRequest) req;
HttpSession session = httpReq.getSession(false);

这是堆栈跟踪:

java.lang.IllegalStateException: getLastAccessedTime: Session already invalidated
at org.apache.catalina.session.StandardSession.getLastAccessedTime(StandardSession.java:423)
at org.apache.catalina.session.StandardSessionFacade.getLastAccessedTime(StandardSessionFacade.java:84)
at com.myapp.admin.CustomerContext.valueUnbound(CustomerContext.java:806)
at org.apache.catalina.session.StandardSession.removeAttributeInternal(StandardSession.java:1686)
at org.apache.catalina.session.StandardSession.expire(StandardSession.java:801)
at org.apache.catalina.session.StandardSession.isValid(StandardSession.java:576)
at org.apache.catalina.connector.Request.doGetSession(Request.java:2386)
at org.apache.catalina.connector.Request.getSession(Request.java:2120)
at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:833)
at com.myapp.ui.web.filter.ApplicationSessionFilter.doFilter(ApplicationSessionFilter.java:45)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.myapp.ui.web.filter.ErrorFilter.doFilter(ErrorFilter.java:42)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:190)
at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:291)
at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:776)
at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:705)
at org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:898)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:690)
at java.lang.Thread.run(Thread.java:662)


推荐答案

这不是Nullpointer异常。

这是一个非法状态异常。

This is not a Nullpointer exception.
This is an Illegal state exception.

当会话失效/过期并且您尝试访问它时会发生这种情况。

This occurs when the session has been invalidated/expired, and you try to access it.

会话失效如下。

session.invalidate();

调用invalidate后,你无法使用会话。

After invalidate is called, you cant use the session.

会话超时时到期。它们到期后,如果你尝试使用它,它将抛出相同的异常。

Session expires when they timeout. After they expire, if you try to use it, it will throw the same exception.

这些是你可以手动设置超时的方法。

These are the ways you can manually set a timeout.

在web.xml中设置超时。这是以分钟为单位设置的。

set your timeout in web.xml. This is set in minutes.

<session-config>
   <session-timeout>30</session-timeout> 
</session-config>

或者您也可以在java中设置它。

Or you can set it in in your java as well.

public void setMaxInactiveInterval(int interval)

session.setMaxInactiveInterval(30*60); // This we set in seconds. 

这里我们指定servlet容器使此会话无效之前客户端请求之间的时间(以秒为单位)。否定时间表示会话永远不会超时。

Here we specify the time, in seconds, between client requests before the servlet container will invalidate this session. A negative time indicates the session should never timeout.

确保您正在执行的操作不会超过超时时间。否则你将不得不重置价值。

Make sure the operation you are doing is not taking longer than the timeout time. Else you will have to reset the value.

编辑:

无法从中恢复干净的方式。会话将失效/不可用,您将需要创建一个新会话并重新填充数据。
通常人们被重定向到备用错误PAge而不是显示堆栈跟踪。 您的会话已过期。请再次登录。


It is not possible to recover from it in a clean manner. The session will get invalidated/unusable, and you will need to create a new session and repopulate data. Generally people are redirected to an alternate Error PAge instead of showing the Stack Trace. "Your session has expired. Please login again."

使用request.isRequestedSessionIdValid()来确定会话ID是否仍然有效。

Use request.isRequestedSessionIdValid() to identify whether the session ID is still valid.

HttpSession session =httpReg.getSession(false);
if( !request.isRequestedSessionIdValid() )
{
        //comes here when session is invalid.
        // redirect to a clean error page "Your session has expired. Please login again."
}

另一种选择是使用try catch

Another option is to use handle the IllegalExceptionState using try catch

HttpSession session =httpReg.getSession(false);
try
{
     if(session != null)
     { 
       Date date = new Date(session.getLastAccessedTime()); 
    }
}
catch( IllegalStateException ex )
{
      //comes here when session is invalid.
      // redirect to a clean error page "Your session has expired. Please login again."
}

备注:
如果您确实有一个正在采取的流程很长一段时间完成和会话超时(例如:解析真正大的数据记录)。
您应该考虑将它们从Web应用程序中取出,并使用简单的Java将它们作为一个进程运行。

Side Note: If you do have a process which is taking a long time to complete and sessions are timing out (ex: parsing really large data records ). You should consider taking them out of the web application, and use simple java to run them as a process.

这篇关于request.getSession(false)导致java.lang.IllegalStateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆