servlet监听总结
2012-12-04 14:36
176 查看
监听器的作用:自动执行一些操作。
三种servlet监听器:
对request的监听。对session的监听。对application的监听。
怎么创建一个session监听器:
1,生成一个普通的class类,如果是对session的监听,则实现HttpSessionListener。
然后重写里面的五个方法:
public void sessionCreated(HttpSessionEvent arg0) {} // 创建
public void sessionDestroyed(HttpSessionEvent arg0) {} // 销毁
public void attributeAdded(HttpSessionEvent arg0) {} // 增加
public void attributeRemoved(HttpSessionEvent arg0) {} // 删除
public void attributeReplaced(HttpSessionEvent arg0) {} // 替换
2、Servlet上下文进行监听(Application级):
用于监听 ServletContext 对象的创建和删除以及属性的添加、删除和修改等操作,该监听器需要用到如下两个接口类:
(1) ServletContextAttributeListener:监听对 ServletContext 属性的操作,比如增加、删除、修改
attributeAdded(ServletContextAttributeEvent e) 添加属性时调用
attributeReplaced(ServletContextAttributeEvent e) 修改属性时调用
attributeRemoved(ServletContextAttributeEvent e) 删除属性时调用
(2) ServletContextListener:监听对 ServletContext 对象的创建和删除
contextInitialized(ServletContextEvent sce) 初始化时调用
contextDestroyed(ServletContextEvent sce) 销毁时调用,即当服务器重新加载时调用
3、 对客户端请求进行监听(Requst级):
用于对客户端的请求进行监听是在 Servlet2.4 规范中新添加的一项新技术,使用的接口如下:
(1) ServletRequestListener 接口类
requestDestroyed(ServletRequestEvent e) 对销毁客户端进行监听,即当执行 request.removeAttribute("xxx") 时调用
requestInitialized(ServletRequestEvent e) 对实现客户端的请求进行监听
(2) ServletRequestAttributeListener 接口类
attributeAdded(ServletRequestAttributeEvent e) 对属性添加进行监听
attributeRemoved(ServletRequestAttributeEvent e) 对属性删除进行监听
attributeReplaced(ServletRequestAttributeEvent e) 对属性替换进行监听
三种servlet监听器:
对request的监听。对session的监听。对application的监听。
怎么创建一个session监听器:
1,生成一个普通的class类,如果是对session的监听,则实现HttpSessionListener。
然后重写里面的五个方法:
public void sessionCreated(HttpSessionEvent arg0) {} // 创建
public void sessionDestroyed(HttpSessionEvent arg0) {} // 销毁
public void attributeAdded(HttpSessionEvent arg0) {} // 增加
public void attributeRemoved(HttpSessionEvent arg0) {} // 删除
public void attributeReplaced(HttpSessionEvent arg0) {} // 替换
2、Servlet上下文进行监听(Application级):
用于监听 ServletContext 对象的创建和删除以及属性的添加、删除和修改等操作,该监听器需要用到如下两个接口类:
(1) ServletContextAttributeListener:监听对 ServletContext 属性的操作,比如增加、删除、修改
attributeAdded(ServletContextAttributeEvent e) 添加属性时调用
attributeReplaced(ServletContextAttributeEvent e) 修改属性时调用
attributeRemoved(ServletContextAttributeEvent e) 删除属性时调用
(2) ServletContextListener:监听对 ServletContext 对象的创建和删除
contextInitialized(ServletContextEvent sce) 初始化时调用
contextDestroyed(ServletContextEvent sce) 销毁时调用,即当服务器重新加载时调用
3、 对客户端请求进行监听(Requst级):
用于对客户端的请求进行监听是在 Servlet2.4 规范中新添加的一项新技术,使用的接口如下:
(1) ServletRequestListener 接口类
requestDestroyed(ServletRequestEvent e) 对销毁客户端进行监听,即当执行 request.removeAttribute("xxx") 时调用
requestInitialized(ServletRequestEvent e) 对实现客户端的请求进行监听
(2) ServletRequestAttributeListener 接口类
attributeAdded(ServletRequestAttributeEvent e) 对属性添加进行监听
attributeRemoved(ServletRequestAttributeEvent e) 对属性删除进行监听
attributeReplaced(ServletRequestAttributeEvent e) 对属性替换进行监听
相关文章推荐
- Filter学习总结,顺便提及点servlet3.0异步filter和异步监听
- Jsp+servlet+javabean使用Lucene3.6.2做一个简单数据库搜索的总结(2)
- HttpServletRequest和HttpServletResponse的方法总结
- Servlet3.0学习总结(一)——使用注解标注Servlet
- javaweb学习总结(十)——HttpServletRequest对象(一)
- Servlet中listener(监听器)和filter的总结
- [置顶] Servlet入门---张国亮--->总结心德第二季
- Servlet总结
- Servlet总结2
- Servlet之过滤器总结--Filter
- Servlet总结(系列文章)
- Servlet总结03——会话跟踪
- 09 ListView监听 ExpandableListView的使用总结
- ServletContext&Request&Session 三个域对象的总结
- HttpServlet及Servlet Container原理总结
- [置顶] Servlet知识总结
- Servlet 学习总结-1
- javaEE之servlet总结
- jsp servlet中的过滤器Filter配置总结
- 监听总结之1.“正在连接到”后面跟着的内容受哪些因素影响(同名不同配置)