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常用元素:

[html]  view plain copy
  1. <web-app>   
  2. <display-name>display-name>定义了WEB应用的名字   
  3. <description>description> 声明WEB应用的描述信息   
  4.   
  5. <context-param>context-param> context-param元素声明应用范围内的初始化参数。   
  6. <filter>filter> 过滤器元素将一个名字与一个实现javax.servlet.Filter接口的类相关联。   
  7. <filter-mapping>filter-mapping> 一旦命名了一个过滤器,就要利用filter-mapping元素把它与一个或多个servlet或JSP页面相关联。   
  8. <listener>listener>servlet API的版本2.3增加了对事件监听程序的支持,事件监听程序在建立、修改和删除会话或servlet环境时得到通知。   
  9.                      Listener元素指出事件监听程序类。   
  10. <servlet>servlet> 在向servlet或JSP页面制定初始化参数或定制URL时,必须首先命名servlet或JSP页面。Servlet元素就是用来完成此项任务的。   
  11. <servlet-mapping>servlet-mapping> 服务器一般为servlet提供一个缺省的URL:http://host/webAppPrefix/servlet/ServletName。   
  12.               但是,常常会更改这个URL,以便servlet可以访问初始化参数或更容易地处理相对URL。在更改缺省URL时,使用servlet-mapping元素。   
  13.   
  14. <session-config>session-config> 如果某个会话在一定时间内未被访问,服务器可以抛弃它以节省内存。   
  15.           可通过使用HttpSession的setMaxInactiveInterval方法明确设置单个会话对象的超时值,或者可利用session-config元素制定缺省超时值。   
  16.   
  17. <mime-mapping>mime-mapping>如果Web应用具有想到特殊的文件,希望能保证给他们分配特定的MIME类型,则mime-mapping元素提供这种保证。   
  18. <welcome-file-list>welcome-file-list> 指示服务器在收到引用一个目录名而不是文件名的URL时,使用哪个文件。   
  19. <error-page>error-page> 在返回特定HTTP状态代码时,或者特定类型的异常被抛出时,能够制定将要显示的页面。   
  20. <taglib>taglib> 对标记库描述符文件(Tag Libraryu Descriptor file)指定别名。此功能使你能够更改TLD文件的位置,   
  21.                   而不用编辑使用这些文件的JSP页面。   
  22. <resource-env-ref>resource-env-ref>声明与资源相关的一个管理对象。   
  23. <resource-ref>resource-ref> 声明一个资源工厂使用的外部资源。   
  24. <security-constraint>security-constraint> 制定应该保护的URL。它与login-config元素联合使用   
  25. <login-config>login-config> 指定服务器应该怎样给试图访问受保护页面的用户授权。它与sercurity-constraint元素联合使用。   
  26. <security-role>security-role>给出安全角色的一个列表,这些角色将出现在servlet元素内的security-role-ref元素   
  27.                    的role-name子元素中。分别地声明角色可使高级IDE处理安全信息更为容易。   
  28. <env-entry>env-entry>声明Web应用的环境项。   
  29. <ejb-ref>ejb-ref>声明一个EJB的主目录的引用。   
  30. < ejb-local-ref> ejb-local-ref>声明一个EJB的本地主目录的应用。   
  31. web-app>   


相应元素配置 
1、Web应用图标:指出IDE和GUI工具用来表示Web应用的大图标和小图标 
[html]  view plain copy
  1. <icon>   
  2. <small-icon>/images/app_small.gifsmall-icon>   
  3. <large-icon>/images/app_large.giflarge-icon>   
  4. icon>   
2、Web 应用名称:提供GUI工具可能会用来标记这个特定的Web应用的一个名称 
[html]  view plain copy
  1. <display-name>Tomcat Exampledisplay-name>   
3、Web 应用描述: 给出于此相关的说明性文本 
[html]  view plain copy
  1. <disciption>Tomcat Example servlets and JSP pages.disciption>   
4、上下文参数:声明应用范围内的初始化参数。 
[html]  view plain copy
  1. <context-param>   
  2.   <param-name>ContextParameterpara-name>   
  3.   <param-value>testparam-value>   
  4.   <description>It is a test parameter.description>   
  5. context-param>   
  在servlet里面可以通过getServletContext().getInitParameter("context/param")得到 

5、过滤器配置:将一个名字与一个实现javaxs.servlet.Filter接口的类相关联。 
[html]  view plain copy
  1. <filter>   
  2.       <filter-name>setCharacterEncodingfilter-name>   
  3.       <filter-class>com.myTest.setCharacterEncodingFilterfilter-class>   
  4.       <init-param>   
  5.           <param-name>encodingparam-name>   
  6.           <param-value>GB2312param-value>   
  7.       init-param>   
  8. filter>   
  9. <filter-mapping>   
  10.       <filter-name>setCharacterEncodingfilter-name>   
  11.       <url-pattern>/*url-pattern>   
  12. filter-mapping>   
6、监听器配置 
[html]  view plain copy
  1. <listener>   
  2.     <listerner-class>listener.SessionListenerlistener-class>   
  3. listener>   
7、Servlet配置 
   基本配置 
[html]  view plain copy
  1. <servlet>   
  2.    <servlet-name>snoopservlet-name>   
  3.    <servlet-class>SnoopServletservlet-class>   
  4. servlet>   
  5. <servlet-mapping>   
  6.    <servlet-name>snoopservlet-name>   
  7.    <url-pattern>/snoopurl-pattern>   
  8. servlet-mapping>   
   高级配置 
[html]  view plain copy
  1. <servlet>   
  2.    <servlet-name>snoopservlet-name>   
  3.    <servlet-class>SnoopServletservlet-class>   
  4.    <init-param>   
  5.       <param-name>fooparam-name>   
  6.       <param-value>barparam-value>   
  7.    init-param>   
  8.    <run-as>   
  9.       <description>Security role for anonymous accessdescription>   
  10.       <role-name>tomcatrole-name>   
  11.    run-as>   
  12. servlet>   
  13. <servlet-mapping>   
  14.    <servlet-name>snoopservlet-name>   
  15.    <url-pattern>/snoopurl-pattern>   
  16. servlet-mapping>   
   元素说明 
      用来声明一个servlet的数据,主要有以下子元素: 
      指定servlet的名称 
      指定servlet的类名称 
      指定web站台中的某个JSP网页的完整路径 
      用来定义参数,可有多个init-param。在servlet类中通过getInitParamenter(String name)方法访问初始化参数 
     指定当Web应用启动时,装载Servlet的次序。 
                                 当值为正数或零时:Servlet容器先加载数值小的servlet,再依次加载其他数值大的servlet. 
                                 当值为负或未定义:Servlet容器将在Web客户首次访问这个servlet时加载它 
      用来定义servlet所对应的URL,包含两个子元素 
        指定servlet的名称 
        指定servlet所对应的URL 
8、会话超时配置(单位为分钟) 
[html]  view plain copy
  1. <session-config>   
  2.    <session-timeout>120session-timeout>   
  3. session-config>   
9、MIME类型配置 
[html]  view plain copy
  1. <mime-mapping>   
  2.    <extension>htmextension>   
  3.    <mime-type>text/htmlmime-type>   
  4. mime-mapping>   
10、指定欢迎文件页配置 
[html]  view plain copy
  1. <welcome-file-list>   
  2.    <welcome-file>index.jspwelcome-file>   
  3.    <welcome-file>index.htmlwelcome-file>   
  4.    <welcome-file>index.htmwelcome-file>   
  5. welcome-file-list>   
11、配置错误页面 
 11.1、 通过错误码来配置error-page 
[html]  view plain copy
  1. <error-page>   
  2.    <error-code>404error-code>   
  3.    <location>/NotFound.jsplocation>   
  4. error-page>   
  上面配置了当系统发生404错误时,跳转到错误处理页面NotFound.jsp。 
11.2、通过异常的类型配置error-page 
[html]  view plain copy
  1. <error-page>   
  2.     <exception-type>java.lang.NullExceptionexception-type>   
  3.     <location>/error.jsplocation>   
  4. error-page>   
  上面配置了当系统发生java.lang.NullException(即空指针异常)时,跳转到错误处理页面error.jsp 
12、TLD配置 
[html]  view plain copy
  1. <taglib>   
  2.     <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglibtaglib-uri>   
  3.     <taglib-location>/WEB-INF/jsp/debug-taglib.tldtaglib-location>   
  4. taglib>   
   如果MyEclipse一直在报错,应该把 放到 中 
[html]  view plain copy
  1. <jsp-config>   
  2.    <taglib>   
  3.        <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglibtaglib-uri>   
  4.        <taglib-location>/WEB-INF/pager-taglib.tldtaglib-location>   
  5.    taglib>   
  6. jsp-config>   
13、资源管理对象配置 
[html]  view plain copy
  1. <resource-env-ref>   
  2.     <resource-env-ref-name>jms/StockQueueresource-env-ref-name>   
  3. resource-env-ref>   
14、资源工厂配置 
[html]  view plain copy
  1. <resource-ref>   
  2.     <res-ref-name>mail/Sessionres-ref-name>   
  3.     <res-type>javax.mail.Sessionres-type>   
  4.     <res-auth>Containerres-auth>   
  5. resource-ref>   
   配置数据库连接池就可在此配置: 
[html]  view plain copy
  1. <resource-ref>   
  2.     <description>JNDI JDBC DataSource of shopdescription>   
  3.     <res-ref-name>jdbc/sample_dbres-ref-name>   
  4.     <res-type>javax.sql.DataSourceres-type>   
  5.     <res-auth>Containerres-auth>   
  6. resource-ref>   
15、安全限制配置 
[html]  view plain copy
  1. <security-constraint>   
  2.    <display-name>Example Security Constraintdisplay-name>   
  3.    <web-resource-collection>   
  4.       <web-resource-name>Protected Areaweb-resource-name>   
  5.       <url-pattern>/jsp/security/protected/*url-pattern>   
  6.       <http-method>DELETEhttp-method>   
  7.       <http-method>GEThttp-method>   
  8.       <http-method>POSThttp-method>   
  9.       <http-method>PUThttp-method>   
  10.    web-resource-collection>   
  11.    <auth-constraint>   
  12.      <role-name>tomcatrole-name>   
  13.      <role-name>role1role-name>   
  14.    auth-constraint>   
  15. security-constraint>   
16、登陆验证配置 
[html]  view plain copy
  1. <login-config>   
  2.   <auth-method>FORMauth-method>   
  3.   <realm-name>Example-Based Authentiation Arearealm-name>   
  4.   <form-login-config>   
  5.      <form-login-page>/jsp/security/protected/login.jspform-login-page>   
  6.      <form-error-page>/jsp/security/protected/error.jspform-error-page>   
  7.   form-login-config>   
  8. login-config>   
17、安全角色:security-role元素给出安全角色的一个列表,这些角色将出现在servlet元素内的security-role-ref元素的role-name子元素中。 
    分别地声明角色可使高级IDE处理安全信息更为容易。 
[html]  view plain copy
  1. <security-role>   
  2.    <role-name>tomcatrole-name>   
  3. security-role>   
18、Web环境参数:env-entry元素声明Web应用的环境项 
[html]  view plain copy
  1. <env-entry>   
  2.    <env-entry-name>minExemptionsenv-entry-name>   
  3.    <env-entry-value>1env-entry-value>   
  4.    <env-entry-type>java.lang.Integerenv-entry-type>   
  5. env-entry>   
19、EJB 声明 
[html]  view plain copy
  1. <ejb-ref>   
  2.    <description>Example EJB referencedecription>   
  3.    <ejb-ref-name>ejb/Accountejb-ref-name>   
  4.    <ejb-ref-type>Entityejb-ref-type>   
  5.    <home>com.mycompany.mypackage.AccountHomehome>   
  6.    <remote>com.mycompany.mypackage.Accountremote>   
  7. ejb-ref>   
20、本地EJB声明 
[html]  view plain copy
  1. <ejb-local-ref>   
  2.    <description>Example Loacal EJB referencedecription>   
  3.    <ejb-ref-name>ejb/ProcessOrderejb-ref-name>   
  4.    <ejb-ref-type>Sessionejb-ref-type>   
  5.    <local-home>com.mycompany.mypackage.ProcessOrderHomelocal-home>   
  6.    <local>com.mycompany.mypackage.ProcessOrderlocal>   
  7. ejb-local-ref>   
21、配置DWR 
[html]  view plain copy
  1. <servlet>   
  2.     <servlet-name>dwr-invokerservlet-name>   
  3.     <servlet-class>uk.ltd.getahead.dwr.DWRServletservlet-class>   
  4. servlet>   
  5. <servlet-mapping>   
  6.     <servlet-name>dwr-invokerservlet-name>   
  7.     <url-pattern>/dwr/*url-pattern>   
  8. servlet-mapping>   
22、配置Struts 
[html]  view plain copy
  1. <filter>  
  2.     <filter-name>strutsfilter-name>  
  3.     <filter-class>  
  4.         org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>  
  5. filter>  
  6. <filter-mapping>  
  7.     <filter-name>strutsfilter-name>  
  8.     <url-pattern>/*url-pattern>  
  9. filter-mapping>  
23、配置Spring(基本上都是在Struts中配置的) 
[html]  view plain copy
  1.     
  2.  <context-param>   
  3.     <param-name>contextConfigLocationparam-name>   
  4.     <param-value>   
  5.         
  6.       /WEB-INF/applicationContext.xml, /WEB-INF/action-servlet.xml   
  7.     param-value>   
  8.  context-param>   
  9.     
  10. <listener>   
  11.    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>   
  12. listener>   
  13. <listener>   
  14.    <listener-class>   
  15.      org.springframework.web.context.request.RequestContextListener   
  16.    listener-class>   
  17. listener>   

你可能感兴趣的:(web前端,Java,web.xml,myeclipse)