标签:
一、ServletContextListener
Method Summary |
|
|
|
|
|
代码举例:
package com.kdyzm.listener; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class MyServletContextListener implements ServletContextListener{ @Override public void contextDestroyed(ServletContextEvent sce) { ServletContext sc=sce.getServletContext(); System.out.println(sc+"被销毁!"); } @Override public void contextInitialized(ServletContextEvent sce) { System.out.println(sce.getServletContext()+"初始化!"); } }
二、ServletContextAttributeListener
Method Summary |
|
|
|
|
|
|
使用方法和HttpSessionAttriuteListener的用法相似,略。
三、HttpSessionBindingListener
1.功能:监听一个Bean是否被放到了Session中。
2.特点:该接口需要被Bean实现才能正常发挥作用,实现该接口的Bean不需要配置到web.xml文件中。
3.举例说明:
package com.kdyzm.domain; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionBindingListener; public class Person implements HttpSessionBindingListener{ private String name; public Person() { } public Person(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Person [name=" + name + "]"; } @Override public void valueBound(HttpSessionBindingEvent event) { System.out.println(this+"被加入到session中!"); } @Override public void valueUnbound(HttpSessionBindingEvent event) { System.out.println(this+"被移出session!"); } }
<%@page import="com.kdyzm.domain.Person"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html; charset=utf-8" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <title>Insert title here!</title> </head> <body> <% Person p=new Person("小黄"); session.setAttribute("p", p); out.println(session.getAttribute("p")); session.removeAttribute("p"); out.println(session.getAttribute("p")); %> </body> </html>
标签:
原文地址:http://www.cnblogs.com/kuangdaoyizhimei/p/4592900.html