1 listener在web.xml中进行配置,执行的顺序是context-param-》listener-》filter-》servlet-》spring
监听是在程序启动前所做的动作。就是在程序完全启动前把一些准备工作的事情放在监听类中来完成它。例如:读取配置文件,数据库连接等等
如: <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> //这时上下文加载监听,主要用于读取配置文件的
</listener>
2 还可以自定义监听类,继承implements ServletContextListener。
如:public class MyListener implements ServletContextListener{
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
System.out.println("自定义监听类销毁过程开始");
System.out.println("......");
System.out.println("自定义监听类销毁过程结束");
}
public void contextInitialized(ServletContextEvent arg0) {
// TODO Auto-generated method stub
System.out.println("自定义监听类初始化过程开始");
ServletContext servletContext=arg0.getServletContext();
String value=servletContext.getInitParameter("contextConfigLocation");
System.out.println("contextConfigLocation value is :"+value);
value=servletContext.getInitParameter("name");
System.out.println("name value is "+value);
System.out.println("自定义监听类初始化过程结束");
}
}
3 监听在程序中作用是在启动前完成一些指定的动作。有ServletContextListener和HttpSessionListener。
监听是在程序启动前所做的动作。就是在程序完全启动前把一些准备工作的事情放在监听类中来完成它。例如:读取配置文件,数据库连接等等
如: <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> //这时上下文加载监听,主要用于读取配置文件的
</listener>
2 还可以自定义监听类,继承implements ServletContextListener。
如:public class MyListener implements ServletContextListener{
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
System.out.println("自定义监听类销毁过程开始");
System.out.println("......");
System.out.println("自定义监听类销毁过程结束");
}
public void contextInitialized(ServletContextEvent arg0) {
// TODO Auto-generated method stub
System.out.println("自定义监听类初始化过程开始");
ServletContext servletContext=arg0.getServletContext();
String value=servletContext.getInitParameter("contextConfigLocation");
System.out.println("contextConfigLocation value is :"+value);
value=servletContext.getInitParameter("name");
System.out.println("name value is "+value);
System.out.println("自定义监听类初始化过程结束");
}
}
3 监听在程序中作用是在启动前完成一些指定的动作。有ServletContextListener和HttpSessionListener。