当我们去启动一个 WEB 项目的时候, 容器(包括 JBoss, Tomcat 等)首先会去读项目的 web.xml 配置文件里面的信息,
当这一步骤没有出错并且完成之后, 项目才能正常的被启动起来。
1> 首先是, 容器会先读 <context-param></context-param> 节点, 并创建一个 ServletContext 实例, 以节点的 name 作为键, value 作为值,
存储到上下文环境中。
2> 接着, 容器会去读 <listener></listener> 节点, 根据配置的 class 类路径来创建监听。
3> 接着, 容器去读 <filter></filter> 节点, 根据指定的类路径来实例化过滤器。
以上都是在 WEB 项目还没有完全启动起来的时候就已经完成了的工作。如果系统中有用到 Servlet, 则 Servlet 是在第一次发起请求的时候被实例化的,
且一般不会被容器销毁, 它可以服务于多个用户的请求。所以, Servlet 的初始化都要比上面提到的那几个要迟。
总的来说, web.xml 的加载顺序是: context-param --> listener --> filter --> servlet
其中, 如果 web.xml 中出现了相同的节点, 则是按照在配置文件中出现的先后顺序来加载的。
下面引入一个小列子来说明:
<?xml version="1.0" encoding="UTF-8"?>
<listener>
<listener-class>net.yeah.fancydeepin.listener.AppStartListener</listener-class>
</listener>
<!-- 为了更好的说明, 特意将 context-param 放在 listener 后面 -->
<context-param>
<param-name>technology</param-name>
<param-value>java,javascript,ajax,css,html</param-value>
</context-param>
<filter>
<filter-name>ReDespatcherFilter</filter-name>
<filter-class>net.yeah.fancydeepin.filter.ReDespatcherFilter</filter-class>
<init-param>
<param-name>it</param-name>
<param-value>android, python, c</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>ReDespatcherFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>ReDespatcherFilter2</filter-name>
<filter-class>net.yeah.fancydeepin.filter.ReDespatcherFilter2</filter-class>
<init-param>
<param-name>mail</param-name>
<param-value>fancydeepin@yeah.net</param-value>
</init-param>
</filter>
</web-app>
package net.yeah.fancydeepin.listener;
import java.util.Arrays;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class AppStartListener implements ServletContextListener{
public void contextInitialized(ServletContextEvent contextEvent) {
System.out.println("********************************************");
ServletContext context = contextEvent.getServletContext();
List<String> params = Arrays.asList(context.getInitParameter("technology").split(","));
for(String param : params){
System.out.print(param + "\t");
}
System.out.println("\n********************************************");
}
public void contextDestroyed(ServletContextEvent contextEvent) {
}
}
package net.yeah.fancydeepin.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class ReDespatcherFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("============================================");
System.out.println(filterConfig.getInitParameter("it"));
System.out.println("============================================");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,ServletException{
chain.doFilter(request, response);
}
public void destroy() {
}
}
package net.yeah.fancydeepin.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class ReDespatcherFilter2 implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("++++++++++++++++++++++++++++++++++++++++++++");
System.out.println(filterConfig.getInitParameter("mail"));
System.out.println("++++++++++++++++++++++++++++++++++++++++++++");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,ServletException{
chain.doFilter(request, response);
}
public void destroy() {
}
}
后台启动输出结果:
posted on 2013-03-30 23:30
fancydeepin 阅读(4734)
评论(4) 编辑 收藏