zoukankan      html  css  js  c++  java
  • javaweb . 页面登出 操作

    对于 监听器的使用

    package com.itstaredu.bookstore.listener;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import javax.servlet.http.HttpSessionAttributeListener;
    import javax.servlet.http.HttpSessionEvent;
    import javax.servlet.http.HttpSessionListener;
    import javax.servlet.http.HttpSessionBindingEvent;
    
    public class MySessionListener implements ServletContextListener,
            HttpSessionListener, HttpSessionAttributeListener {
    
        // Public constructor is required by servlet spec
        public MySessionListener() {
        }
    
        // -------------------------------------------------------
        // ServletContextListener implementation
        // -------------------------------------------------------
        @Override
        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.
          */
        }
    
        @Override
        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
        // -------------------------------------------------------
        @Override
        public void sessionCreated(HttpSessionEvent se) {
            /* Session is created. */
            ServletContext sc = se.getSession().getServletContext();
            Object count = sc.getAttribute("count");
            if (count == null) {
                sc.setAttribute("count", 1);
            } else {
                sc.setAttribute("count", (Integer) count + 1);
            }
        }
    
        @Override
        public void sessionDestroyed(HttpSessionEvent se) {
            /* Session is destroyed. */
            ServletContext sc = se.getSession().getServletContext();
            sc.setAttribute("count", (Integer) (sc.getAttribute("count")) - 1);
        }
    
        // -------------------------------------------------------
        // HttpSessionAttributeListener implementation
        // -------------------------------------------------------
    
        @Override
        public void attributeAdded(HttpSessionBindingEvent sbe) {
          /* This method is called when an attribute 
             is added to a session.
          */
        }
    
        @Override
        public void attributeRemoved(HttpSessionBindingEvent sbe) {
          /* This method is called when an attribute
             is removed from a session.
          */
        }
    
        @Override
        public void attributeReplaced(HttpSessionBindingEvent sbe) {
          /* This method is invoked when an attibute
             is replaced in a session.
          */
        }
    }
    

      

    跳转到登出页面时候,无论是分发 或是 重定向,服务器会判定该用户的再一次访问,所以把对session的销毁放在最后面

    package com.itstaredu.bookstore.servlet;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import java.io.IOException;
    
    /**
     * @author 
     */
    public class LogoutServlet extends HttpServlet {
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }
    
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            HttpSession session = request.getSession();
            // go to login page
            System.out.println("logout");
            System.out.println(session.getServletContext().getAttribute("count"));
            request.getRequestDispatcher("/login.jsp").forward(request, response);
            session.invalidate();
        }
    }
    

      

  • 相关阅读:
    Silverlight & Blend动画设计系列一:偏移动画(TranslateTransform)
    如何在DeepEarth中进行图形绘制(点、线、多边形以及自定义图片图层)
    Bing Maps进阶系列六:使用Silverlight剪切(Clip)特性实现Bing Maps的迷你小地图
    解决 ICTCLAS 2009 Windows_JNI_32 在 Web Project无法使用的方法
    XListControl 改变颜色 行高
    设置 java.library.path其实是在 Apache Tomcat 的任务栏 Icon中设置
    ICTCLAS 2009 JNI_32 遇到MyEclipse Web Project下无法运行
    10分钟开始使用ICTCLAS Java版
    boost regex_search 找出所有 匹配串
    Boost 1_37_0 的安装以及在VC6.0中的使用
  • 原文地址:https://www.cnblogs.com/liubosong/p/12006473.html
Copyright © 2011-2022 走看看