徐培森的Blog 未分类 web.xml的加载过程

web.xml的加载过程

WEB容器(JBoss、Tomcat等)首先去项目的配置文件web.xml中读取和节点 WEB容器创建一个Se…

WEB容器(JBoss、Tomcat等)首先去项目的配置文件web.xml中读取和节点

WEB容器创建一个ServletContext(servlet上下文application),这个web项目的所有部分都将共享这个上下文。

WEB容器将web.xml中的的作为键、作为值,转换为键值对,并存入ServletContext

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

WEB容器读取并创建web.xml中的中的类路径,创建监听器(Listener),启动Web应用时,系统将调用Listener的contextInitialized(ServletContextEvent args)初始化方法。

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

在监听器中可以通过application.getInitParameterr("<param-name>");获取<param-value>
WEB容器读取并创建web.xml中的中的类路径,创建过滤器(Filter)。

<filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

以上都是在WEB项目还没有完全启动起来的时候就已经完成了的工作。如果系统中有Servlet,则Servlet是在第一次发起请求的时候被实例化的,而且一般不会被容器销毁,它可以服务于多个用户的请求。所以,Servlet的初始化都要比上面提到的那几个要迟。

对于某类元素而言,与它们出现的顺序是有关的。以为例,web.xml中当然可以定义多个,与相关的一个元素是,注意:对于拥有相同的和元素而言,必须出现在之后,否则当解析到时,它所对应的还未定义。web容器启动初始化每个时,按照出现的顺序来初始化的,当请求资源匹配多个时,拦截资源是按照元素出现的顺序来依次调用doFilter()方法的。同类似,此处不再赘述。


<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <!--1. ContextLoaderListener监听器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

本文来自网络,不代表徐培森的Blog立场,转载请注明出处:https://blog.xupeisen.com/archives/674

作者: 培森

联系我们

联系我们

13262951234

在线咨询: QQ交谈

邮箱: admin@xupeisen.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部