tomcat加载应用程序配置文件顺序

    对于三大框架,例如ssh或ssm,在tomcat容器启动时配置文件加载顺序是?

    针对这样的疑问,我大概知道每个项目的加载都是从web.xml开始的,所以我们以下面的配置文件为例,说明加载过程,我的项目web.xml配置文件如下:


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">

	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>/WEB-INF/classes/log4j.properties</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>

	<!--  户登录安全filter  -->
	
	<filter>
		<filter-name>encodingFilter</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>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

		 
	<!--  添加CAS filter   begin -->
	<!-- 填写退出的URL -->
	<context-param>
		<param-name>casServerLogoutUrl</param-name>
		<param-value>https://cass.cdv.com:8443/cas/logout</param-value>
	</context-param>
	<context-param>
		<param-name>serverName</param-name>
		<param-value>http://192.166.0.117:8080</param-value>
	</context-param>
	<context-param>
		<param-name>casServerLoginUrl</param-name>
		<param-value>https://cass.cdv.com:8443/cas/login</param-value>
	</context-param>
	<listener>
		<listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>
	</listener>

	<!-- CAS sign out -->
	<filter>
		<filter-name>CAS Single Sign Out Filter</filter-name>
		<!--  the class to cometrue Filter Interface -->
		<filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>CAS Single Sign Out Filter</filter-name>
		<!-- the webpage to filter  -->
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!--单点退出配置-->
	<!-- CAS login首次登陆拦截跳转登陆页面 -->
	<filter>
		<filter-name>CAS Filter</filter-name>
		<filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
		<!-- 初始化filter,默认的设置. -->
		<init-param>
			<param-name>casServerLoginUrl</param-name>
			<!-- 统一认证服务器地址 -->
			        <param-value>https://cass.cdv.com:8443/cas/login</param-value> 

		</init-param>
		<init-param>
			<param-name>serverName</param-name>
			<!-- 项目所在的服务器地址和端口号,认证服务器回调的URL-->
			<param-value>http://192.166.0.84:8080</param-value>
		</init-param>
	</filter>

	<!-- 过滤哪些网站 -->
	<filter-mapping>
		<filter-name>CAS Filter</filter-name>
		<!-- 项目中所要拦截的url -->
		<url-pattern>/*</url-pattern> 
	
	</filter-mapping>

	<!-- CAS Validation Filter 再次验证ticket-->
	<filter>
		<filter-name>CAS Validation Filter</filter-name>
		<filter-class>
			org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>
		<!-- 初始化Filter,默认设置. -->
		<init-param>
			<param-name>casServerUrlPrefix</param-name>
			 <param-value>https://cass.cdv.com:8443/cas</param-value> 

		</init-param>
		<init-param>
			<param-name>serverName</param-name>
			<!-- 验证成功转到的地址 -->
			<param-value>http://192.166.0.117:8080</param-value>
		</init-param>
	</filter>
	<!-- 过滤所有的网页 -->
	<filter-mapping>
		<filter-name>CAS Validation Filter</filter-name>
		<!-- 项目中所要拦截的url -->
		 <url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<listener>
		<listener-class>com.cdv.vms.web.listener.SessionListener</listener-class>
	</listener>


	<!--  添加CAS filter  end -->

	<!--  添加security filter 控制菜单的显示和隐藏   begin -->
	<filter>
		<description>login security filter</description>
		<display-name>SecurityFilter</display-name>
		<filter-name>SecurityFilter</filter-name>
		<filter-class>com.cdv.ms.web.filter.SecurityFilter</filter-class>
		<!-- 不需要过滤则直接转到主界面   /user/toMain/-->
		<init-param>
			<param-name>noFilter</param-name>
			<param-value>main.jsp</param-value>
		</init-param>
		<init-param>
			<param-name>noFilter1</param-name>
			<param-value>/publish/showV/</param-value>
		</init-param>
	</filter>
	<!-- 过滤内容 -->
	<filter-mapping>
		<filter-name>SecurityFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!--  添加security filter  end -->


	<servlet>
		<servlet-name>springMVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springMVC.xml,classpath:applicationContext.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springMVC</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	<!-- upload start -->
	<servlet>
		<servlet-name>TokenServlet</servlet-name>
		<servlet-class>com.cdv.upload.servlet.TokenServlet</servlet-class>
		<load-on-startup>0</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>TokenServlet</servlet-name>
		<url-pattern>/tk</url-pattern>
	</servlet-mapping>
	<servlet>
		<servlet-name>StreamServlet</servlet-name>
		<servlet-class>com.cdv.upload.servlet.StreamServlet</servlet-class>
		<load-on-startup>0</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>StreamServlet</servlet-name>
		<url-pattern>/upload</url-pattern>
	</servlet-mapping>
	<servlet>
		<servlet-name>FormDataServlet</servlet-name>
		<servlet-class>com.cdv.upload.servlet.FormDataServlet</servlet-class>
		<load-on-startup>0</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>FormDataServlet</servlet-name>
		<url-pattern>/fd</url-pattern>
	</servlet-mapping>
	<!-- upload end -->
	
	<error-page>
		<error-code>404</error-code>
		<location>/common/404.jsp</location>
	</error-page>
	<error-page>
		<error-code>505</error-code>
		<location>/common/505.jsp</location>
	</error-page>

	<!--   welcome main.jsp -->
	<welcome-file-list>
		<welcome-file>main.jsp</welcome-file>
	</welcome-file-list>
</web-app>


tomcat容器加载


一、加载顺序

   1、对于不同的标签:启动一个web项目的时候,web容器去读取它的配置文件web.xml,读取web.xml文件的时候那应该先读取哪些节点呢?和他们配置的位置没有关系。读取节点的顺序是:listener>filter>servlet。上述配置中还存在一个<context-param>标签,这个标签用来提供应用程序上下文信息,<context-param>可以写在任意位置上,因此加载web.xml标签的顺序为:context-param>listener>filter>servlet

   2、对于同类标签

    filter:在配置filter的时候,web.xml中可以定义多个filter,与filter相关的节点有filter-name、filter-class,filter-mapping必须出现在filter-name标签后,启动容器初始化每个filter的时候,初始化顺序是依照filter-mapping配置节点出现的顺序来调用的doFilter()方法的

    servlet:也filter一样。但当我们设置load-on-startup元素的时候,这个元素的值必须是一个整数。

    如果为负数或者不存在的时候,容器会在该servlet被调用的时候加载这个servlet。

    如果为正整数或0,容器在加载的时候会保证较小的值先被加载。

    如果值相等,容器会自动选择先加载谁。

    由此可知,上述的web.xml系统会如何加载各个项的了。



二、各个标签的说明

web.xml常用元素

    <web-app>

        <display-name></display-name>:定义了web应用的名字。

        <description></description>:声明了web应用描述信息。

        <context-param></context-param>:声明应用范围内初始化参数。

        <filter></filter>:过滤元素,将一个名字与一个实现javax.servlet.Filter接口的类相关联。

        <filter-mapping></filter-mapping>:一旦命名了过滤器,就要利用filter-mapping元素把它与一个或多个servlet或jsp页面关联。

        <listener></listener>:servlet API版本2.3增加了事件监听支持,事件监听程序在建立、修改和删除回话或servlet环境时得到通知。

        <servlet></servlet>:在servlet或jsp页面执行初始化参数或定制URL时,必须首先命名servlet或jsp页面。

        <servlet-mapping></servlet-mapping>:服务器一般为servlet提供一个缺省的url。在更改缺省url时,使用servlet-mapping元素。

        <session-config></session-config>:设置回话超时时间,默认单位分钟。

        <mime-mapping></mime-mapping>:发送到客户端为什么类型。

        <welcome-file-list></welcome-file-list>:指示服务器在收到引用一个目录名而不是文件名的URL时,使用哪个文件。

        <error-page></error-page>:返回特定http状态码时,或者特定类型异常抛出时,能够制定要显示的页面。

        <taglib></taglib>:对标记库描述文件指定别名。


</webk-app>

三、常用元素相关配置

    1、上下文参数配置

<context-param>   
    <param-name>ContextParameter</para-name>   
    <param-value>test</param-value>   
    <description>It is a test parameter.</description>   
  </context-param> 

    2、过滤器配置

<filter>
		<filter-name>encodingFilter</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>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

    3、监听器配置

<listener>   
      <listerner-class>listener.SessionListener</listener-class>   
  </listener> 

    4、servlet配置

	<servlet>
		<servlet-name>springMVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springMVC.xml,classpath:applicationContext.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springMVC</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
    5、回话超时设置

 <session-config>   
      <session-timeout>120</session-timeout>   
   </session-config> 

    6、MIME类型配置

<mime-mapping>   
      <extension>htm</extension>   
      <mime-type>text/html</mime-type>   
   </mime-mapping>  

    7、指定欢迎页配置

 <welcome-file-list>   
      <welcome-file>index.jsp</welcome-file>   
      <welcome-file>index.html</welcome-file>   
      <welcome-file>index.htm</welcome-file>   
   </welcome-file-list>   

    8、配置错误页面配置


<error-page>
		<error-code>404</error-code>
		<location>/common/404.jsp</location>
	</error-page>
	<error-page>
		<error-code>505</error-code>
		<location>/common/505.jsp</location>
	</error-page>










你可能感兴趣的:(tomcat加载应用程序配置文件顺序)