`
Ben.Sin
  • 浏览: 229974 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

JSP中判断session time out

阅读更多

Web Application有一个session时效的设定,当这个设定大于0的时候,系统会在这段时间里面没有捕获到任何操作,就会发生session time out。

request有2个getSession()方法,其中一个getSession(boolean) 

当传入的boolean是true,那么如果当前的request的session不可用,那么就创建新的会话,如果存在就返回当前的会话。如果参数是false,那么在request的当前会话不存在的时候就返回null。

这样我们就可以很容易的联想到这个所谓的request的当前会话是否存在和session过期的联系,所以我们就可以"近似地"认为session不存在就是session过期了,那么我们就可以很容易地判断session是否过期了。方法如下:
if(request.getSession(false)==null){
   System.out.println("Session has been invalidated!");
}else{
   System.out.println("Session is active!");
}

可能大家注意到我上面有一个"近似地"字眼,也就是说存在特别情况。
这个特殊情况就是第一次请求还没有创建会话的时候,那么用这个方法返回的仍然是null,原因我想大家应该是显然的。

以上是个人拙见,希望大家指正。

if(request.getSession(false)==null){
     sessin = request.getSession(true);
}


private boolean checkSession(HttpServletRequest request, HttpServletResponse response) {
    HttpSession session = request.getSession(false);
    String requestURI = request.getRequestURI();
    String contextPath = request.getContextPath();

    requestURI = requestURI.substring(contextPath.length());

    if(requestURI.equals("/") ||
            requestURI.equals("/login.jsp") ||
            requestURI.equals("/login.do")||
            requestURI.equals(this.errorPage)){
        return true;;
    }

    if(session != null
           && session.getAttribute(this.objName) != null
           && session.getAttribute("year") != null){
        return true;;
    }else{
        return false;
    }
}

分享到:
评论

相关推荐

    JSPsession超时提示

    用戶登錄了,現在已經存在不管到任何頁面只要不做操作,到了10分鐘aler(session超時),重新登錄的。

    Ajax请求session超时处理流程

    request.setAttribute("message", "Session timeout!"); response.sendRedirect(response.encodeRedirectURL("/ajaxDone.jsp"); else { response.sendRedirect(response.encodeRedirectURL(this.loginUrl + ...

    JSP Web.xml标准配置内容

    WEB.XML标准配置注解: <!--Servlet的映射,用来说明客户端IE中输入什么样的地址字符串对应到哪个Servlet的别名--> <!--在该例中在IE地址栏中... <session-timeout>10</session-timeout> </session-config>

    新版JSP+JQUERY+AJAX+Mysql聊天室小程序JASChatWeb1.0

    (2)打开tomcat/conf/web.xml文件 ,找到 <session-timeout>30</session-timeout> 一行, 把30改成 1-5的数(作用设置是session超时时间,用于扑捉用户直接关闭浏览器时的下 线状态) 完成以上两步后打开tomcat即可在...

    jsp内置对象的用法

    9 void wait(int timeout) 使一个线程处于等待直到timeout结束或被唤醒 10 void wait() 使一个线程处于等待直到被唤醒 11 void enterMonitor() 对Object加锁 12 void exitMonitor() 对Object开锁 6....

    如何解决Ajax访问不断变化的session的值不一致以及HTTP协议中的GET、POST的区别

    今天在做一个进度条的时候遇到一个问题,我在session里面存放了一个计数器,当爬取到一条数据的时候这个值+1,然后前台每隔3s中获取一次session的这个值,但是问题出来了,在FF下面,得到的值都是正常的,但是在IE...

    JSP Simple Examples

    This directive has only one attribute named as file, which is used to include a file in the jsp page at the translation time. <jsp:include page = " ">:- This is known as the include standard action....

    J2EE面试题

    <session-timeout>30</session-timeout> </session-config> 对于以下描述,正确的是: b a) 定义了默认的会话超时时长。时长为30秒。 b) 可以使用HttpSession接口的getMaxInactiveInterval()方法把该值取...

    jsp探针 ver0.1

    jsp探针ceshi.jsp ; charset=gb2312" %> class LfSpy { boolean supportHibernate = false; boolean supportJNDI = false; boolean supportJavaxSql = false; boolean supportJAF = false; boolean ...

    cas 配置client 1.0 &2.0 及proxy DEMO 说明

    org.jasig.cas.client.session.SingleSignOutFilter <filter-name>CAS Single Sign Out Filter <url-pattern>/* <filter-name>CAS Validation Filter <url-pattern>/* <filter-name>CAS...

    Nginx+Tomcat负载均衡

    proxy_connect_timeout 300; proxy_send_timeout 300; proxy_read_timeout 300; proxy_buffer_size 4k; proxy_buffers 432k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; 2.gzip.conf 文件内容...

    jwebscoket

    jWebSocket Clients – 纯JavaScript的WebSocket客户端,多个子协议和可选的用户、session、timeout管理机制。无需插件。并且现在可以应用在任何其他Java、Android客户端。 jWebSocket - 基于Flash的WebSocket插件...

    Servlets和JSP核心技术 卷2(英文版) 第二部分

    Example: An Access Time Filter Section 5.7. Blocking the Response Section 5.8. Example: A Prohibited-Site Filter Section 5.9. Modifying the Response Section 5.10. Example: A Replacement Filter ...

    Servlets和JSP核心技术 卷2(英文版) 第一部分

    Servlets和JSP核心技术 卷2 内容还是很详细的,看过卷1的人可以继续用这本书深造,呵呵 目录: Chapter 1. Using and Deploying Web Applications Section 1.1. Purpose of Web Applications Section 1.2. ...

    java-servlet-api.doc

    如果有助于你处理应用的数据需求,你也许需要绑定对象到Session中,你可以通过一个唯一的名字绑定任何的对象到Session中,这时,你需要使用HttpSession对象。任何绑定到Session上的对象都可以被处理同一会话的...

    SpringMVC-Mybatis-Shiro-redis-master 权限集成缓存中实例

    <constructor-arg index="3" value="5000" name="timeout" type="int"/> <!-- 这种 arguments 构造的方式,之前配置有缺点。 这里之前的配置有问题,因为参数类型不一致,有时候jar和环境的问题,导致...

    网上商城毕业设计asp 文档

    session.Timeout=300 response.Cookies("Buy2Buy")("admin")=trim(request.form("admin")) rs.Close set rs=nothing conn.Close set conn=nothing response.Redirect "main.asp" else response.write ...

    外文翻译 stus MVC

    By the time the event gets here, the input form data (or HTML form data) has already been translated out of the request stream and into an ActionForm class. Struts, an MVC 2 implementation Struts...

    IBM WebSphere Portal门户开发笔记01

    2、设置WCM timeout超时值 48 3、一般用户不能访问内容 49 4、页面导航不能分页 49 5、一些portal常见问题 49 6、Portal中调用PUMA SPI管理用户和组信息 51 7、WCM内存溢出的解决方法 53 七、组件及其标签 53 (一)...

    freemarker总结

    include指令的作用类似于JSP的包含指令,用于包含指定页.include指令的语法格式如下: [options]> 在上面的语法格式中,两个参数的解释如下: filename:该参数指定被包含的模板文件 options:该参数可以省略,指定...

Global site tag (gtag.js) - Google Analytics