初始Servlet监听器

Servlet监听器简介
Servlet监听器的作用是监听Web容器的有效事件,由容器管理。利用Listener接口监听在容器中的某个执行程序,并更具应用程序的需求做出适当的响应。

监听Servlet上下文
Servlet上下文监听可以监听ServletContext对象的创建、删除和添加属性,以及删除和修改操作,该监听器需要用到下面两个接口。

  • ServletContextListener接口:监听ServletContext的创建和删除。
class myServletContextListener implements ServletContextListener{

    //通知正在收听的对象应用程序已经被加载及初始化
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {

    }

    //通知正在收听的对象应用程序已经被载出,即关闭
    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
  • ServletAttributeListener接口:监听ServletContext属性的修改、删除和增加
class myServletAttributeListener implements ServletContextAttributeListener{
    //若有对象加入Application范围,通知正在收听的对象
    @Override
    public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent) {

    }
    //若有对象从Application范围移除,通知正在收听的对象
    @Override
    public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) {

    }
    //若有对象在Application范围内的一个对象被另一个对象替换,通知正在收听的对象
    @Override
    public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) {

    }
}

class myServletAttributeListener2 implements ServletRequestAttributeListener{

    @Override
    public void attributeAdded(ServletRequestAttributeEvent servletRequestAttributeEvent) {

    }

    @Override
    public void attributeRemoved(ServletRequestAttributeEvent servletRequestAttributeEvent) {

    }

    @Override
    public void attributeReplaced(ServletRequestAttributeEvent servletRequestAttributeEvent) {

    }
}

监听HTTP会话

  • HttpSessionListener接口:监听HTTP会话的创建及销毁

class myHttpSessionListener implements HttpSessionListener{

    //通知正在收听的对象,session已经被加载及初始化
    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {

    }
    //通知正在收听的对象,session被载出
    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {

    }
}
  • HttpSessionActivationListener接口:监听HTTP会话active和passivate情况
class myHttpSessionActivationListener implements HttpSessionActivationListener{
    //通知正在收听的对象,其session状态变为有效状态
    @Override
    public void sessionWillPassivate(HttpSessionEvent httpSessionEvent) {

    }
    //通知正在收听的对象,其session状态变为无效状态
    @Override
    public void sessionDidActivate(HttpSessionEvent httpSessionEvent) {

    }
}
  • HttpSessionBindingListener接口:监听HTTP会话中对象的绑定信息,它是唯一不需要再web.xml中设置Listener的。
class myHttpBindingListener implements HttpSessionBindingListener{
    //当有对象加入session的范围时,会被自动调用
    @Override
    public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {

    }
    //当有对象从session的范围移除时,会被自动取消调用
    @Override
    public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {

    }
}
  • HttpSessionAttributeListener接口:监听HTTP会话中属性的设置请求。
class myHttpSessionAttributeListener implements HttpSessionAttributeListener {

    //若有对象加入Session范围,通知正在收听的对象
    @Override
    public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {

    }

    //若有对象从Session范围移除,通知正在收听的对象
    @Override
    public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {

    }

    //若有对象在Session范围内的一个对象被另一个对象替换,通知正在收听的对象
    @Override
    public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {

    }
}

监听Servlet请求

  • ServletRequestListener接口:
class myServletRequestListener implements ServletRequestListener{

    //通知正在收听的对象,ServletRequest已经被加载及初始化
    @Override
    public void requestDestroyed(ServletRequestEvent servletRequestEvent) {

    }
    //通知正在收听的对象,ServletRequest已经被载出,即关闭
    @Override
    public void requestInitialized(ServletRequestEvent servletRequestEvent) {

    }
}
  • ServletRequestAttributeListener接口:
class myServletRequestAttributeListener implements ServletRequestAttributeListener{
    //若有对象加入Request范围,通知正在收听的对象
    @Override
    public void attributeAdded(ServletRequestAttributeEvent servletRequestAttributeEvent) {

    }
    //若有对象从Request范围移除,通知正在收听的对象
    @Override
    public void attributeRemoved(ServletRequestAttributeEvent servletRequestAttributeEvent) {

    }
    //若有对象在Request范围内的一个对象被另一个对象替换,通知正在收听的对象
    @Override
    public void attributeReplaced(ServletRequestAttributeEvent servletRequestAttributeEvent) {

    }
}

下面使用一个小例子:使用监听器查看在线用户
UserInfoList类:

package com.java.model;

import java.util.ArrayList;

/** * Created with IntelliJ IDEA. * Created by YEN on 2016/7/10 and 22:05. */
public class UserInfoList {
    private static UserInfoList userInfoList=new UserInfoList();
    private ArrayList arrayList=null;
    public UserInfoList(){
        this.arrayList=new ArrayList();
    }
    public static UserInfoList getInstance(){
        return userInfoList;
    }
    //增加用户
    public boolean addUserInfo(String username){
        if(username!=null){
            this.arrayList.add(username);
            return true;
        }else return false;
    }
    //获取用户列表
    public ArrayList getUserList(){
        return arrayList;
    }
    //移除用户
    public void removeUserInfo(String username){
        if(username!=null){
            arrayList.remove(username);
        }
    }
}

UserInfoTrace类:

package com.java.model;

import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

/** * Created with IntelliJ IDEA. * Created by YEN on 2016/7/10 and 22:12. */

public class UserInfoTrace implements HttpSessionBindingListener{
    private String username;
    private UserInfoList container=UserInfoList.getInstance();
    public UserInfoTrace(){
        this.username="";
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    @Override
    public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {
        System.out.println("上线................"+this.username);
    }

    @Override
    public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {
        System.out.println("下线................"+this.username);
        if(username!=""){
            container.removeUserInfo(username);
        }
    }
}

login.jsp页面

<%-- Created by IntelliJ IDEA. User: YEN Date: 2016/7/9 Time: 16:29 To change this template use File | Settings | File Templates. --%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script> function checkForm() { if(document.getElementById("username").value==null || document.getElementById("username").value==""){ alert("用户名不能为空!"); return false; } if(document.getElementById("password").value==null || document.getElementById("password").value==""){ alert("密码不能为空!"); return false; } return true; } </script>
</head>
<body>
<form name="form1" action="<%=request.getContextPath()%>/showUserInfo.jsp" method="get" onsubmit="return checkForm()">
    username:<input type="text" name="username" id="username">   <br>
    password:<input type="password" name="password" id="password">   <br>
    <input type="submit" value="提交">
</form>
</body>
</html>

showUserInfo.jsp页面

<%-- Created by IntelliJ IDEA. User: YEN Date: 2016/7/10 Time: 22:16 To change this template use File | Settings | File Templates. --%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page import="com.java.model.UserInfoList" language="java" %>
<%@page import="com.java.model.UserInfoTrace" language="java" %>
<%@ page import="java.util.ArrayList" %>
<html>
<head>
    <title>UserInfoPage</title>
</head>
<body>
<% UserInfoList userInfoList=UserInfoList.getInstance(); //获得UserInfoList类的对象 UserInfoTrace userInfoTrace=new UserInfoTrace(); //创建UserInfoTrace类的对象 request.setCharacterEncoding("UTF-8"); String username=request.getParameter("username"); userInfoTrace.setUsername(username); session.setAttribute("list",userInfoTrace); userInfoList.addUserInfo(username); session.setMaxInactiveInterval(30); //session过期时间为30s %>
<textarea rows="8" cols="20">
    <% ArrayList arrayList=userInfoList.getUserList(); if(arrayList!=null && arrayList.size()>0 ){ for (int i = 0; i < arrayList.size(); i++) { out.print(arrayList.get(i)); } } %>
</textarea>
</body>
</html>

运行结果:
一:在login.jsp页面输入信息
初始Servlet监听器_第1张图片
二:点击登陆按钮
初始Servlet监听器_第2张图片
初始Servlet监听器_第3张图片

三:在登陆一个用户
初始Servlet监听器_第4张图片

你可能感兴趣的:(servlet,监听器,listener,监听器查看在线用户)