listener:监听器概述以及生命周期监听器

java中的监听器

事件源:三大域

--1.ServletContext

  • 生命周期监听:ServletContextListener ,它有两个方法,一个出生时调用,一个在死亡时调用!
  • 属性监听:ServletContextbuteListener ,它有两个方法,一个在添加属性时调用,一个在替换属性时调用,最后一个在移除属性时调用!

--2.HttpSession

  • 生命周期监听:HttpSessionListener ,它有两个方法,一个出生时调用,一个在死亡时调用!
  • 属性监听:HttpSessionbuteListener ,它有两个方法,一个在添加属性时调用,一个在替换属性时调用,最后一个在移除属性时调用!

--3.ServletRequest

  • 生命周期监听:ServletRequestListener ,它有两个方法,一个出生时调用,一个在死亡时调用!
  • 属性监听:ServletRequestbuteListener ,它有两个方法,一个在添加属性时调用,一个在替换属性时调用,最后一个在移除属性时调用!
package com.tinstu.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * ServletContext生死监听
 * 可以在这个监听器寻访一些在Tomcat启动时就要完成的代码
 *
 */
public class test implements ServletContextListener {

   public void contextDestroyed(ServletContextEvent arg0)  {
    	System.out.println("服务器关闭,我挂!");
    }
    public void contextInitialized(ServletContextEvent arg0)  { 
    	System.out.println("服务器启动,我被创建");
    }
}

xml中的配置:

 

  <listener>
    <listener-class>com.tinstu.listener.AListener</listener-class>
  </listener>

 javaweb中完成编写监听器

 

1.写一个监听器类,要求必须去实现某个监听器接口;

2.注册,是在web.xml中配置完成注册!

事件对象

-*ServletContextEvent:  ServletContext getServletContext()
-*HttpServletEvent:HttpSession()
-*ServletRequest:
---**ServletContext getServletContext();
---**servletReques getServletRequest();

 属性监听器

创建属性监听器时,需要勾选

监听器 test2.java

package com.tinstu.listener;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.annotation.WebListener;

public class test2 implements ServletContextAttributeListener {

    public void attributeAdded(ServletContextAttributeEvent arg0)  { 
	System.out.println("您向application中添加了一个名为"+arg0.getName()+",值为:"+arg0.getValue()+"的属性");
    }
    public void attributeRemoved(ServletContextAttributeEvent arg0)  { 
    	System.out.println(arg0.getName()+"="+arg0.getValue());
    }
    public void attributeReplaced(ServletContextAttributeEvent arg0)  { 
    	System.out.println(arg0.getName()+"="+arg0.getValue()+","
    +arg0.getServletContext().getAttribute(arg0.getName()));
    }
}

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
application.setAttribute("xxx", "yyy");
%>
</body>
</html>

运行结果:

之后访问 replace.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
application.setAttribute("xxx", "yyy222");
%>
</body>
</html>

再运行remove.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
application.setAttribute("xxx", "yyy3333");
%>
</body>
</html>

阅读剩余
THE END