Servlet上下文监听器
来源:互联网 发布:sql删除表数据语句 编辑:程序博客网 时间:2024/06/06 05:40
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 {@Overridepublic void contextDestroyed(ServletContextEvent arg0) {// TODO Auto-generated method stub System.out.println("contextDestroyed被调用");}@Overridepublic void contextInitialized(ServletContextEvent arg0) {// TODO Auto-generated method stub System.out.println("contextInitialized方法被调用," + "ServletContext对象被初始化");}@Overridepublic void attributeAdded(ServletContextAttributeEvent arg0) {// TODO Auto-generated method stubSystem.out.println("attributeAdded方法被调用,servletContext中添加新的属性" +"属性名"+arg0.getName()+"属性值"+arg0.getValue());}@Overridepublic void attributeRemoved(ServletContextAttributeEvent arg0) {// TODO Auto-generated method stubSystem.out.println("attributeRemoved方法被调用,servletContext中删除了的属性" +"属性名"+arg0.getName()+"属性值"+arg0.getValue());}@Overridepublic void attributeReplaced(ServletContextAttributeEvent arg0) {// TODO Auto-generated method stubSystem.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页面,控制台的显示信息为
(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
attributeAdded方法被调用,servletContext中添加新的属性属性名username属性值zhangsan
attributeReplaced方法被调用,servletContext中更新了的属性属性名username属性值zhangsan
attributeRemoved方法被调用,servletContext中删除了的属性属性名username属性值xiaoli
0 0
- Servlet上下文监听器
- Servlet上下文监听器实例(转)
- 过滤器、监听器、上下文、servlet线程安全问题
- Servlet上下文
- Servlet 上下文
- Servlet上下文
- Servlet上下文
- Servlet上下文
- Servlet上下文
- web上下文监听器ServletContextListener
- Servlet监听器
- servlet监听器
- Servlet监听器
- Servlet监听器
- servlet 监听器
- servlet监听器
- Servlet监听器
- servlet监听器
- 在linux设备中搭建Qt运行环境
- 哲学家就餐问题——多线程代码学习
- Linux内核 定时器 用法
- NIO边看边记 之 通道之间的数据传输(五)
- QtCreater中使用opencv3.1.0创建项目
- Servlet上下文监听器
- HDU2050
- 鲜城告诉你去哪玩
- Activity+fragment+ViewPager方法调用
- Codeforces-665E-Beautiful Subarrays Trie树(字典树)
- Java 泛型(一) 泛型使用基础
- Java设计模式之工厂方法模式【Factory Method Pattern】
- Android中ListView内容超过一屏,上下滑动后数据紊乱的问题
- check the manual that corresponds to your MySQL server version for the right syntax to use near 'USI