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在實際項目開發中,用到的不多。這里最好知道有這么回事就成。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM