zoukankan      html  css  js  c++  java
  • session的使用

    用监听器监听访问网站的事件,难以实现服务器端验证游览器端是否关闭游览器,所以模拟session过期设置session的生命周期。

    import javax.servlet.ServletContext;
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import javax.servlet.http.*;
    
    public class ServletListener implements ServletContextListener,
            HttpSessionListener, HttpSessionAttributeListener {
    
        // Public constructor is required by servlet spec
        public ServletListener() {
        }
        // -------------------------------------------------------
        // ServletContextListener implementation
        // -------------------------------------------------------
        //session创建时人数自增
        public void sessionCreated(HttpSessionEvent se) {
            /* Session is created. */
            HttpSession session = se.getSession();
            session.setMaxInactiveInterval(3*60);
            ServletContext servletContext = session.getServletContext();
            int count = (int) servletContext.getAttribute("count");
            servletContext.setAttribute("count",++count);
            System.out.println("调用了sessionCreate方法!!");
            System.out.println("当前在线人数:"+count);
        }
    
        public void sessionDestroyed(HttpSessionEvent se) {
            /* Session is destroyed. */
            ServletContext servletContext = se.getSession().getServletContext();
            int count = (int) servletContext.getAttribute("count");
            servletContext.setAttribute("count",--count);
            System.out.println("调用了sessionDestroy方法!!");
            System.out.println("当前在线人数:"+count);
        }
    
        public void contextInitialized(ServletContextEvent sce) {
          /* This method is called when the servlet context is
             initialized(when the Web application is deployed).
             You can initialize servlet context related data here.
          */
            ServletContext servletContext = sce.getServletContext();
            servletContext.setAttribute("count",0);
        }
    
        public void contextDestroyed(ServletContextEvent sce) {
          /* This method is invoked when the Servlet Context
             (the Web application) is undeployed or
             Application Server shuts down.
          */
        }
    
        // -------------------------------------------------------
        // HttpSessionListener implementation
        // -------------------------------------------------------
    
        // -------------------------------------------------------
        // HttpSessionAttributeListener implementation
        // -------------------------------------------------------
    
        public void attributeAdded(HttpSessionBindingEvent sbe) {
          /* This method is called when an attribute 
             is added to a session.
          */
        }
    
        public void attributeRemoved(HttpSessionBindingEvent sbe) {
          /* This method is called when an attribute
             is removed from a session.
          */
        }
    
        public void attributeReplaced(HttpSessionBindingEvent sbe) {
          /* This method is invoked when an attibute
             is replaced in a session.
          */
        }
    }

    在web.xml中注册监听器

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
        <listener>
            <listener-class>ServletListener</listener-class>
        </listener>
    </web-app>

    用不同的游览器模拟不同的用户访问网站

  • 相关阅读:
    在VMware9.0上安装CentOS6.3+mysql5.5.28数据库 东师理想
    Python学习总结(二)python的练习方法
    gdb调试nasm语法的汇编程序(转载)
    配置Bochs
    量变与质变(生活中,技术上)
    设置gdb反汇编语法为intel(转载)
    Python学习总结(一)
    2012暑假计划
    理解TCP为什么需要进行三次握手(白话)(转载)
    对自己的学习方式的思考(转载)
  • 原文地址:https://www.cnblogs.com/lr1402585172/p/10705841.html
Copyright © 2011-2022 走看看