Java web.xml配置解读与加载分析

一 启动

1 、启动一个 WEB 项目的时候, WEB 容器会去读取它的配置文件 web.xml ,读取 <listener> 和 <context-param> 两个结点。  

2 、紧接着,容器会创建一个 ServletContext ( servlet 上下文),这个 web 项目的所有部分都将共享这个上下文。  

3 、容器将 <context-param> 转换为键值对,并交给 servletContext 。  

4 、容器创建 <listener> 中的类实例,创建监听器。

二  Load-on-startup

Load-on-startup 元素在 web 应用启动的时候指定了 servlet 被加载的顺序,它的值必须是一个整数。

如果它的值是一个负整数或是这个元素不存在,容器会在该 servlet 被调用的时候才加载这个 servlet ;如果值是正整数或零,容器在配置的时候就加载并初始化这个 servlet ;容器必须保证值小的先被加载;如果值相等,容器可以自动选择先加载谁。  

在 servlet 的配置当中, <load-on-startup>5</load-on-startup> 的含义是:  

标记容器是否在启动的时候就加载这个 servlet 。  

当值为 0 或者大于 0 时,表示容器在应用启动时就加载这个 servlet ;  

当是一个负数时或者没有指定时,则指示容器在该 servlet 被选择时才加载。  

正数的值越小,启动该 servlet 的优先级越高。  

三   加载顺序

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

ServletContext-> listener ->filter -> servlet

同时还存在着这样一种配置节点: context-param ,它用于向 ServletContext 提供键值对,即应用程序上下文信息。我们的 listener, filter 等在初始化时会用到这些上下文中的信息, 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 的加载顺序是:

ServletContext-> context-param ->listener -> filter -> servlet

而同个类型之间的实际程序调用的时候的顺序是根据对应的 mapping 的顺序进行调用的。

四  web.xml 文件详解  

web.xml 首先是肯定要包含它的 schema.

<web-app xmlns=” http://java.sun.com/xml/ns/j2ee

xmlns:xsi=” http://www.w3.org/2001/XMLSchema-instance

xsi:schemaLocation=” http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd

version=”2.4″>

</web-app>

其它的元素都放在 <web-app></web-app> 之中。

<discription></discription> 是对站点的描述

<display-name></display-name> 定义站点的名称

<distributable/> 指定该站点是否可分布式处理

<context-param></context-param> 用来设定 web 站点的环境参数,它包含两个子元素:

<param-name></param-name> key

<param-value></param-value>

在此设定的参数,可以在 servlet 中用 getServletContext().getInitParameter(“my_param”) 来取得

<filter></filter> 是用来声明 filter 的相关设定,它包含以下子元素:

<filter-name></filter-name>

<filter-class></filter-class>

<init-param></init-param>

<param-name></param-name>

<param-value></param-value>

e.g. :

<filter>

<filter-name>setCharacterEncoding</filter-name>

<filter-class>com.myTest.setCharacterEncodingFilter</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>GB2312</param-value>

</init-param>

</filter>

与 <filter> 同时使用的是 <filter-mapping> ,用来定义 filter 所对应的 URL ,它有两个子元素:

<filter-name></filter-name> 指定 filter 的名字

<url-pattern></url-pattern> 指定 filter 所对应的 URL

e.g. :

<filter-mapping>

<filter-name>setCharacterEncoding</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<listener></listener> 用来设定 Listener 接口,它的主要子元素为

<listener-class></listener-class> 定义 Listener 的类名称

e.g. :

<listener>

<listener-class>com.myTest.ContextListener</listener-class> 

</listener>

<servlet></servlet> 用来声明一个 servlet 的数据,主要有以下子元素:

<servlet-name></servlet-name>

<servlet-class></servlet-class>

<jsp-file></jsp-file> 指定 web 站台中的某个 JSP 网页的完整路径

<init-param></init-param> 用来定义参数,和前面的 <init-param> 差不多

同样, 与 <servlet></servlet> 一起使用的是 <servlet-mapping></servlet-mapping> 用来定义 servlet 所对应的 URL ,包含两个子元素:

<servlet-name></servlet-name>

<url-pattern></url-pattern>

e.g. :

<servlet>

<servlet-name>ShoppingServlet</servlet-name>

<servlet-class>com.myTest.ShoppingServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>ShoppingServlet</servlet-name>

<url-pattern>/shop/ShoppingServlet</url-pattern>

</servlet-mapping>

<session-config></session-config> 用来定义 web 站台中的 session 参数,包含一个子元素:

<session-timeout></session-timeout> 用来定义这个 web 站台所有 session 的有效期限,单位为 min

<mime-mapping></mime-mapping> 定义某一个扩展名和某一个 MIMEType 做对映,包含 :

<extension></extension> 扩展名的名称

<mime-type></mime-type>MIME 格式

e.g. :

<mime-mapping>

<extension>doc</extension>

<mime-type>application/vnd.ms-word</mime-type>

</mime-mapping>

<mime-mapping>

<extension>xls</extension>

<mime-type>application/vnd.ms-excel</mime-type>

</mime-mapping>

<welcome-file-list></welcom-file-list> 用来定义首页的列单,包含一个子元素:

<welcome-file></welcome-file>

e.g. :

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

<welcome-file>index.html</welcome-file>

</welcom-file-list>

<error-page></error-page> 用来处理错误代码或异常的页面,有三个子元素:

<error-code></error-code> 指定错误代码

<exception-type></exception-type> 指定一个 JAVA 异常类型

<location></location> 指定在 web 站台内的相关资源路径

e.g. :

<error-page>

<error-code>404</error-code>

<location>/error404.jsp</location>

</error-page>

<error-page>

<exception-type>java.lang.Exception</exception-type>

<location>/exception.jsp</location>

</error-page>

<taglib></taglib> 用来设定 JSP 网页所用到的 Tag Library 路径,有两个子元素:

<taglib-uri></taglib-uri> 定义 TLD 文件的 URI ,在 JSP 网页中用 taglib 指令便可取得该 URI 的 TLD 文件

<taglib-location></taglib-location> 指定 TLD 文件相对于 web 站台的存放位置

比如:

<taglib>

<taglib-uri>myTaglib</taglib-uri>

<taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>

</taglib>

<resource-ref></resource-ref> 定义利用 JNDI 取得站台可利用的资源,有五个子元素:

<description></description> 资源说明

<rec-ref-name></rec-ref-name> 资源名称

<res-type></res-type> 资源种类

<res-auth></res-auth> 资源经由 Application 或 Container 来许可

<res-sharing-scope></res-sharing-scope> 资源是否可以共享,有 Shareable 和 Unshareable 两个 值,默认为 Shareable

比如,配置数据库连接池就可在此配置:

<resource-ref>

<description>JNDI JDBC DataSource ofshop</description>

<res-ref-name>jdbc/sample_db</res-ref-name>

<res-type>javax.sql.DataSource</res-type>

<res-auth>Container</res-auth>

</resource-ref>

<jsp-config> 包括 <taglib> 和 <jsp-property-group> 两个子元素。

其中 <taglib> 元素在 JSP1.2 时就已经存在;而 <jsp-property-group> 是 JSP2.0 新增的元素。

<jsp-property-group> 元素主要有八个子元素,它们分别为:

1.<description> :设定的说明;

2.<display-name> :设定名称;

3.<url-pattern> :设定值所影响的范围,如: /CH2 或 /*.jsp ;

4.<el-ignored> :若为 true ,表示不支持 EL 语法;

5.<scripting-invalid> :若为 true ,表示不支持

<%scripting

%>

语法;

6.<page-encoding> :设定 JSP 网页的编码;

7.<include-prelude> :设置 JSP 网页的抬头,扩展名为 .jspf ;

8.<include-coda> :设置 JSP 网页的结尾,扩展名为 .jspf 。

一个简单的 <jsp-config> 元素完整配置:

<jsp-config>

<taglib>

<taglib-uri>Taglib</taglib-uri>

<taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>

</taglib>

<jsp-property-group>

<description>Special property groupfor JSP Configuration JSPexample.</description>

<display-name>JSPConfiguration</display-name>

<url-pattern>/jsp/*</url-pattern>

<el-ignored>true</el-ignored>

<page-encoding>GB2312</page-encoding>

<scripting-invalid>true</scripting-invalid>

<include-prelude>/include/prelude.jspf</include-prelude>

<include-coda>/include/coda.jspf</include-coda>

</jsp-property-group>

</jsp-config>

配置 web.xml 来限制对某些 servlet 的请求

有时我们只希望通过认证的用户才能请求某些 servlet 的话,就可以在 web.xml 中来进行相应的配置,来达到此目的。

这就要用到 <security-constraint></security-constraint> 元素。

对于 tomcat ,中 web.xml 使用 security-constraint 元素需要在位于 <Tomcat-installation-directory>/conf/tomcat-users.xml 的 XML 文件中创建用户名和密码。比如下面的这个 tomcat-users.xml 文件:

<?xml version=’1.0′encoding=’utf-8′?>

<tomcat-users>

<rolerolename=”tomcat”/>

<rolerolename=”manager”/>

<rolerolename=”admin”/>

<user

username=”tomcat”password=”tomcat”

roles=”tomcat”/>

<user

username=”both”password=”tomcat”

roles=”tomcat,manager”/>

<user

username=”admin”password=”admin”

roles=”admin”/>

</tomcat-users>

此 XML 片段包括一个 tomcat-users 根元素,它包含一个或多个 role 和 user 元素。

然后在 Web 应用程序的 web.xml 中创建 security-constraint 、 login-config 和 security-role 元素。

<security-constraint>

<web-resource-collection>

<web-resource-name>HelloServlet</web-resource-name>

<url-pattern>/HelloServlet</url-pattern>

<http-method>GET</http-method>

<http-method>POST</http-method>

</web-resource-collection>

<auth-constraint>

<description>This applies only to the”tomcat” securityrole</description>

<role-name>admin</role-name>

</auth-constraint>

<user-data-constraint>

<transport-guarantee>NONE</transport-guarantee>

</user-data-constraint>

</security-constraint>

<login-config>

<auth-method>BASIC</auth-method>

</login-config>

<security-role>

<role-name>admin</role-name>

</security-role>

其中 security-constraint 元素包含一个或多个 web-resource-collection 元素,它是描述 Web 应用程序中的哪些 web 资源受到指定安全限制的保护。 http-method 元素指定安全限制覆盖的 HTTP 方法。上面的例子中,当我们对 /HelloServlet 的 GET 或 POST 请求时将触发配置的安全机制。

auth-constraint 元素用于描述允许访问 Web 组件的安全角色。此例中安全角色的例子有 tomcat 、 manager 、 admin 。而只有当作为 admin 角色的用户才可以访问 HelloServlet 。

Web 应用程序通过 login-config 元素来认证用户,并确认该用户是否为正确的角色。

longin-config 包含的 transport-guarantee 子元素用来指定认证方法, BASIC 是一种常见的 Web 认证方式,浏览器给用户提示一个对话框,要求输入用户名和密码,随后 Tomcat 将给出的用户名和密码与 tomcat-users.xml 中的用户名和密码进行比较,然后使用前面的 security-constraint 配置来确定用户是否可访问受保护的 servlet 。

(除 BASIC 外,还可以是 FORM 、 CLIENT-CERT 、 DIGEST 等)

其实这种认证方法实际上有两个步骤:

1 、检查提供的用户名和密码是否正确。

2 、判断用户是否映射到特定的安全角色。例如,用户可能提供了正确的用户名和密码,但没有映射到特定的安全角色,也将被禁止访问特定的 Web 资源。

五 相应元素配置     

1 、 Web 应用图标:指出 IDE 和 GUI 工具用来表示 Web 应用的大图标和小图标     

<icon>

<small-icon>/images/app_small.gif</small-icon>

<large-icon>/images/app_large.gif</large-icon>

</icon>

2 、 Web 应用名称:提供 GUI 工具可能会用来标记这个特定的 Web 应用的一个名称     

<display-name>TomcatExample</display-name>

3 、 Web 应用描述:给出于此相关的说明性文本     

<disciption>Tomcat Example servletsand JSPpages.</disciption>    

4 、上下文参数:声明应用范围内的初始化参数。     

<context-param>

<param-name>ContextParameter</para-name>

<param-value>test</param-value>

<description>It is a testparameter.</description>    

</context-param>

在 servlet 里面可以通过 getServletContext().getInitParameter(“context/param”) 得到     

5 、过滤器配置:将一个名字与一个实现 javaxs.servlet.Filter 接口的类相关联。     

<filter>

<filter-name>setCharacterEncoding</filter-name>

<filter-class>com.myTest.setCharacterEncodingFilter</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>GB2312</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>setCharacterEncoding</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

6 、监听器配置     

<listener>

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

</listener>

7 、 Servlet 配置     

基本配置     

<servlet>

<servlet-name>snoop</servlet-name>

<servlet-class>SnoopServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>snoop</servlet-name>

<url-pattern>/snoop</url-pattern>

</servlet-mapping>

高级配置     

<servlet>

<servlet-name>snoop</servlet-name>

<servlet-class>SnoopServlet</servlet-class>

<init-param>

<param-name>foo</param-name>

<param-value>bar</param-value>

</init-param>

<run-as>

<description>Security role foranonymousaccess</description>    

<role-name>tomcat</role-name>

</run-as>

</servlet>

<servlet-mapping>

<servlet-name>snoop</servlet-name>

<url-pattern>/snoop</url-pattern>

</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(Stringname) 方法访问初始化参数     

<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 、会话超时配置(单位为分钟)     

<session-config>

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

</session-config>

9 、 MIME 类型配置     

<mime-mapping>

<extension>htm</extension>

<mime-type>text/html</mime-type>

</mime-mapping>

10 、指定欢迎文件页配置     

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

<welcome-file>index.html</welcome-file>

<welcome-file>index.htm</welcome-file>

</welcome-file-list>

11 、配置错误页面     

一、通过错误码来配置 error-page    

<error-page>

<error-code>404</error-code>

<location>/NotFound.jsp</location>

</error-page>

上面配置了当系统发生 404 错误时,跳转到错误处理页面 NotFound.jsp 。     

二、通过异常的类型配置 error-page    

<error-page>

<exception-type>java.lang.NullException</exception-type>

<location>/error.jsp</location>

</error-page>

上面配置了当系统发生 java.lang.NullException (即空指针异常)时,跳转到错误处理页面 error.jsp    

12 、 TLD 配置     

<taglib>

<taglib-uri> http://jakarta.apache.org/tomcat/debug-taglib </taglib-uri>    

<taglib-location>/WEB-INF/jsp/debug-taglib.tld</taglib-location>

</taglib>

如果 MyEclipse 一直在报错 , 应该把 <taglib> 放到 <jsp-config> 中     

<jsp-config>

<taglib>

<taglib-uri> http://jakarta.apache.org/tomcat/debug-taglib </taglib-uri>    

<taglib-location>/WEB-INF/pager-taglib.tld</taglib-location>

</taglib>

</jsp-config>

13 、资源管理对象配置     

<resource-env-ref>

<resource-env-ref-name>jms/StockQueue</resource-env-ref-name>

</resource-env-ref>

14 、资源工厂配置     

<resource-ref>

<res-ref-name>mail/Session</res-ref-name>

<res-type>javax.mail.Session</res-type>

<res-auth>Container</res-auth>

</resource-ref>

配置数据库连接池就可在此配置:     

<resource-ref>

<description>JNDI JDBC DataSource ofshop</description>    

<res-ref-name>jdbc/sample_db</res-ref-name>

<res-type>javax.sql.DataSource</res-type>

<res-auth>Container</res-auth>

</resource-ref>

15 、安全限制配置     

<security-constraint>

<display-name>Example SecurityConstraint</display-name>    

<web-resource-collection>

<web-resource-name>ProtectedArea</web-resource-name>

<url-pattern>/jsp/security/protected/*</url-pattern>

<http-method>DELETE</http-method>

<http-method>GET</http-method>

<http-method>POST</http-method>

<http-method>PUT</http-method>

</web-resource-collection>

<auth-constraint>

<role-name>tomcat</role-name>

<role-name>role1</role-name>

</auth-constraint>

</security-constraint>

16 、登陆验证配置     

<login-config>

<auth-method>FORM</auth-method>

<realm-name>Example-BasedAuthentiationArea</realm-name>

<form-login-config>

<form-login-page>/jsp/security/protected/login.jsp</form-login-page>

<form-error-page>/jsp/security/protected/error.jsp</form-error-page>

</form-login-config>

</login-config>

17 、安全角色: security-role 元素给出安全角色的一个列表,这些角色将出现在 servlet 元素内的 security-role-ref 元素的 role-name 子元素中。     

分别地声明角色可使高级 IDE 处理安全信息更为容易。     

<security-role>

<role-name>tomcat</role-name>

</security-role>

18 、 Web 环境参数: env-entry 元素声明 Web 应用的环境项     

<env-entry>

<env-entry-name>minExemptions</env-entry-name>

<env-entry-value>1</env-entry-value>

<env-entry-type>java.lang.Integer</env-entry-type>

</env-entry>

19 、 EJB 声明     

<ejb-ref>

<description>Example EJBreference</decription>    

<ejb-ref-name>ejb/Account</ejb-ref-name>

<ejb-ref-type>Entity</ejb-ref-type>

<home>com.mycompany.mypackage.AccountHome</home>

<remote>com.mycompany.mypackage.Account</remote>

</ejb-ref>

20 、本地 EJB 声明     

<ejb-local-ref>

<description>Example Loacal EJBreference</decription>    

<ejb-ref-name>ejb/ProcessOrder</ejb-ref-name>

<ejb-ref-type>Session</ejb-ref-type>

<local-home>com.mycompany.mypackage.ProcessOrderHome</local-home>

<local>com.mycompany.mypackage.ProcessOrder</local>

</ejb-local-ref>

21 、配置 DWR    

<servlet>

<servlet-name>dwr-invoker</servlet-name>

<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>dwr-invoker</servlet-name>

<url-pattern>/dwr/*</url-pattern>

</servlet-mapping>

22 、配置 Struts    

<display-name>Struts BlankApplication</display-name>    

<servlet>

<servlet-name>action</servlet-name>

<servlet-class>

org.apache.struts.action.ActionServlet

</servlet-class>

<init-param>

<param-name>detail</param-name>

<param-value>2</param-value>

</init-param>

<init-param>

<param-name>debug</param-name>

<param-value>2</param-value>

</init-param>

<init-param>

<param-name>config</param-name>

<param-value>/WEB-INF/struts-config.xml</param-value>

</init-param>

<init-param>

<param-name>application</param-name>

<param-value>ApplicationResources</param-value>

</init-param>

<load-on-startup>2</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>action</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

<!– Struts Tag Library Descriptors–>    

<taglib>

<taglib-uri>struts-bean</taglib-uri>

<taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>

</taglib>

<taglib>

<taglib-uri>struts-html</taglib-uri>

<taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>

</taglib>

<taglib>

<taglib-uri>struts-nested</taglib-uri>

<taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location>

</taglib>

<taglib>

<taglib-uri>struts-logic</taglib-uri>

<taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>

</taglib>

<taglib>

<taglib-uri>struts-tiles</taglib-uri>

<taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location>

</taglib>

23 、配置 Spring (基本上都是在 Struts 中配置的)     

<!– 指定 spring 配置文件位置 –>    

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>

<!– 加载多个 spring 配置文件 –>    

/WEB-INF/applicationContext.xml,/WEB-INF/action-servlet.xml

</param-value>

</context-param>

<!– 定义 SPRING 监听器,加载 spring–>    

<listener>

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

</listener>

<listener>

<listener-class>

org.springframework.web.context.request.RequestContextListener

</listener-class>

</listener>

其他说明:

web.xml 文件中一般包括 servlet, spring, filter,listenr 的配置。那么他们是按照一个什么顺序加载呢?

加载顺序会影响对

spring

bean

的调用。

比如 filter 需要用到 bean ,但是加载顺序是 先加载 filter 后加载 spring ,则 filter 中初始化操作中的 bean 为 null ;

首先可以肯定 加载顺序与他们在 web.xml 文件中的先后顺序无关。

web.xml 中 listener 和 serverlet 的加载顺序为 先 listener 后 serverlet

最终得出结果:先 listener >> filter>> servlet>>  spring

所以,如果过滤器中要使用到 bean ,可以将 spring 的加载 改成 Listener 的方式

<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值