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

Session监控

    博客分类:
  • java
 
阅读更多

确保系统稳定性能,可以进行session控制

 

public class SessionListener implements HttpSessionListener{

      

       public void sessionCreated(HttpSessionEvent event) {

       HttpSession ses = event.getSession();

       String id=ses.getId()+ses.getCreationTime();

       SummerConstant.UserMap.put(id, Boolean.TRUE);     // 添加用户

}

 

  public void sessionDestroyed(HttpSessionEvent event) {

      HttpSession ses = event.getSession();

      String id=ses.getId()+ses.getCreationTime();

      synchronized (this) {

                     SummerConstant.USERNUM--;   // 用户数减一

                     SummerConstant.UserMap.remove(id); // 从用户组中移除掉,用户组为一个map

              }

       }

}

 
然后只需要把这个监听器在web.xml 中声明就可以了
例如:

Xml 代码

  1. <listener>  
  2.       <listener-class>  
  3.              com.demo.SessionListener  
  4.        </listener-class>  
  5. </listener>  

 

关于系统失效时间的控制

在一般系统登录后,都会设置一个当前session 失效的时间,以确保在用户长时间不与服务器交互,自动退出登录,销毁session
具体设置很简单,方法有三种:
1 )在主页面或者公共页面中加入:session.setMaxInactiveInterval(900);
参数900 单位是秒,即在没有活动15 分钟后,session 将失效。
这里要注意这个session 设置的时间是根据服务器来计算的,而不是客户端。所以如果是在调试程序,应该是修改服务器端时间来测试,而不是客户端。
2 )也是比较通用的设置session 失效时间的方法,就是在项目的web.xml 中设置

Xml 代码

  1. <session-config>  
  2.   <session-timeout>15</session-timeout>  
  3. </session-config>  

<session-config>

  <session-timeout>15</session-timeout>

</session-config>

  这里的15 也就是15 分钟失效.
3 )直接在应用服务器中设置,如果是tomcat ,可以在tomcat 目录下conf/web.xml
找到<session-config> 元素,tomcat 默认设置是30 分钟,只要修改这个值就可以了。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics