servlet监听器Listener(理论+例子)

Listener采用了观察者模式(24种模式之一),Listener是servlet的监听器,他可以监听客户端的请求、服务器端的操作等, 通过监听器,可以自动激发一些操作。比如:监听在线用户数量

当增加一个HttpSession时,就会激发sessinCreated(HttpSessionEvent sce)方法,这样就可以给在线人数+1了。

常见的监听器接口:

ServletContextAttributeListener 监听对servletContext属性的操作,比如删除、增加、修改属性等

ServletContextListener 监听ServletContext,当创建ServletContext时,激发contextInitialized(ServletContextEvent sce)方法,当销毁ServletContext时,激发ContextDestory(ServletContextEvent sce)方法、

实例:

首先配置web.xml

  <!--servlet 监听器  start-->
  <listener>
      <listener-class>com.listener.MyServletContextListener</listener-class>
  </listener>
  
  <listener>
      <listener-class>com.listener.MyServletContextAttributeListener</listener-class>
  </listener>
  <!-- servlet 监听器 end -->
web.xml

2.ServletContextListener接口的调用

/**
 * 在xml中配置监听器
 * ServletContextListener 坚挺servletContext,当创建servletContext时,激发ContextInitialized(ServletContextEvent sce)方法
 * 当销毁servletContext时,激发contextDestoryed(ServletContextEvent sce)方法
 */
package com.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;


public class MyServletContextListener implements ServletContextListener
{

    @Override
    public void contextDestroyed(ServletContextEvent arg0)
    {
        System.out.println(arg0.toString());
        
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0)
    {
        System.out.println(arg0.toString());
    }
    
}
MyServletContextListener

3.ServletContextAttributeListener接口的调用

package com.listener;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;

public class MyServletContextAttributeListener implements ServletContextAttributeListener
{

    @Override
    public void attributeAdded(ServletContextAttributeEvent arg0)
    {
        System.out.println("attributeAdd ");
        System.out.println(arg0.getName() + ":" + arg0.getValue());
        
    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent arg0)
    {
        System.out.println("attributeRemoved");
        System.out.println(arg0.getName() + ":" + arg0.getValue());//value值将显示之前的 值
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent arg0)
    {
        System.out.println("attributeRepaced");
        System.out.println(arg0.getName() + ":" + arg0.getValue());//value值将显示之前的 值
        
    }
    
}
MyServletContextAttributeListener

为了方便,没有用servlet举例,直接写的jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<%

    application.setAttribute("name1","value1");
    application.setAttribute("name2","value2");
    application.setAttribute("name1","value3");
    application.setAttribute("name2","value4");
%>
<a href="listener2.jsp">next</a>
</body>
</html>
listener1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
    application.removeAttribute("name1");
%>
</body>
</html>
listener2.jsp

结束。listener在实际项目开发中,用到的不多。这里最好知道有这么回事就成。

原文地址:https://www.cnblogs.com/feiguo/p/3804451.html