web.xml资料详解

web.xml文件详解

web.xml文件详解

在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似的,所以自己查找资料总结了下,下面有些是转载其他人的,毕竟人家写的不错,自己 也就不重复造*了,只是略加点了自己的修饰。

        首先可以肯定的是,加载顺序与它们在 web.xml 文件中的先后顺序无关。即不会因为 filter 写在 listener 的前面而会先加载 filter。最终得出的结论是:listener -> filter -> servlet

        同时还存在着这样一种配置节:context-param,它用于向 ServletContext 提供键值对,即应用程序上下文信息。我们的 listener, filter 等在初始化时会用到这些上下文中的信息,那么 context-param 配置节是不是应该写在 listener 配置节前呢?实际上 context-param 配置节可写在任意位置,因此真 正的加载顺序为:context-param -> listener -> filter -> servlet

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

        servlet 同 filter 类似 ,此处不再赘述。

       由此,可以看出,web.xml 的加载顺序是:context-param -> listener -> filter -> servlet ,而同个类型之间的实际程序调用的时候的顺序是根据对应的 mapping 的顺序进行调用的。

 

web.xml文件详解

Xml代码 web.xml资料详解
  1. Web.xml常用元素    
  2. <web-app>     
  3. <display-name></display-name> 定义了WEB应用的名字    
  4. <description></description>  声明WEB应用的描述信息    
  5.   
  6. <context-param></context-param>  context-param元素声明应用范围内的初始 化参数。    
  7. <filter></filter>  过滤器元素将一个名字与一个实现 javax.servlet.Filter接口的类相关联。    
  8. <filter-mapping></filter-mapping>  一旦命名了一个过滤器,就要利用filter- mapping元素把它与一个或多个servlet或JSP页面相关联。    
  9. <listener></listener> servlet API的版本2.3增加了对事件监听程序 的支持,事件监听程序在建立、修改和删除会话或servlet环境时得到通知。    
  10.                      Listener元素指出事件监听程序类。    
  11. <servlet></servlet>  在向servlet或JSP页面制定初始化参数或定制 URL时,必须首先命名servlet或JSP页面。Servlet元素就是用来完成此项任务的。    
  12. <servlet-mapping></servlet-mapping>  服务器一般为servlet提供一个缺省的 URL:http://host/webAppPrefix/servlet/ServletName。    
  13.               但是,常常会更改这个URL,以便servlet可以访问初始化参数或更容易地处理相对URL。在更改 缺省URL时,使用servlet-mapping元素。    
  14.   
  15. <session-config></session-config>  如果某个会话在一定时间内未被访问,服务器可以抛弃它 以节省内存。    
  16.           可通过使用HttpSession的setMaxInactiveInterval方法明确设置单个会话对象的 超时值,或者可利用session-config元素制定缺省超时值。    
  17.   
  18. <mime-mapping></mime-mapping> 如果Web应用具有想到特殊的文件,希望能保证给他们分配 特定的MIME类型,则mime-mapping元素提供这种保证。    
  19. <welcome-file-list></welcome-file-list>  指示服务器在收到引用一个目录名而不是文件名的URL 时,使用哪个文件。    
  20. <error-page></error-page>  在返回特定HTTP状态代码时,或者特定类型的异常被抛 出时,能够制定将要显示的页面。    
  21. <taglib></taglib>  对标记库描述符文件 (Tag Libraryu Descriptor file)指定别名。此功能使你能够更改TLD文件的位置,    
  22.                   而不用编辑使用这些文件的JSP页面。    
  23. <resource-env-ref></resource-env-ref> 声明与资源相关的一个管理对象。    
  24. <resource-ref></resource-ref>  声明一个资源工厂使用的外部资源。    
  25. <security-constraint></security-constraint>  制定应该保护的URL。它与login-config 元素联合使用    
  26. <login-config></login-config>  指定服务器应该怎样给试图访问受保护页面的用户授权。它 与sercurity-constraint元素联合使用。    
  27. <security-role></security-role> 给出安全角色的一个列表,这些角色将出现在 servlet元素内的security-role-ref元素    
  28.                    的role-name子元素中。分别地声明角色可使高级IDE处理安全信息更为容易。    
  29. <env-entry></env-entry> 声明Web应用的环境项。    
  30. <ejb-ref></ejb-ref> 声明一个EJB的主目录的引用。    
  31. <  ejb-local-ref></  ejb-local-ref> 声明一个EJB的本地主目录的应用。    
  32. </web-app>     
  33.   
  34.   
  35. 相应元素配置    
  36.   
  37. 1、Web应用图标:指出IDE和GUI工具用来表示Web应用的大图标和小图标    
  38. <icon>     
  39. <small-icon> /images/app_small.gif</small-icon>     
  40. <large-icon> /images/app_large.gif</large-icon>     
  41. </icon>     
  42. 2、Web 应用名称:提供GUI工具可能会用来标记这个特定的Web应用的一个名称    
  43. <display-name> Tomcat Example</display-name>     
  44. 3、Web 应用描述: 给出于此相关的说明性文本    
  45. <disciption> Tomcat Example servlets and JSP pages.</disciption>     
  46. 4、上下文参数:声明应用范围内的初始化参数。    
  47.   <context-param>     
  48.     <param-name> ContextParameter</para-name>     
  49.     <param-value> test</param-value>     
  50.     <description> It is a test parameter.</description>     
  51.   </context-param>     
  52.   在servlet里面可以通过 getServletContext().getInitParameter("context/param")得到    
  53.   
  54. 5、过滤器配置:将一个名字与一个实现javaxs.servlet.Filter接口的类相关联。    
  55.   <filter>     
  56.         <filter-name> setCharacterEncoding</filter-name>     
  57.         <filter-class> com.myTest.setCharacterEncodingFilter</filter-class>     
  58.         <init-param>     
  59.             <param-name> encoding</param-name>     
  60.             <param-value> GB2312</param-value>     
  61.         </init-param>     
  62.   </filter>     
  63.   <filter-mapping>     
  64.         <filter-name> setCharacterEncoding</filter-name>     
  65.         <url-pattern> /*</url-pattern>     
  66.   </filter-mapping>     
  67. 6、监听器配置    
  68.   <listener>     
  69.       <listerner-class> listener.SessionListener</listener-class>     
  70.   </listener>     
  71. 7、Servlet配置    
  72.    基本配置    
  73.    <servlet>     
  74.       <servlet-name> snoop</servlet-name>     
  75.       <servlet-class> SnoopServlet</servlet-class>     
  76.    </servlet>     
  77.    <servlet-mapping>     
  78.       <servlet-name> snoop</servlet-name>     
  79.       <url-pattern> /snoop</url-pattern>     
  80.    </servlet-mapping>     
  81.    高级配置    
  82.    <servlet>     
  83.       <servlet-name> snoop</servlet-name>     
  84.       <servlet-class> SnoopServlet</servlet-class>     
  85.       <init-param>     
  86.          <param-name> foo</param-name>     
  87.          <param-value> bar</param-value>     
  88.       </init-param>     
  89.       <run-as>     
  90.          <description> Security role for anonymous access</description>     
  91.          <role-name> tomcat</role-name>     
  92.       </run-as>     
  93.    </servlet>     
  94.    <servlet-mapping>     
  95.       <servlet-name> snoop</servlet-name>     
  96.       <url-pattern> /snoop</url-pattern>     
  97.    </servlet-mapping>     
  98.    元素说明    
  99.      <servlet></servlet>  用 来声明一个servlet的数据,主要有以下子元素:    
  100.      <servlet-name></servlet-name>  指 定servlet的名称    
  101.      <servlet-class></servlet-class>  指 定servlet的类名称    
  102.      <jsp-file></jsp-file>  指 定web站台中的某个JSP网页的完整路径    
  103.      <init-param></init-param>  用 来定义参数,可有多个init-param。在servlet类中通过getInitParamenter(String name)方法访问初始化参 数    
  104.      <load-on-startup></load-on-startup> 指 定当Web应用启动时,装载Servlet的次序。    
  105.                                  当值为正数或零时:Servlet容器先加 载数值小的servlet,再依次加载其他数值大的servlet.    
  106.                                  当值为负或未定义:Servlet容器将在 Web客户首次访问这个servlet时加载它     
  107.      <servlet-mapping></servlet-mapping>  用 来定义servlet所对应的URL,包含两个子元素    
  108.        <servlet-name></servlet-name>  指 定servlet的名称    
  109.        <url-pattern></url-pattern>  指 定servlet所对应的URL    
  110. 8、会话超时配置(单位为分钟)    
  111.    <session-config>     
  112.       <session-timeout> 120</session-timeout>     
  113.    </session-config>     
  114. 9、MIME类型配置    
  115.    <mime-mapping>     
  116.       <extension> htm</extension>     
  117.       <mime-type> text/html</mime-type>     
  118.    </mime-mapping>     
  119. 10、指定欢迎文件页配置    
  120.    <welcome-file-list>     
  121.       <welcome-file> index.jsp</welcome-file>     
  122.       <welcome-file> index.html</welcome-file>     
  123.       <welcome-file> index.htm</welcome-file>     
  124.    </welcome-file-list>     
  125. 11、配置错误页面    
  126.   一、 通过错误码来配置error-page    
  127.    <error-page>     
  128.       <error-code> 404</error-code>     
  129.       <location> /NotFound.jsp</location>     
  130.    </error-page>     
  131.   上面配置了当系统发生404错误时,跳转到错误处理页面NotFound.jsp。    
  132. 二、通过异常的类型配置error-page    
  133.    <error-page>     
  134.        <exception-type> java.lang.NullException</exception-type>     
  135.        <location> /error.jsp</location>     
  136.    </error-page>     
  137.   上面配置了当系统发生java.lang.NullException(即空指针异常)时,跳转到错误处理页面 error.jsp    
  138. 12、TLD配置    
  139.    <taglib>     
  140.        <taglib-uri> http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>     
  141.        <taglib-location> /WEB-INF/jsp/debug-taglib.tld</taglib-location>     
  142.    </taglib>     
  143.    如果MyEclipse一直在报错,应该把<taglib>  放到 <jsp-config> 中    
  144.    <jsp-config>     
  145.       <taglib>     
  146.           <taglib-uri> http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>     
  147.           <taglib-location> /WEB-INF/pager-taglib.tld</taglib-location>     
  148.       </taglib>     
  149.    </jsp-config>     
  150. 13、资源管理对象配置    
  151.    <resource-env-ref>     
  152.        <resource-env-ref-name> jms/StockQueue</resource-env-ref-name>     
  153.    </resource-env-ref>     
  154. 14、资源工厂配置    
  155.    <resource-ref>     
  156.        <res-ref-name> mail/Session</res-ref-name>     
  157.        <res-type> javax.mail.Session</res-type>     
  158.        <res-auth> Container</res-auth>     
  159.    </resource-ref>     
  160.    配置数据库连接池就可在此配置:    
  161.    <resource-ref>     
  162.        <description> JNDI JDBC DataSource of shop</description>     
  163.        <res-ref-name> jdbc/sample_db</res-ref-name>     
  164.        <res-type> javax.sql.DataSource</res-type>     
  165.        <res-auth> Container</res-auth>     
  166.    </resource-ref>     
  167. 15、安全限制配置    
  168.    <security-constraint>     
  169.       <display-name> Example Security Constraint</display-name>     
  170.       <web-resource-collection>     
  171.          <web-resource-name> Protected Area</web-resource-name>     
  172.          <url-pattern> /jsp/security/protected/*</url-pattern>     
  173.          <http-method> DELETE</http-method>     
  174.          <http-method> GET</http-method>     
  175.          <http-method> POST</http-method>     
  176.          <http-method> PUT</http-method>     
  177.       </web-resource-collection>     
  178.       <auth-constraint>     
  179.         <role-name> tomcat</role-name>     
  180.         <role-name> role1</role-name>     
  181.       </auth-constraint>     
  182.    </security-constraint>     
  183. 16、登陆验证配置    
  184.    <login-config>     
  185.      <auth-method> FORM</auth-method>     
  186.      <realm-name> Example-Based Authentiation Area</realm-name>     
  187.      <form-login-config>     
  188.         <form-login-page> /jsp/security/protected/login.jsp</form-login-page>     
  189.         <form-error-page> /jsp/security/protected/error.jsp</form-error-page>     
  190.      </form-login-config>     
  191.    </login-config>     
  192. 17、安全角色:security-role元素给出安全角色的一个列表,这些角色将出现在servlet元素内的 security-role-ref元素的role-name子元素中。    
  193.     分别地声明角色可使高级IDE处理安全信息更为容易。    
  194.   <security-role>     
  195.      <role-name> tomcat</role-name>     
  196.   </security-role>     
  197. 18、Web环境参数:env-entry元素声明Web应用的环境项    
  198.   <env-entry>     
  199.      <env-entry-name> minExemptions</env-entry-name>     
  200.      <env-entry-value> 1</env-entry-value>     
  201.      <env-entry-type> java.lang.Integer</env-entry-type>     
  202.   </env-entry>     
  203. 19、EJB 声明    
  204.   <ejb-ref>     
  205.      <description> Example EJB reference</decription>     
  206.      <ejb-ref-name> ejb/Account</ejb-ref-name>     
  207.      <ejb-ref-type> Entity</ejb-ref-type>     
  208.      <home> com.mycompany.mypackage.AccountHome</home>     
  209.      <remote> com.mycompany.mypackage.Account</remote>     
  210.   </ejb-ref>     
  211. 20、本地EJB声明    
  212.   <ejb-local-ref>     
  213.      <description> Example Loacal EJB reference</decription>     
  214.      <ejb-ref-name> ejb/ProcessOrder</ejb-ref-name>     
  215.      <ejb-ref-type> Session</ejb-ref-type>     
  216.      <local-home> com.mycompany.mypackage.ProcessOrderHome</local-home>     
  217.      <local> com.mycompany.mypackage.ProcessOrder</local>     
  218.   </ejb-local-ref>     
  219. 21、配置DWR    
  220.   <servlet>     
  221.       <servlet-name> dwr-invoker</servlet-name>     
  222.       <servlet-class> uk.ltd.getahead.dwr.DWRServlet</servlet-class>     
  223.   </servlet>     
  224.   <servlet-mapping>     
  225.       <servlet-name> dwr-invoker</servlet-name>     
  226.       <url-pattern> /dwr/*</url-pattern>     
  227.   </servlet-mapping>     
  1. 22、配置Struts    
  2.     <display-name> Struts Blank Application</display-name>     
  3.     <servlet>     
  4.         <servlet-name> action</servlet-name>     
  5.         <servlet-class>     
  6.             org.apache.struts.action.ActionServlet    
  7.         </servlet-class>     
  8.         <init-param>     
  9.             <param-name> detail</param-name>     
  10.             <param-value> 2</param-value>     
  11.         </init-param>     
  12.         <init-param>     
  13.             <param-name> debug</param-name>     
  14.             <param-value> 2</param-value>     
  15.         </init-param>     
  16.         <init-param>     
  17.             <param-name> config</param-name>     
  18.             <param-value> /WEB-INF/struts-config.xml</param-value>     
  19.         </init-param>     
  20.         <init-param>     
  21.             <param-name> application</param-name>     
  22.             <param-value> ApplicationResources</param-value>     
  23.         </init-param>     
  24.         <load-on-startup> 2</load-on-startup>     
  25.     </servlet>     
  26.     <servlet-mapping>     
  27.         <servlet-name> action</servlet-name>     
  28.         <url-pattern> *.do</url-pattern>     
  29.     </servlet-mapping>     
  30.     <welcome-file-list>     
  31.         <welcome-file> index.jsp</welcome-file>     
  32.     </welcome-file-list>     
  33.   
  34.     <!-- Struts Tag Library Descriptors -->     
  35.     <taglib>     
  36.         <taglib-uri> struts-bean</taglib-uri>     
  37.         <taglib-location> /WEB-INF/tld/struts-bean.tld</taglib-location>     
  38.     </taglib>     
  39.     <taglib>     
  40.         <taglib-uri> struts-html</taglib-uri>     
  41.         <taglib-location> /WEB-INF/tld/struts-html.tld</taglib-location>     
  42.     </taglib>     
  43.     <taglib>     
  44.     <taglib-uri> struts-nested</taglib-uri>     
  45.     <taglib-location> /WEB-INF/tld/struts-nested.tld</taglib-location>     
  46.     </taglib>     
  47.     <taglib>     
  48.         <taglib-uri> struts-logic</taglib-uri>     
  49.         <taglib-location> /WEB-INF/tld/struts-logic.tld</taglib-location>     
  50.     </taglib>     
  51.     <taglib>     
  52.         <taglib-uri> struts-tiles</taglib-uri>     
  53.         <taglib-location> /WEB-INF/tld/struts-tiles.tld</taglib-location>     
  54.     </taglib>     
  55. 23、配置Spring(基本上都是在Struts中配置的)    
  56.   
  57.    <!-- 指定spring配置文件位置 -->     
  58.    <context-param>     
  59.       <param-name> contextConfigLocation</param-name>     
  60.       <param-value>     
  61.        <!--加载多个spring配置文件 -->     
  62.         /WEB-INF/applicationContext.xml, /WEB-INF/action-servlet.xml    
  63.       </param-value>     
  64.    </context-param>     
  65.   
  66.    <!-- 定义SPRING监听器,加载spring -->     
  67.   
  68.   <listener>     
  69.      <listener-class> org.springframework.web.context.ContextLoaderListener</listener-class>     
  70.   </listener>     
  71.   
  72.   <listener>     
  73.      <listener-class>     
  74.        org.springframework.web.context.request.RequestContextListener    
  75.      </listener-class>     
  76.   </listener>