Web.xml配置详解

来源:互联网 发布:windows airplay 推送 编辑:程序博客网 时间:2024/06/06 03:29

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


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

5、过滤器配置:将一个名字与一个实现javaxs.servlet.Filter接口的类相关联。
[html] view plaincopyprint?
  1. <filter>
  2. <filter-name>setCharacterEncoding</filter-name>
  3. <filter-class>com.myTest.setCharacterEncodingFilter</filter-class>
  4. <init-param>
  5. <param-name>encoding</param-name>
  6. <param-value>GB2312</param-value>
  7. </init-param>
  8. </filter>
  9. <filter-mapping>
  10. <filter-name>setCharacterEncoding</filter-name>
  11. <url-pattern>/*</url-pattern>
  12. </filter-mapping>
6、监听器配置
[html] view plaincopyprint?
  1. <listener>
  2. <listerner-class>listener.SessionListener</listener-class>
  3. </listener>
7、Servlet配置
基本配置
[html] view plaincopyprint?
  1. <servlet>
  2. <servlet-name>snoop</servlet-name>
  3. <servlet-class>SnoopServlet</servlet-class>
  4. </servlet>
  5. <servlet-mapping>
  6. <servlet-name>snoop</servlet-name>
  7. <url-pattern>/snoop</url-pattern>
  8. </servlet-mapping>
高级配置
[html] view plaincopyprint?
  1. <servlet>
  2. <servlet-name>snoop</servlet-name>
  3. <servlet-class>SnoopServlet</servlet-class>
  4. <init-param>
  5. <param-name>foo</param-name>
  6. <param-value>bar</param-value>
  7. </init-param>
  8. <run-as>
  9. <description>Security role for anonymous access</description>
  10. <role-name>tomcat</role-name>
  11. </run-as>
  12. </servlet>
  13. <servlet-mapping>
  14. <servlet-name>snoop</servlet-name>
  15. <url-pattern>/snoop</url-pattern>
  16. </servlet-mapping>
元素说明
<servlet></servlet> 用来声明一个servlet的数据,主要有以下子元素:
<servlet-name></servlet-name> 指定servlet的名称
<servlet-class></servlet-class> 指定servlet的类名称
<jsp-file></jsp-file> 指定web站台中的某个JSP网页的完整路径
<init-param></init-param> 用来定义参数,可有多个init-param。在servlet类中通过getInitParamenter(String name)方法访问初始化参数
<load-on-startup></load-on-startup>指定当Web应用启动时,装载Servlet的次序。
当值为正数或零时:Servlet容器先加载数值小的servlet,再依次加载其他数值大的servlet.
当值为负或未定义:Servlet容器将在Web客户首次访问这个servlet时加载它
<servlet-mapping></servlet-mapping> 用来定义servlet所对应的URL,包含两个子元素
<servlet-name></servlet-name> 指定servlet的名称
<url-pattern></url-pattern> 指定servlet所对应的URL
8、会话超时配置(单位为分钟)
[html] view plaincopyprint?
  1. <session-config>
  2. <session-timeout>120</session-timeout>
  3. </session-config>
9、MIME类型配置
[html] view plaincopyprint?
  1. <mime-mapping>
  2. <extension>htm</extension>
  3. <mime-type>text/html</mime-type>
  4. </mime-mapping>
10、指定欢迎文件页配置
[html] view plaincopyprint?
  1. <welcome-file-list>
  2. <welcome-file>index.jsp</welcome-file>
  3. <welcome-file>index.html</welcome-file>
  4. <welcome-file>index.htm</welcome-file>
  5. </welcome-file-list>
11、配置错误页面
11.1、 通过错误码来配置error-page
[html] view plaincopyprint?
  1. <error-page>
  2. <error-code>404</error-code>
  3. <location>/NotFound.jsp</location>
  4. </error-page>
上面配置了当系统发生404错误时,跳转到错误处理页面NotFound.jsp。
11.2、通过异常的类型配置error-page
[html] view plaincopyprint?
  1. <error-page>
  2. <exception-type>java.lang.NullException</exception-type>
  3. <location>/error.jsp</location>
  4. </error-page>
上面配置了当系统发生java.lang.NullException(即空指针异常)时,跳转到错误处理页面error.jsp
12、TLD配置
[html] view plaincopyprint?
  1. <taglib>
  2. <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>
  3. <taglib-location>/WEB-INF/jsp/debug-taglib.tld</taglib-location>
  4. </taglib>
如果MyEclipse一直在报错,应该把<taglib> 放到 <jsp-config>中
[html] view plaincopyprint?
  1. <jsp-config>
  2. <taglib>
  3. <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>
  4. <taglib-location>/WEB-INF/pager-taglib.tld</taglib-location>
  5. </taglib>
  6. </jsp-config>
13、资源管理对象配置
[html] view plaincopyprint?
  1. <resource-env-ref>
  2. <resource-env-ref-name>jms/StockQueue</resource-env-ref-name>
  3. </resource-env-ref>
14、资源工厂配置
[html] view plaincopyprint?
  1. <resource-ref>
  2. <res-ref-name>mail/Session</res-ref-name>
  3. <res-type>javax.mail.Session</res-type>
  4. <res-auth>Container</res-auth>
  5. </resource-ref>
配置数据库连接池就可在此配置:
[html] view plaincopyprint?
  1. <resource-ref>
  2. <description>JNDI JDBC DataSource of shop</description>
  3. <res-ref-name>jdbc/sample_db</res-ref-name>
  4. <res-type>javax.sql.DataSource</res-type>
  5. <res-auth>Container</res-auth>
  6. </resource-ref>
15、安全限制配置
[html] view plaincopyprint?
  1. <security-constraint>
  2. <display-name>Example Security Constraint</display-name>
  3. <web-resource-collection>
  4. <web-resource-name>Protected Area</web-resource-name>
  5. <url-pattern>/jsp/security/protected/*</url-pattern>
  6. <http-method>DELETE</http-method>
  7. <http-method>GET</http-method>
  8. <http-method>POST</http-method>
  9. <http-method>PUT</http-method>
  10. </web-resource-collection>
  11. <auth-constraint>
  12. <role-name>tomcat</role-name>
  13. <role-name>role1</role-name>
  14. </auth-constraint>
  15. </security-constraint>
16、登陆验证配置
[html] view plaincopyprint?
  1. <login-config>
  2. <auth-method>FORM</auth-method>
  3. <realm-name>Example-Based Authentiation Area</realm-name>
  4. <form-login-config>
  5. <form-login-page>/jsp/security/protected/login.jsp</form-login-page>
  6. <form-error-page>/jsp/security/protected/error.jsp</form-error-page>
  7. </form-login-config>
  8. </login-config>
17、安全角色:security-role元素给出安全角色的一个列表,这些角色将出现在servlet元素内的security-role-ref元素的role-name子元素中。
分别地声明角色可使高级IDE处理安全信息更为容易。
[html] view plaincopyprint?
  1. <security-role>
  2. <role-name>tomcat</role-name>
  3. </security-role>
18、Web环境参数:env-entry元素声明Web应用的环境项
[html] view plaincopyprint?
  1. <env-entry>
  2. <env-entry-name>minExemptions</env-entry-name>
  3. <env-entry-value>1</env-entry-value>
  4. <env-entry-type>java.lang.Integer</env-entry-type>
  5. </env-entry>
19、EJB 声明
[html] view plaincopyprint?
  1. <ejb-ref>
  2. <description>Example EJB reference</decription>
  3. <ejb-ref-name>ejb/Account</ejb-ref-name>
  4. <ejb-ref-type>Entity</ejb-ref-type>
  5. <home>com.mycompany.mypackage.AccountHome</home>
  6. <remote>com.mycompany.mypackage.Account</remote>
  7. </ejb-ref>
20、本地EJB声明
[html] view plaincopyprint?
  1. <ejb-local-ref>
  2. <description>Example Loacal EJB reference</decription>
  3. <ejb-ref-name>ejb/ProcessOrder</ejb-ref-name>
  4. <ejb-ref-type>Session</ejb-ref-type>
  5. <local-home>com.mycompany.mypackage.ProcessOrderHome</local-home>
  6. <local>com.mycompany.mypackage.ProcessOrder</local>
  7. </ejb-local-ref>
21、配置DWR
[html] view plaincopyprint?
  1. <servlet>
  2. <servlet-name>dwr-invoker</servlet-name>
  3. <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
  4. </servlet>
  5. <servlet-mapping>
  6. <servlet-name>dwr-invoker</servlet-name>
  7. <url-pattern>/dwr/*</url-pattern>
  8. </servlet-mapping>
22、配置Struts
[html] view plaincopyprint?
  1. <filter>
  2. <filter-name>struts</filter-name>
  3. <filter-class>
  4. org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  5. </filter>
  6. <filter-mapping>
  7. <filter-name>struts</filter-name>
  8. <url-pattern>/*</url-pattern>
  9. </filter-mapping>
23、配置Spring(基本上都是在Struts中配置的)
[html] view plaincopyprint?
  1. <!-- 指定spring配置文件位置 -->
  2. <context-param>
  3. <param-name>contextConfigLocation</param-name>
  4. <param-value>
  5. <!--加载多个spring配置文件 -->
  6. /WEB-INF/applicationContext.xml, /WEB-INF/action-servlet.xml
  7. </param-value>
  8. </context-param>
  9. <!-- 定义SPRING监听器,加载spring -->
  10. <listener>
  11. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  12. </listener>
  13. <listener>
  14. <listener-class>
  15. org.springframework.web.context.request.RequestContextListener
  16. </listener-class>
  17. </listener>