Jsp Tomcat会话已过期

Jsp Tomcat会话已过期,jsp,session,tomcat,Jsp,Session,Tomcat,我有一个关于Tomcat会话的问题 当Tomcat默认会话过期时,我知道时间是30分钟 我想向用户显示会话过期的消息页 我怎么办 如果存在会话过期错误代码,请指导我 我在谷歌上搜索了会话过期的错误代码,但找不到它 请教我您的方法。如果您使用的是Servlet,那么您将抛出java.lang.IllegalStateException。当您尝试使用会话时。 如果您使用的是JSP,那么您将抛出NullPointerException。当您尝试访问旧会话丢失的会话属性时。如果您使用的是Servlet,

我有一个关于Tomcat会话的问题

当Tomcat默认会话过期时,我知道时间是30分钟

我想向用户显示会话过期的消息页

我怎么办

如果存在会话过期错误代码,请指导我

我在谷歌上搜索了会话过期的错误代码,但找不到它


请教我您的方法。

如果您使用的是Servlet,那么您将抛出java.lang.IllegalStateException。当您尝试使用会话时。
如果您使用的是JSP,那么您将抛出NullPointerException。当您尝试访问旧会话丢失的会话属性时。

如果您使用的是Servlet,则会抛出java.lang.IllegalStateException。当您尝试使用会话时。
如果您使用的是JSP,那么您将抛出NullPointerException。当您尝试访问旧会话丢失的会话属性时。

当会话过期时,会话对象将从请求对象返回null

if(request.getSession(false)==null){
     //Session expired or never existed
} else{
   //session is still good.
}
更好的做法是将此类测试留给一个过滤器,该过滤器可以检查每个请求,但您可以在JSP或servlet中进行。将重定向代码放在JSP中往往会使其更加复杂,并将注意力从UI上移开。我建议使用一个过滤器,如果不是的话,那么在servlet中将请求路由到JSP控制器。如果您不使用控制器

不推荐使用JSP版本-在JSP中使用以下内容

<%
         if(request.getSession(false)==null){
            //session has expired
            response.sendRedirect("/ExpiredPage");
         } else{
           //Do what you normally would               
        }
%>
public void doGet(HttpServletRequest request,HttpServletResponse){    
     if(request.getSession(false)==null){
        //session has expired
        response.sendRedirect("/ExpiredPage");
     } else{
       //Do what you normally would
    }
}
public void doFilter(ServletRequest request,ServletResponse, FilterChain chain){    
     if((HttpServletRequest)request.getSession(false)==null){
        //session has expired
        ((HttpServletResponse)response).sendRedirect("/ExpiredPage");
     } else{
       //Do what you normally would
       chain.doFilter(request,response);
    }
}
过滤器版本最佳选择-在调用servlet(调用JSP)的过滤器中,使用以下命令

<%
         if(request.getSession(false)==null){
            //session has expired
            response.sendRedirect("/ExpiredPage");
         } else{
           //Do what you normally would               
        }
%>
public void doGet(HttpServletRequest request,HttpServletResponse){    
     if(request.getSession(false)==null){
        //session has expired
        response.sendRedirect("/ExpiredPage");
     } else{
       //Do what you normally would
    }
}
public void doFilter(ServletRequest request,ServletResponse, FilterChain chain){    
     if((HttpServletRequest)request.getSession(false)==null){
        //session has expired
        ((HttpServletResponse)response).sendRedirect("/ExpiredPage");
     } else{
       //Do what you normally would
       chain.doFilter(request,response);
    }
}

当会话过期时,会话对象将从请求对象返回null

if(request.getSession(false)==null){
     //Session expired or never existed
} else{
   //session is still good.
}
更好的做法是将此类测试留给一个过滤器,该过滤器可以检查每个请求,但您可以在JSP或servlet中进行。将重定向代码放在JSP中往往会使其更加复杂,并将注意力从UI上移开。我建议使用一个过滤器,如果不是的话,那么在servlet中将请求路由到JSP控制器。如果您不使用控制器

不推荐使用JSP版本-在JSP中使用以下内容

<%
         if(request.getSession(false)==null){
            //session has expired
            response.sendRedirect("/ExpiredPage");
         } else{
           //Do what you normally would               
        }
%>
public void doGet(HttpServletRequest request,HttpServletResponse){    
     if(request.getSession(false)==null){
        //session has expired
        response.sendRedirect("/ExpiredPage");
     } else{
       //Do what you normally would
    }
}
public void doFilter(ServletRequest request,ServletResponse, FilterChain chain){    
     if((HttpServletRequest)request.getSession(false)==null){
        //session has expired
        ((HttpServletResponse)response).sendRedirect("/ExpiredPage");
     } else{
       //Do what you normally would
       chain.doFilter(request,response);
    }
}
过滤器版本最佳选择-在调用servlet(调用JSP)的过滤器中,使用以下命令

<%
         if(request.getSession(false)==null){
            //session has expired
            response.sendRedirect("/ExpiredPage");
         } else{
           //Do what you normally would               
        }
%>
public void doGet(HttpServletRequest request,HttpServletResponse){    
     if(request.getSession(false)==null){
        //session has expired
        response.sendRedirect("/ExpiredPage");
     } else{
       //Do what you normally would
    }
}
public void doFilter(ServletRequest request,ServletResponse, FilterChain chain){    
     if((HttpServletRequest)request.getSession(false)==null){
        //session has expired
        ((HttpServletResponse)response).sendRedirect("/ExpiredPage");
     } else{
       //Do what you normally would
       chain.doFilter(request,response);
    }
}

如果会话过期,用户将被注销,如果用户刷新或导航到受保护的页面,用户将被带到登录页面。@EJP,感谢您的好意。但如何及时识别会话过期的情况呢?能否发布web.xml的相关部分?您是否正在用web xml本身配置受保护的页面?如果会话过期,用户将注销,如果刷新或导航到受保护的页面,用户将被带到登录页面。@EJP,感谢您的好意。但如何及时识别会话过期的情况呢?能否发布web.xml的相关部分?您是否在web xml本身中配置受保护的页面?这是一个很好的解决方案:3.tyvm.很好的解决方案:3.tyvm。