web.xml文件中标签的加载顺序

 web.xml中配置的加载优先级context-param -> listener -> filter -> servlet

1.context-param,它用于向 ServletContext 提供键值对,即应用程序上下文信息。我们的 listener, filter 等在初始化时会用到这些上下文中的信息所以context-param最先加载

2.Listener加载


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

Spring提供ServletContentListener的一个实现类ContextLoaderListener监听器,该类可以作为Listener使用,在启动Tomcat
容器的时候,该类的作用就是自动装载ApplicationContext的配置信息,如果没有设置contextConfigLocation的初始参数则会
使用默认参数WEB-INF路径下的application.xml文件。如果需要自定义读取多个配置文件或者修改默认路径,则可以在web.xml
中设置: 

3. filter加载

4.servlet加载


  
  
  1. <!--前端控制器-->
  2. <servlet>
  3. <!--这里的名字应和前端容器的配置文件的名字一样,后面的“-servlet.xml”编译器自动补齐-->
  4. <servlet-name>dispatcher</servlet-name>
  5. <servlet- class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  6. <load-on-startup>1</load-on-startup>
  7. </servlet>

web.xml配置


  
  
  1. <?xml version= "1.0" encoding= "UTF-8"?>
  2. <web-app xmlns= "http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation= "http://xmlns.jcp.org/xml/ns/javaee
  5. http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  6. version= "3.1"
  7. metadata-complete= "true">
  8. <!--监听器-->
  9. <listener>
  10. <listener- class>org.springframework.web.context.ContextLoaderListener</listener-class>
  11. </listener>
  12. <!--根容器/上下文容器-->
  13. <context-param>
  14. <param-name>contextConfigLocation</param-name>
  15. <param-value>/WEB-INF/applicationContext.xml</param-value>
  16. </context-param>
  17. <!--前端控制器-->
  18. <servlet>
  19. <!--这里的名字应和前端容器的配置文件的名字一样,后面的“-servlet.xml”编译器自动补齐-->
  20. <servlet-name>dispatcher</servlet-name>
  21. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  22. <load-on-startup>1</load-on-startup>
  23. </servlet>
  24. <servlet-mapping>
  25. <servlet-name>dispatcher</servlet-name>
  26. <url-pattern>/</url-pattern>
  27. </servlet-mapping>
  28. </web-app>
                        <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#csdnc-thumbsup"></use>
                        </svg><span class="name">点赞</span>
                        <span class="count"></span>
                        </a></li>
                        <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#icon-csdnc-Collection-G"></use>
                        </svg><span class="name">收藏</span></a></li>
                        <li class="tool-item tool-active is-share"><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#icon-csdnc-fenxiang"></use>
                        </svg>分享</a></li>
                        <!--打赏开始-->
                                                <!--打赏结束-->
                                                <li class="tool-item tool-more">
                            <a>
                            <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                            </a>
                            <ul class="more-box">
                                <li class="item"><a class="article-report">文章举报</a></li>
                            </ul>
                        </li>
                                            </ul>
                </div>
                            </div>
            <div class="person-messagebox">
                <div class="left-message"><a href="https://blog.csdn.net/wl_public">
                    <img src="https://profile.csdnimg.cn/A/B/B/3_wl_public" class="avatar_pic" username="wl_public">
                                            <img src="https://g.csdnimg.cn/static/user-reg-year/2x/2.png" class="user-years">
                                    </a></div>
                <div class="middle-message">
                                        <div class="title"><span class="tit"><a href="https://blog.csdn.net/wl_public" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">wl_public</a></span>
                                            </div>
                    <div class="text"><span>发布了12 篇原创文章</span> · <span>获赞 17</span> · <span>访问量 3万+</span></div>
                </div>
                                <div class="right-message">
                                            <a href="https://im.csdn.net/im/main.html?userName=wl_public" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                        </a>
                                                            <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">关注</a>
                                    </div>
                            </div>
                    </div>
    
发布了5 篇原创文章 · 获赞 0 · 访问量 161

 web.xml中配置的加载优先级context-param -> listener -> filter -> servlet

猜你喜欢

转载自blog.csdn.net/qq_44813090/article/details/104095498