Servlet上下文监听器

Servlet上下文监听器

用于实现Servlet上下文监听的接口为ServletContextListener和ServletContextAttributeListener。其中,ServletContextListener用于监听ServletContext对象的创建和销毁,ServletContextAttributeListener用于监听
ServletContext对象中属性的增加、修改和删除。

 ServletContextListener接口中定义的方法:
1.void contextInitialized(ServletContextEvent sec)    说明加载web应用程序时,即创建ServletContexxt对象时,web容器调用此方法。
2.void  contextDestroyed(ServletContextEvent sec)  说明加载web应用程序时,即销毁ServletContext对象时,web容器调用此方法。

servletContextAttributeListener接口中定义的方法
1.void attributeAdded(ServletContextAttributeEvent  scae)  说明Servlet上下文中增加属性时由Web容器调用
2 void attributeRemoved( ServletContextAttributeEvent scae)  说明Servlet上下文中属性被删除时由Web容器调用。
3.void attributeReplaced(ServletContextAttributeEvent Scae)   说明上下文中属性被更新时由Web容器调用。
下面进行监听器的测试:
<span style="color:#333333;font-weight: bold;">package com.cn.contextlistener;</span><span style="color:#ff0000;"><strong>//包名</strong></span><span style="color:#333333;font-weight: bold;">

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class MyServletContextListener implements ServletContextListener,
		ServletContextAttributeListener {

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub
                 System.out.println("contextDestroyed被调用");
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		// TODO Auto-generated method stub
	       System.out.println("contextInitialized方法被调用," +
	       		"ServletContext对象被初始化");
	}

	@Override
	public void attributeAdded(ServletContextAttributeEvent arg0) {
		// TODO Auto-generated method stub
		System.out.println("attributeAdded方法被调用,servletContext中添加新的属性" +
				"属性名"+arg0.getName()+"属性值"+arg0.getValue());
	}

	@Override
	public void attributeRemoved(ServletContextAttributeEvent arg0) {
		// TODO Auto-generated method stub
		System.out.println("attributeRemoved方法被调用,servletContext中删除了的属性" +
				"属性名"+arg0.getName()+"属性值"+arg0.getValue());
	}

	@Override
	public void attributeReplaced(ServletContextAttributeEvent arg0) {
		// TODO Auto-generated method stub
		System.out.println("attributeReplaced方法被调用,servletContext中更新了的属性" +
				"属性名"+arg0.getName()+"属性值"+arg0.getValue());
	}

}
</span>
XML代码:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>测试MyServletContextListener监听器</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
      <%
        //在ServletContext对象中添加新属性username
        application.setAttribute("username","zhangsan");
        out.print("添加属性<br>");
        //更改属性username
        application.setAttribute("username","xiaoli");
      	out.print("更新属性<br>");
      	//移除属性username
      	application.removeAttribute("username");
      	out.print("移除属性");
       %>
  </body>
</html>
在web.xml中配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <listener>
    <listener-class>com.cn.contextlistener.MyServletContextListener</listener-class>
  </listener>
</web-app>
(1)服务器已启动时,停止服务器,控制台的输出信息为:
         contextDestroyed被调用
(2)重新启动服务器,控制台的显示信息为:
contextInitialized方法被调用,ServletContext对象被初始化
(3)访问index.jsp页面,控制台的显示信息为
attributeAdded方法被调用,servletContext中添加新的属性属性名org.apache.jasper.runtime.JspApplicationContextImpl属性值org.apache.jasper.runtime.JspApplicationContextImpl@1712651
attributeAdded方法被调用,servletContext中添加新的属性属性名username属性值zhangsan
attributeReplaced方法被调用,servletContext中更新了的属性属性名username属性值zhangsan
attributeRemoved方法被调用,servletContext中删除了的属性属性名username属性值xiaoli

你可能感兴趣的:(servlet,对象,jar,Servlet上下文监听器)