Detailed explanation of web.xml configuration file in javeEE in java web

Detailed explanation of web.xml configuration file in javeEE in java web


    web.xml, the deployment descriptor file (specialized term), is defined in the Servlet specification and is the configuration file for web applications (Servlet 3.0 has begun to abandon the use of web.xml and instead use annotation annotations to configure projects), it usually Placed in the WEB-INF directory of the web project.


1. Basic configuration of web.xml

    a.Servlet 2.4

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" 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 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Servlet2.4Test</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

  • important point: 

    1. web.xml is a standard XML file, so it must also follow the XML syntax specification, that is, case-sensitive , and all element tags in web.xml are lowercase! 

    2. web.xml is sensitive to the order of elements


  • <web-app>: The top-level (root) element is used to describe the Servlet version specification, XML version, encoding and other information used by the project ( special note: If the declared Servlet version is inconsistent with the actual version used by the project, it may cause the web.xml card to be opened with Eclipse death phenomenon ), the declaration of each Servlet version is as follows: 

b. Servlet2.3


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app id="WebApp_ID">
    <display-name>Servlet2.3Test</display-name>
</web-app>

  • c. Servlet2.5

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
                    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
                  <display-name>Servlet2.5Test</display-name>
</web-app>

  • d. Servlet3.0 (this version has begun to recommend the use of annotations for web project configuration, but it can still be configured using web.xml) :

<?xml version="1.0" encoding="UTF-8"?> 
    <web-app version="3.0" 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_3_0.xsd">               <display-name>Servlet3.0Test</display-name>
</web-app>


2. Introduction to web.xml element node attributes



  • 1、<icon>: Indicates the large and small icons used by IDEs and GUI tools to represent web applications. The icons must be in GIF or JPEG format, and the extension must be .gif or .jpg. 
    • <small-icon>: Small icon, the size is 16 X 16 pixel
    • <large-icon>: Large icon, 32 X 32 pixel.
<icon> 
    <small-icon>/images/app_small.gif</small-icon> 
    <large-icon>/images/app_large.gif</large-icon> 
</icon> 


  • 2、<display-name>: Provides a name that GUI tools may use to label this particular web application.
<!-- 项目名称  -->
<display-name>ServletTest</display-name> 


  • 3、<disciption> : Describes the Web application.
<disciption>doing some Servlet Test!</disciption> 


  • 4、<context-param>: 用于声明应用范围内的初始化参数。在服务器启动时,容器会将<context-param>转换为键值对,并交给servletContext,所以可以通过ServletContext对象的getInitParamter()方法来获取该参数值。 
    • <param-name>: 参数名,该值在整个Web应用中必须是惟一的。
    • <param-value>: 参数值
    • <description >: 参数描述
<context-param>
        <!-- 参数名称  -->
        <param-name>test_param_name</param-name>
        <!-- 参数值  -->
        <param-value>This is param name test!</param-value>
</context-param>

JSP中取值方法:   ${initParam.test_param_name} 

Servlet取值方法: String param_name=getServletContext().getInitParamter("test_param_name");



  • 5、<filter>: 用于设定web应用的过滤器,可以过滤url或servlet请求。 
    • <filter-name>: 过滤器名称,与<filter-mapping><filter-name>相同,进行一 一映射,该值在整个Web应用中必须是惟一的。
    • <filter-class>:过滤类,在指定的请求被过滤器捕获到后,进行相关操作的类,该类必须实现javaxs.servlet.Filter接口。
    • <description>:过滤器描述说明。
    • <init-param>: 自定义配置过滤器相关参数值,可以通过javax.servlet.FilterConfig的getInitParameter(“param-name”)获取到。 
      • <param-name>: 参数名
      • <param-value>:参数值-
  • 6、<filter-mapping>: 用于声明过滤器要拦截的资源,必须与<filter>配合使用。filter过滤器的调用顺序是根据<filter-mapping>在web.xml中的先后顺序调用的。 
    • <filter-name>:过滤器名称,与<filter-mapping><filter-name>相同,进行一 一映射。
    • <servlet-name>:被过滤的servlet名称。
    • <url-pattern>:被过滤的url。
    • <dispatcher>: 指定过滤器所拦截的资源被 Servlet 容器调用的方式,可以是REQUEST,INCLUDE,FORWARD和ERROR之一,默认REQUEST。用户可以设置多个<dispatcher>子元素用来指定 Filter 对资源的多种调用方式进行拦截。

<dispatcher> 子元素可以设置的值及其意义: 
REQUEST:当用户直接访问页面时,Web容器将会调用过滤器。如果目标资源是通过RequestDispatcher的include()或forward()方法访问时,那么该过滤器就不会被调用。 
INCLUDE:如果目标资源是通过RequestDispatcher的include()方法访问时,那么该过滤器将被调用。除此之外,该过滤器不会被调用。 
FORWARD:如果目标资源是通过RequestDispatcher的forward()方法访问时,那么该过滤器将被调用,除此之外,该过滤器不会被调用。 
ERROR:如果目标资源是通过声明式异常处理机制调用时,那么该过滤器将被调用。除此之外,过滤器不会被调用。


  • 7、<listener>: 用于定义注册一个监听器类,可以收到事件什么时候发生以及用什么作为响应的通知。 
    • <listener-class>: 监听器类,须包含完整路径。

  • 8、<servlet>:声明一个Servlet类 
    • <servlet-name>:servlet名称
    • <servlet-class>:Servlet类,须包含完整路径。
    • <display-name>
    • <description>: Servlet相关描述说明。
    • <load-on-startup>:设置Servlet加载的次序。当这个数>=0时,Web容器启动时会按数值从小到大依次加载进内存(实例化此servlet,并调用它的init方法)。如果数值<0或没有指定,则由Web容器决定什么时候加载servlet(Web容器通常会在客户首次访问这个Servlet时才加载)。配置该属性值可以避免首次Servlet访问时,加载Servlet导致的响应延迟。
    • <jsp-file>
    • <init-param>: Servlet相关参数配置。 
      • <param-name>:参数名
      • <param-value>:参数值
      • <description>:相关描述说明
    • <run-as>: 如果定义了run-as元素,它会重写用于调用Web应用中servlet所设定的Enterprise JavaBean(EJB)的安全身份。 
      • <description>:相关描述说明
      • <role-name>:为当前Web应用定义的一个安全角色的名称。
  • 9、<servlet-mapping>: 将URL模式映射到某个servlet上。 
    • <servlet-name>:servlet名称
    • <url-pattern>

  • 10、<session-config>:指定javax.servlet.http.HttpSession对象的相关参数。 
    • <session-timeout>:指定默认的session超时时间,单位为分钟,该数值必须为整数。如果<session-timeout>元素的值为零或负数,则表示会话将永远不会超时。也可以用HttpSesion的setMaxInactiveInterval()方法设置sesion的时效。
<session-config>
    <session-timeout>30</session-timeout>
</session-config>


  • 11、<mime-mapping>:将mime类型映射到扩展名 
    • <extension>:文件扩展名
    • <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>
<mime-mapping>
   <extension>ppt</extesnion>
   <mime-type>application/vnd.ms-powerpoint</mime-type>
</mime-mapping>


  • 12、<welcome-file-list>: 
    • <welcome-file>:用于指定默认首页文件的名称

  • 13、<error-page>: 
    • <error-code>:HTTP错误代码
    • <exception-type>:Java异常类型的完全限定的名称,eg:java.lang.Exception | java.io.IOException | javax.servlet.ServletException …
    • location >:跳转的路径
<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>


  • 14、<jsp-config>:用于JSP的相关配置,该标签为Servlet2.4版本新增属性 
    • <taglib>:用于自定义标签库 
      • <taglib-uri>:TLD文件的URI
      • <taglib-location>:TLD文件在项目中的存放路径
    • <jsp-property-group>:Servlet2.4版本新增属性 
      • <description>: 相关描述说明
      • <display-name>
      • <el-ignored>:是否不支持EL语法,取值:true|false
      • <page-encoding>:设定JSP页面的编码
      • <scripting-invalid>:是否不支持<%scription%>语法,取值:true|false
      • <is-xml>
      • <icon>: 
        • <small-icon>
        • <large-icon>
      • <url-pattern>:设定值所影响的范围,如:/CH2 或者/*.jsp
      • <include-prelude>
      • <include-coda>

  • 15、<resource-env-ref>:声明与资源相关的一个管理对象 
    • <description>:相关描述说明
    • <env-entry-name>
    • <env-entry-type>
    • <env-entry-value>

  • 16、<resource-ref>: 
    • <description>:相关描述说明
    • <res-ref-name>:与java:comp/env上下文相对应的JNDI名称,在整个Web应用中必须是惟一的。
    • <res-type>:指明资源类型
    • <res-auth>:指定管理所引用资源的Manager,取值:Application | Container。Container表示由容器来创建和管理Resource,Application表示由web应用来创建和管理Resource。
    • <res-sharing-scope>:是否可以共享通过给定资源管理器连接工厂引用获得的连接。取值:Shareable(默认值) | Unshareable。
 <resource-env-ref>  
      <resource-env-ref-name>jdbc/testDataSource</resource-env-ref-name>  
      <resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
      <res-auth>Container</res-auth>  
 </resource-env-ref> 

  • 取该JNDI方法:
Context ctx = new InitialContext();  
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jndi/testDataSource");  
Connection conn = ds.getConnection();  


  • 17、<security-constraint>

  • 18、<login-config>:设定该web站点四种认证类型 
    • <auth-method>:指定验证方法。取值:BASIC | DIGEST | FORM | CLIENT-CERT。 

      BASIC:HTTP规范,Base64 
      DIGEST:HTTP规范,数据完整性强一些,但不是SSL 
      CLIENT-CERT:J2EE规范,数据完整性很强,公共钥匙(PKC) 
      FORM:J2EE规范,数据完整性非常弱,没有加密,允许有定制的登陆界面。

    • <realm-name>:指定BASIC验证中使用的领域名。<auth-method>属性配置为BASIC时才有效。
    • <form-login-config>:指定FORM验证中,相关登陆页面和出错页面。<auth-method>属性配置为FORM时才有效。 
      • <form-login-page>:定义登陆页面的路径
      • <form-error-page>:定义出错页面的路径

  • 19、<security-role>

  • 20、<env-entry>:’

  • 21、<ejb-ref>

  • 22、<ejb-local-ref>


感恩原作者:http://blog.csdn.net/chen_zw/article/details/47706069


参考资料: http://blog.csdn.net/guihaijinfen/article/details/8363839



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325746177&siteId=291194637