首页 > 代码库 > Servlet的三种监听器
Servlet的三种监听器
Servlet的三种监听器
1:对Servlet上下文进行监听:
可以监听ServletContext对象的创建和删除以及属性的添加、删除和修改等操作,该监听器需要用到如下两个接口类:
(1)ServletContextAttributeListener:监听对ServletContext属性的操作,比如增加、删除、修改。
attributeAdded(ServletContextAttributeEvent sa)添加属性时调用
attributeReplaced(ServletContextAttributeEvent sa)修改属性时调用
attributeRemoved(ServletContextAttributeEvent sa)删除属性时调用
(2)ServletContextListener:监听对ServletContext对象的创建和删除。
contextInitialized(ServletContextEvent sce)初始化时调用
contextDestroyed(ServletContextEvent sce)销毁时调用,即当服务器重新加载时调用
/*******************************************************************************/
相关案例
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class ServletContext implements ServletContextListener,ServletContextAttributeListener{
//上下文属性:添加、删除、修改监听
//1.上下文属性添加监听
public void attributeAdded(ServletContextAttributeEvent scae) {
print("增加ServletContext对象的一个属性:"+scae.getName()+","+scae.getValue());
}
//2.上下文属性删除监听
public void attributeRemoved(ServletContextAttributeEvent scae) {
print("删除ServletContext对象的一个属性:"+scae.getName()+","+scae.getValue());
}
//3.上下文属性修改监听
public void attributeReplaced(ServletContextAttributeEvent scae) {
print("修改ServletContext对象的一个属性:"+scae.getName()+","+scae.getValue());
}
//上下文对象:创建、销毁监听
//1.上下文对象创建监听
public void contextInitialized(ServletContextEvent sce) {
print("ServletContext初始化......"); }
//2.上下文对象销毁监听
public void contextDestroyed(ServletContextEvent sce) {
print("ServletContext销毁......"); }
@SuppressWarnings("deprecation")
private void print(String message){
//调用该方法在txt文件中打印出message字符串信息
PrintWriter out = null;
try {
out = new PrintWriter(new FileOutputStream("F:context_result.txt",true));
out.println(new java.util.Date().toLocaleString()+"contextListener:"+message);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();) }
}
}
2:监听http会话:
可以监听http会话活动情况和http会话中的属性设置情况,也可以监听http会话的active和passivate情况等,该监听器需要用到如下多个接口类:
(1)HttpSessionListener:监听httpSession的操作。
sessionCreate(HttpSessionEvent se)初始化时调用;
sessionDestroyed(httpSessionEvent se)销毁时调用,即当用户注销时调用
(2)HttpSessionActivationListener:用于监听http会话的active和passivate情况
(3)HttpSessionAttributeListener:监听HttpSession中的属性操作
attributeAdded(HttpSessionBindingEvent se)添加属性时调用
attributeRemoved(HttpSessionBindingEvent se)删除属性时调用
attributeReplaced(HttpSessionBindingEvent se)修改属性时调用
/*******************************************************************************/
相关案例
import java.io.FileNotFoundException
import java.io.FileOutputStream;
import java.io.PrintWriter; import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class SessionListener implements HttpSessionActivationListener,HttpSessionAttributeListener,
HttpSessionListener,ServletContextListener{
ServletContext context;
int users;
//session属性监听
//1.监听属性添加
public void attributeAdded(HttpSessionBindingEvent arg0) {
print("attributeAdded(‘"+arg0.getSession().getId()+"‘,‘"+arg0.getName()+"‘,‘"+arg0.getValue()+"‘)");
}
//2.监听属性删除
public void attributeRemoved(HttpSessionBindingEvent arg0) {
print("attributeRemoved(‘"+arg0.getSession().getId()+"‘,‘"+arg0.getName()+"‘,‘"+arg0.getValue()+"‘)");
}
//3.监听属性替换
public void attributeReplaced(HttpSessionBindingEvent arg0) {
print("attributeReplaced(‘"+arg0.getSession().getId()+"‘,‘"+arg0.getName()+"‘,‘"+arg0.getValue()+"‘)");
}
//session会话创建监听
//1.监听会话创建
public void sessionCreated(HttpSessionEvent arg0) {
users++;
print("sessionCreated(‘"+arg0.getSession().getId()+"‘),目前拥有"+users+"个用户");
context.setAttribute("users",new Integer(users));
}
//2.监听会话释放
public void sessionDestroyed(HttpSessionEvent arg0) {
users--;
print("sessionDestroyed(‘"+arg0.getSession().getId()+"‘),目前拥有"+users+"个用户");
context.setAttribute("users",new Integer(users));
}
public void contextInitialized(ServletContextEvent arg0) {
this.context = arg0.getServletContext();
this.print("ServletContext的初始化.....");
}
public void contextDestroyed(ServletContextEvent arg0) {
this.context = null;
print("ServletContext的释放");
}
//session活动监听
//1.监听会话的active情况
public void sessionDidActivate(HttpSessionEvent arg0) {
print("sessionDidActivate("+arg0.getSession().getId()+")");
}
//2.监听会话的passivate情况
public void sessionWillPassivate(HttpSessionEvent arg0) {
print("sessionWillPassivate("+arg0.getSession().getId()+")");
}
@SuppressWarnings("deprecation")
private void print(String message){
//调用该方法在txt文件中打印出message字符串信息
PrintWriter out = null;
try {
out = new PrintWriter(new FileOutputStream("F:session_result.txt",true));
out.println(new java.util.Date().toLocaleString()+"contextListener:"+message);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
3:对客户端请求进行监听:
对客户端的请求进行监听是在Servlet2.4规范中新添加的一项新技术,使用的接口如下:
(1)ServletRequestListener接口类
requestDestroyed(ServletRequestEvent arg0)对销毁客户端进行监听,即当执行request.removeAttribute("user")时调用
requestInitialized(ServletRequestEvent arg0)对实现客户端的请求进行监听
(2)ServletRequestAttributeListener接口类
attributeAdded(ServletRequestAttributeEvent arg0)对属性添加进行监听
attributeRemoved(ServletRequestAttributeEvent arg0)对属性删除进行监听
attributeReplaced(ServletRequestAttributeEvent arg0)对属性替换进行监听
/*******************************************************************************/
相关案例
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
public class RequestListener implements ServletRequestListener,ServletRequestAttributeListener{
//监听请求的初始化与否
//1.对实现客户端请求进行监听
public void requestInitialized(ServletRequestEvent arg0) {
print("request的初始化");
print(arg0.getServletRequest().getRemoteAddr()+";"+arg0.getServletRequest().getRemoteHost()+","+arg0.getServletRequest().getRemotePort());
}
//2.对销毁客户端请求进行监听
public void requestDestroyed(ServletRequestEvent arg0) {
print("request的销毁");
}
//监听请求属性
//1.对属性的添加进行监听
public void attributeAdded(ServletRequestAttributeEvent arg0) {
print("attributeAdded(‘"+arg0.getName()+"‘,‘"+arg0.getValue()+"‘)");
}
//2.对属性的删除进行监听
public void attributeRemoved(ServletRequestAttributeEvent arg0) {
print("attributeRemoved(‘"+arg0.getName()+"‘,‘"+arg0.getValue()+"‘)");
}
//3.对属性的更新进行监听
public void attributeReplaced(ServletRequestAttributeEvent arg0) {
print("attributeReplaced(‘"+arg0.getName()+"‘,‘"+arg0.getValue()+"‘)");
}
@SuppressWarnings("deprecation")
private void print(String message){
//调用该方法在txt文件中打印出message字符串信息
PrintWriter out = null;
try {
out = new PrintWriter(new FileOutputStream("F: equest_result.txt",true));
out.println(new java.util.Date().toLocaleString()+"RequestListener:"+message);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Servlet的三种监听器