1、概述 html
一、启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点。 java
二、紧接着,容器建立一个ServletContext(servlet上下文),这个web项目的全部部分都将共享这个上下文。 web
三、容器将<context-param>转换为键值对,并交给servletContext。 sql
四、容器建立<listener>中的类实例,建立监听器 数据库
二 、 load-on-startup 浏览器
load-on-startup 元素在web应用启动的时候指定了servlet被加载的顺序,它的值必须是一个整数。若是它的值是一个负整数或是这个元素不存在,那么容器会在该servlet被调用的时候,加载这个servlet 。若是值是正整数或零,容器在配置的时候就加载并初始化这个servlet,容器必须保证值小的先被加载。若是值相等,容器能够自动选择先加载谁。 tomcat
在servlet的配置当中,<load-on-startup>5</load-on-startup>的含义是: 安全
标记容器是否在启动的时候就加载这个servlet。 app
当值为0或者大于0时,表示容器在应用启动时就加载这个servlet; jsp
当是一个负数时或者没有指定时,则指示容器在该servlet被选择时才加载。
正数的值越小,启动该servlet的优先级越高。
三 、加载顺序
首先能够确定的是,加载顺序与它们在 web.xml 文件中的前后顺序无关。即不会由于 filter 写在 listener 的前面而会先加载 filter。最终得出的结论是:ServletContext -> 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 的加载顺序是:ServletContext -> context-param -> listener -> filter -> servlet ,而同个类型之间的实际程序调用的时候的顺序是根据对应的 mapping 的顺序进行调用的。
四 、 web.xml文件详解我将本身知道的web.xml的元素整理了一下:
一、web.xml首先是确定要包含它的schema.
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> </web-app>
其它的元素都放在<web-app></web-app>之中。
<discription></discription> 是对站台的描述
<display-name></display-name> 定义站台的名称
<distributable/> 是指定该站台是否可分布式处理
<context-param></context-param> 用来设定web站台的环境参数,它包含两个子元素:
<param-name></param-name> 用来指定参数的名称
<param-value></param-value> 用来设定参数值
好比:
<context-param> <param-name>my_param</param-name> <param-value>hello</param-value> </context-param>
在此设定的参数,能够在servlet中用 getServletContext().getInitParameter(“my_param”) 来取得
mime-mapping
<mime-mapping></mime-mapping> 定义某一个扩展名和某一个MIME Type作对映,包含两个子元素:
<extension></extension> 扩展名的名称
<mime-type></mime-type> MIME格式
好比:
<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>
welcom-file-list
<welcome-file-list></welcom-file-list> 用来定义首页的列单,包含一个子元素:
<welcome-file></welcome-file> 指定首页的文件名称
好比:
<welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> </welcom-file-list>
error-page
<error-page></error-page> 用来处理错误代码或异常的页面,有三个子元素:
<error-code></error-code> 指定错误代码
<exception-type></exception-type> 指定一个JAVA异常类型
<location></location> 指定在web站台内的相关资源路径
好比:
<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></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></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 of shop</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
<jsp-config> 包括<taglib> 和<jsp-property-group> 两个子元素。
其中<taglib>元素在JSP 1.2时就已经存在;而<jsp-property-group>是JSP 2.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 group for JSP Configuration JSP example.</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> <role rolename=”tomcat”/> <role rolename=”manager”/> <role rolename=”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” security role</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等)
其实这种认证方法实际上有两个步骤:
一、检查提供的用户名和密码是否正确。
二、判断用户是否映射到特定的安全角色。例如,用户可能提供了正确的用户名和密码,但没有映射到特定的安全角色,也将被禁止访问特定的Web资源。