zoukankan      html  css  js  c++  java
  • 九、web.xml理解

    1、web.xml文件在每个web工程不是必须要有的:
         web.xml文件是用来初始化配置信息:比如Welcome页面、servlet、servlet-mapping、filter、listener、启动加载级别等。当web工程没用到这些时,你可以不用web.xml文件来配置你的Application。

    2、web.xml定义的内容及加载的顺序
    2.1web.xml定义的内容

      .站台的名称和说明  即:定义了WEB应用的名字
      .针对环境参数(Context)做初始化工作
      .Servlet的名称和映射
      .Session的设定
      .Tag library的对映
      .JSP网页设定
      .Mime Type处理
      .错误处理
      .利用JDNI取得站台资源
    备注:每个xml文件都有定义它书写规则的Schema文件,也就是说javaEE的定义web.xml所对应的xml Schema文件中定义了多少种标签元素,web.xml中就可以出现它所定义的标签元素,也就具备哪些特定的功能。web.xml的模式文件是由Sun 公司定义的,每个web.xml文件的根元素为<web-app>中,必须标明这个web.xml使用的是哪个模式文件。如:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    </web-app>
        web.xml的模式文件中定义的标签并不是定死的,模式文件也是可以改变的,一般来说,随着web.mxl模式文件的版本升级,里面定义的功能会越来越复杂,标签元素的种类肯定也会越来越多,但有些不是很常用的,我们只需记住一些常用的并知道怎么配置就可以了。

    2.1web.xml中内容的加载顺序
    容器启动时,web项目中文件的加载顺序:
    1)启动一个WEB项目,WEB容器会先去读取它的配置文件web.xml,读取<context-param>和<listener>两个节点
    2)接着,容器创建一个ServletContext(servlet上下文),这个web项目的所有部分都将共享这个上下文
    3)容器将<context-param>转换为键值对,并交给servletContext。
    4)容器创建<listener>中的类实例,创建监听器。

    补充说明:
    1)加载顺序与它们在 web.xml 文件中的先后顺序无关。即不会因为 filter 写在 listener 的前面而会先加载 filter。最终得出的结论是:listener –> filter –> servlet
    2)同时还存在着这样一种配置节:context-param,它用于向 ServletContext 提供键值对,即应用程序上下文信息。我们的 listener, filter 等在初始化时会用到这些上下文中的信息,那么 context-param 配置节是不是应该写在 listener 配置节前呢?实际上 context-param 配置节可写在任意位置,因此真正的加载顺序为:context-param –> listener –> filter –> servlet
    3)对于某类配置节而言,与它们出现的顺序是有关的。以 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 的顺序进行调用的。

    3、web.xml文件中相应元素配置理解
    3.1)welcome-file-list
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>index1.jsp</welcome-file>
      </welcome-file-list>
    PS:指定了2个欢迎页面,显示时按顺序从第一个找起,如果第一个存在,就显示第一个,后面的不起作用。如果第一个不存在,就找第二个,以此类推。
    3.2)servlet命名和URL定制
    1)为Servlet命名:
    <servlet>
        <servlet-name>servlet1</servlet-name>
        <servlet-class>org.whatisjava.TestServlet</servlet-class>
    </servlet>

    2)为Servlet定制URL
    <servlet-mapping>
        <servlet-name>servlet1</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    3.3)定制初始化参数:可以定制servlet、JSP、Context的初始化参数,然后可以再servlet、JSP、Context中获取这些参数值。
    下面用servlet来举例:
    <servlet>
        <servlet-name>testServlet</servlet-name>
        <servlet-class>org.whatisjava.TestServlet</servlet-class>
        <init-param>
              <param-name>userName</param-name>
              <param-value>Daniel</param-value>
              <load-on-startup>10</load-on-startup>
        </init-param>
    </servlet>
    load-on-startup 标记容器是否在启动的时候就加载这个servlet。当值为0或者大于0时,表示容器在应用启动时就加载这个servlet;当是一个负数时或者没有指定时,则指示容器在该servlet被选择时才加载。正数的值越小,启动该servlet的优先级越高
    经过上面的配置,在servlet中能够调用getServletConfig().getInitParameter("userName")获得参数名对应的值。
    3.4)指定错误处理页面,error-page元素包含三个子元素error-code,exception-type和location.将错误代码(Error Code)或异常(Exception)的种类对应
    到web站台资源路径.
    <error-page>
         <error-code>错误代码</error-code> HTTP Error code,例如: 404
         <exception-type>Exception</exception-type>一个完整名称的Java异常类型
         <location>/路径</location>在web站台内的相关资源路径
    </error-page>
    3.5)设置过滤器:将一个名字与一个实现javaxs.servlet.Filter接口的类相关联    比如设置一个编码过滤器,过滤所有资源  

    <filter>
        <filter-name>EncodingFilter</filter-name>
        <filter-class>
            org.springframework.web.filter.CharacterEncodingFilter
        </filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
            <!--  <param-value>GBK</param-value>-->
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>EncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    View Code

    3.6)设置监听器:
    <listener>
        <listener-class>net.test.XXXLisenet</listener-class>
    </listener>
    3.7)设置会话(Session)过期时间,其中时间以分钟为单位,假如设置60分钟超时:
    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>
    3.8)web应用的名称及描述
    <display-name>站台名称</display-name>定义站台的名称. 即web应用的名称
    <description>站台描述</discription>对站台做出描述    即web应用的描述
    3.9)web站台中的大小图标
    icon元素包含small-icon和large-icon两个子元素.用来指定web站台中小图标和大图标的路径.
    <small-icon>/路径/smallicon.gif</small-icon>  small-icon元素应指向web站台中某个小图标的路径,大小为16 X 16 pixel,但是图象文件必须为GIF或JPEG格             式,扩展名必须为:.gif或.jpg.
    <large-icon>/路径/largeicon-jpg</large-icon>large-icon元素应指向web站台中某个大图表路径,大小为32 X 32 pixel,但是图象文件必须为GIF或JPEG的格               式,扩展名必须为; gif或jpg.
    范例:
    <display-name>Develop Example</display-name>
    <description>JSP 2.0 Tech Book's Examples</description>
    <icon>
       <small-icon>/images/small.gif</small-icon>
       <large-icon>/images/large.gir</large-icon>
    </icon>
    3.10)web站台是否可分布式处理
    <distributable>
          distributable 元素为空标签,它的存在与否可以指定站台是否可分布式处理.如果web.xml中出现这个元素,则代表站台在开发时已经被设计为能在多个JSP Container 之间分散执行
    <distributable/>
    3.11)<context-param>:声明应用范围内的初始化参数
    context-param 元素用来设定web站台的环境参数(context),它包含两个子元素:param-name和param-value.
    <context-param>
        <param-name>参数名称</param-name>设定Context名称
        <param-value>值</param-value>设定Context名称的值
    </context-param>
    范例:
    <context-param>
       <param-name>param_name</param-name>
       <param-value>param_value</param-value>
    </context-param>
    备注:
    此所设定的参数,在JSP网页中可以使用下列方法来取得:${initParam.param_name}
    若在Servlet可以使用下列方法来获得:String param_name=getServletContext().getInitParamter("param_name");
    3.12)过滤器
    filter元素用来声明filter的相关设定.filter元素除了下面介绍的的子元素之外,还包括<servlet>、<icon>、<display-name>、<description>、<init-param>其用途一样
    <filter>
          <filter-name>Filter的名称</filter-name>定义Filter的名称.
           <filter-class>Filter的类名称</filter-class>定义Filter的类名称.例如:com.foo.hello
    </filter>
    范例:
    <filter>
          <filter-name>setCharacterEncoding</filter-name>
          <filter-class>coreservlet.javaworld.CH11.SetCharacterEncodingFilter</filter-class>
          <init-param>
                 <param-name>encoding</param-name>
                 <param-value>GB2312</param-value>
          </init-param>
    </filter>
    3.13)过滤器映射器
    filter-mapping 元素的两个主要子元素filter-name和url-pattern.用来定义Filter所对应的URL.
    <filter-mapping>
         <filter-name>Filter的名称</filter-name>定义Filter的名称.
         <url-pattern>URL</url-pattern>Filter所对应的RUL.例如:<url-pattern>/Filter/Hello</url-pattern>
         <servlet-name>Servlet的名称<servlet-name>定义servlet的名称.
          <dispatcher>REQUEST|INCLUDE|FORWARD|ERROR</disaptcher>设定Filter对应的请求方式,有RQUEST,INCLUDE,FORWAR,ERROR四种,默认为REQUEST.
    </filter-mapping>
    范例:
    <filter-mapping>
          <filter-name>GZIPEncoding</filter-name>
          <url-pattern>/*</url-pattern>
    </filter-mapping>
    3.14)<mima-mapping>
    mime-mapping包含两个子元素extension和mime-type.定义某一个扩展名和某一MIME Type做对映.
    <mima-mapping>
          <extension>扩展名名称</extension>扩展名称
          <mime-type>MIME格式</mime-type>MIME格式.
    </mime-mapping>
    范例:
    <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>
    <mime-mapping>
       <extension>ppt</extesnion>
       <mime-type>application/vnd.ms-powerpoint</mime-type>
    </mime-mapping>
    3.15)jsp相关配置
           jsp-config元素主要用来设定JSP的相关配置,<jsp:config>包括<taglib>和<jsp-property-group>两个子元素.其中<taglib>元素在JSP 1.2时就已经存在了;而<jsp-property-group>是JSP 2.0新增的元素。
    <jsp-config>

        taglib元素包含两个子元素taglib-uri和taglib-location.用来设定JSP网页用到的Tag Library路径.
       <taglib>
             <taglib-uri>URI</taglib-uri>taglib-uri定义TLD文件的URI,JSP网页的taglib指令可以经由这个URI存取到TLD文件.
             <taglib-location>/WEB-INF/lib/xxx.tld</taglib-laction>TLD文件对应Web站台的存放位置.
       </taglib>
       <jsp-property-group>jsp-property-group元素包含8个元素,分别为:
             <description>Description</descrition>此设定的说明
             <display-name>Name</display-name>此设定的名称
             <url-pattern>URL</url-pattern>设定值所影响的范围,如:/CH2 或者/*.jsp
             <el-ignored>true|false</el-ignored>若为true,表示不支持EL语法.
             <scripting-invalid>true|false</scripting-invalid>若为true表示不支持<%scription%>语法
             <page-encoding>encoding</page-encoding>设定JSP网页的编码
             <include-prelude>.jspf</include-prelude>设置JSP网页的抬头,扩展名为.jspf
             <include-coda>.jspf</include-coda>设置JSP网页的结尾,扩展名为.jspf
       </jsp-property-group>
    </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>
       <uri-pattern>/*</uri-pattern>
       <el-ignored>true</el-ignored>
       <page-encoding>GB2312</page-encoding>
       <scripting-inivalid>true</scripting-inivalid>
       ............
    </jsp-property-group>
    </jsp-config>
    View Code

    3.16)资源管理对象配置:
    <resource-env-ref>
         <resource-env-ref-name>jms/StockQueue</resource-env-ref-name>
    </resource-env-ref>
    3.17)<resource-ref>:资源工厂配置
    resource-ref元素包括五个子元素description,res-ref-name,res-type,res-auth,res-sharing-scope.利用JNDI取得站台可利用资源.
    <resource-ref>
    <description>说明</description>资源说明
    <rec-ref-name>资源名称</rec-ref-name>资源名称
    <res-type>资源种类</res-type>资源种类
    <res-auth>Application|Container</res-auth>资源由Application或Container来许可
    <res-sharing-scope>Shareable|Unshareable</res-sharing-scope>资源是否可以共享.默认值为 Shareable
    </resource-ref>
    范例:
    <resource-ref>
       <description>JNDI JDBC DataSource of JSPBook</description>
       <res-ref-name>jdbc/sample_db</res-ref-name>
       <res-type>javax.sql.DataSoruce</res-type>
       <res-auth>Container</res-auth>
    </resource-ref>
    3.17)配置数据库连接池就可在此配置
    <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>
    3.18)安全限制配置
    <security-constraint>
         <display-name>Example Security Constraint</display-name>
         <web-resource-collection>
             <web-resource-name>Protected Area</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>

  • 相关阅读:
    最近实际项目中遇到的技术问题与解决思路
    独立完成一个城市选择组件(阿里前端题目,内附知识点、思路)
    用Node.js写爬虫,撸羞羞的图片
    Flutter项目之app升级方案
    Flutter数据持久化入门以及与Web开发的对比
    为什么要学会正则表达式
    async/await,了解一下?
    面向面试题和实际使用谈promise
    从一次输入框无法输入的bug,谈如何限制输入框输入类型
    Vue组件的is具体用法
  • 原文地址:https://www.cnblogs.com/jiarui-zjb/p/10587491.html
Copyright © 2011-2022 走看看