📜  Servlet – HttpSessionEvent 和 HttpSessionListener

📅  最后修改于: 2022-05-13 01:55:24.698000             🧑  作者: Mango

Servlet – HttpSessionEvent 和 HttpSessionListener

在Java中, HttpSessionEvent是一个类,它表示 Web 应用程序中会话更改或更新的事件通知。同样,该事件的接口是HttpSessionListener ,用于接收有关 HttpSession 生命周期变化的通知事件。作为接受这些通知事件的一种方式,类的实现必须遵循以下之一:

  • 它应该在 Web 应用程序的部署描述符中声明,并使用 WebListener 进行注释
  • 它应该通过 ServletContext 上定义的 addListener 方法之一注册

HttpSessionListner 接口有两种方法:

  1. sessionCreated :它接收会话已创建的通知。
  2. sessionDestroyed :它接收到会话几乎失效的通知。

例子

使用 HttpSessionEvent 和 HttpSessionListener 计算总会话数和活动会话数。现在,我们必须创建这些文件:

  • 索引.html
  • 计数用户列表。Java
  • 登录Servlet。Java
  • 注销Servlet。Java

index.html:登录凭据

HTML


  
    

Login Credentials

       
            Username:
         Password:
                   
        


Java
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
  
public class CountUserListener implements HttpSessionListener{
    ServletContext scx =null;
    static int all =0, active =0;
  
    // It receives the notification that
    // a session has been created.
    public void sessionCreated(HttpSessionEvent se) 
    {
        all++;
        active++;
  
        scx =se.getSession().getServletContext();
        scx.setAttribute("All Users", all);
        scx.setAttribute("Active Users", active);
  
    }
  
      // It receives the notification that 
      // a session is almost invalidated
    public void sessionDestroyed(HttpSessionEvent se) 
    {
        active--;
        scx.setAttribute("Active Users", active);
    }
  
}


Java
import java.io.IOException;
import java.io.PrintWriter;
    
import javax.servlet.ServletContext;
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
  
public class LoginServlet extends HttpServlet {
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
            throws ServletException, IOException {
  
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();
  
        String str = request.getParameter("Username");
        out.print("Welcome "+str);
  
        HttpSession session = request.getSession();
        session.setAttribute("Uname",str);
  
        // this retrieve data from ServletContext object
        ServletContext scx = getServletContext();
        int au = (Integer)scx.getAttribute("All Users");
        int acu = (Integer)scx.getAttribute("Active Users");
        out.print("
All Users = "+au);         out.print("
Active Users = "+acu);            out.print("
Logout");            out.close();     }    }


Java
import java.io.IOException;
import java.io.PrintWriter;
  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import javax.servlet.http.HttpSession;
  
public class LogoutServlet extends HttpServlet {
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
            throws ServletException, IOException {
  
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
  
        HttpSession session=request.getSession(false);
        
        // This invalidates the session
        session.invalidate();
  
        out.print("You are successfully logged out");
        out.close();
    }
  
}


计数用户列表。 Java:此侦听器类计算总会话数和活动会话数,并将此信息作为属性存储在 ServletContext 对象中。

Java

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
  
public class CountUserListener implements HttpSessionListener{
    ServletContext scx =null;
    static int all =0, active =0;
  
    // It receives the notification that
    // a session has been created.
    public void sessionCreated(HttpSessionEvent se) 
    {
        all++;
        active++;
  
        scx =se.getSession().getServletContext();
        scx.setAttribute("All Users", all);
        scx.setAttribute("Active Users", active);
  
    }
  
      // It receives the notification that 
      // a session is almost invalidated
    public void sessionDestroyed(HttpSessionEvent se) 
    {
        active--;
        scx.setAttribute("Active Users", active);
    }
  
}

登录Servlet。 Java:这个 Servlet 类创建一个会话并打印总活跃用户数和当前活跃用户数。

Java

import java.io.IOException;
import java.io.PrintWriter;
    
import javax.servlet.ServletContext;
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
  
public class LoginServlet extends HttpServlet {
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
            throws ServletException, IOException {
  
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();
  
        String str = request.getParameter("Username");
        out.print("Welcome "+str);
  
        HttpSession session = request.getSession();
        session.setAttribute("Uname",str);
  
        // this retrieve data from ServletContext object
        ServletContext scx = getServletContext();
        int au = (Integer)scx.getAttribute("All Users");
        int acu = (Integer)scx.getAttribute("Active Users");
        out.print("
All Users = "+au);         out.print("
Active Users = "+acu);            out.print("
Logout");            out.close();     }    }

注销Servlet。 Java:这个 Servlet 类使会话无效。

Java

import java.io.IOException;
import java.io.PrintWriter;
  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import javax.servlet.http.HttpSession;
  
public class LogoutServlet extends HttpServlet {
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
            throws ServletException, IOException {
  
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
  
        HttpSession session=request.getSession(false);
        
        // This invalidates the session
        session.invalidate();
  
        out.print("You are successfully logged out");
        out.close();
    }
  
}