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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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


技术分享
  <!--servlet 监听器  start-->
  <listener>
      <listener-class>com.listener.MyServletContextListener</listener-class>
  </listener>
  
  <listener>
      <listener-class>com.listener.MyServletContextAttributeListener</listener-class>
  </listener>
  <!-- servlet 监听器 end -->
技术分享

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


技术分享
/**
 * 在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());
    }
    
}
技术分享

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


技术分享
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值将显示之前的 值
        
    }
    
}
技术分享

为了方便,没有用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.setAttribute("name1","value1");
    application.setAttribute("name2","value2");
    application.setAttribute("name1","value3");
    application.setAttribute("name2","value4");
%>
<a href="listener2.jsp">next</a>
</body>
</html>
技术分享

<%@ 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


技术分享
<%@ 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>
技术分享

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

以上是关于servlet监听器Listener(理论+例子)的主要内容,如果未能解决你的问题,请参考以下文章

java_day29_Servlet三大组件之监听器listener

Servlet3.0的注解自定义原生Listener监听器实战

JavaWeb开发——servlet监听器 listener

servlet技术中的listener有啥具体作用

Servlet监听器(Listener)

2020.05.03 Listener监听器