标签:
1 ServletContextListener
监听ServletContext对象被创建和销毁的Servlet监听器
开发:>创建一个实现了ServletContextListener的类并且实现里面的两个方法
public class HelloServletContextListener implements ServletContextListener
public class HelloServletContextListener implements ServletContextListener { public void contextDestroyed(ServletContextEvent sce) { // TODO Auto-generated method stub System.out.println("销毁"); } public void contextInitialized(ServletContextEvent sce) { // TODO Auto-generated method stub System.out.println("创建"); }
>并在web.xml里配置该Listener
<!-- 配置listener --> <listener> <listener-class>cn.stud.wlc.listener.HelloServletContextListener</listener-class> </listener>
>ServletContextListener最常用在web应用加载时对当前web应用的相关资源进行初始化操作(创建数据库链接池。。。)
(2,3)ServletRequestListener& HttpSessionListener和ServletContextListener类似
public class HelloServletContextListener implements ServletContextListener,HttpSessionListener,ServletRequestListener { public void contextDestroyed(ServletContextEvent sce) { // TODO Auto-generated method stub System.out.println("销毁"); } public void contextInitialized(ServletContextEvent sce) { // TODO Auto-generated method stub System.out.println("创建"); } public void requestDestroyed(ServletRequestEvent sre) { // TODO Auto-generated method stub } public void requestInitialized(ServletRequestEvent sre) { // TODO Auto-generated method stub } public void sessionCreated(HttpSessionEvent se) { // TODO Auto-generated method stub } public void sessionDestroyed(HttpSessionEvent se) { // TODO Auto-generated method stub } }
标签:
原文地址:http://www.cnblogs.com/wlc297984368/p/5436924.html