web.xml 配置詳解

學名叫部署描述性文件
大小寫敏感(xml 本來就是大小寫敏感的),元素出現順序敏感
最簡單的 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>

文件結構 總共 16 個元素
icon 
    small-icon 值爲相對於站點的圖片路徑,格式必須爲 .gif 或 .jpg 下同
    large-icon
display-name 用於說明應用名稱
description 詳細說明應用
context-param 包含兩個字元素,用來設定 web 應用的環境參數
    param-name
    param-value
        取值的話:   jsp 用initParam.參數名 servlet 中用 getServletContext().getInitParam("參數名");
filter 與 filter-mapping 用於攔截訪問路徑
filter
    filter-name
    filter-class
    init-param
      param-name
      param-value
filter-mapping 
    filter-name
    url-pattern
listener
    listener-class
servlet
    servlet-name
    servlet-class
    init-param
servlet-mapping
    servlet-name
    url-pattern
    load-on-startup 啓動順序
session-config
    session-timeout 設置 session 超時時間 以分鐘記
mime-mapping 用於定義某一擴展名和某一 MIME type 做對應
    extension
    mime-type
      例:
         <mime-mapping>
            <extension>xls</extension>
            <mime-type>application/vnd.ms-excel</mime-type>
          </mime-mapping>
          <mime-mapping>
            <extension>doc</extension>
            <mime-type>application/vnd.ms-word</mime-type>
          </mime-mapping>
welcome-file-list
    welcome-file 相對於站點的網頁
error-page
    error-code
    exception-type
    location 相對於站點的資源路徑或網頁
jsp-config
    taglib
      taglib-uri
      taglib-location
    jsp-property-group
      description
      display-name
      url-pattern
      el-ignored
      scripting-invalid
      page-encoding
      include-prelude
      include-coda
resource-env-ref 當 web 應用查找該資源的時候,返回的 java 類名的全稱
    resource-env-ref-name 
    resource-env-ref-type
resource-ref 利用 jndi 取得應用可利用資源
    description
    rec-ref-name
    rec-type
    rec-auth
    res-sharing-scope

完整示例:
<?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">
  <icon>
    <small-icon>/images/three_sun_16_16.jpg</small-icon>
    <large-icon>/images/three_sun_32_32.jpg</large-icon>
  </icon>
  <display-name>template</display-name> 
  <description>應用描述</description>
  <context-param>
    <param-name>abc</param-name>
    <param-value>context-param 值爲 sanri</param-value>
  </context-param>
  <filter>
    <filter-name>encoding</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>xxservlet</servlet-name>
    <servlet-class></servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>xxservlet</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
  <session-config>
    <session-timeout>30</session-timeout>
  </session-config>
  <mime-mapping>
    <extension>xls</extension>
    <mime-type>application/vnd.ms-excel</mime-type>
  </mime-mapping>
  <mime-mapping>
    <extension>doc</extension>
    <mime-type>application/vnd.ms-word</mime-type>
  </mime-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <error-page>
    <error-code>500</error-code>
    <exception-type>Exception</exception-type>
    <location>/index.jsp</location>
  </error-page>
  <jsp-config>
    <taglib>
      <taglib-uri>c</taglib-uri>
      <taglib-location>/WEB-INF/lib/c.tld</taglib-location>
    </taglib>
    <jsp-property-group>
          <description>屬性描述</description>
          <display-name>某某屬性</display-name>
          <url-pattern>/*</url-pattern>
          <el-ignored>false</el-ignored>
          <scripting-invalid>false</scripting-invalid>
          <page-encoding>utf-8</page-encoding>
          <include-prelude>.jspf</include-prelude>
          <include-coda>.jspf</include-coda>
    </jsp-property-group>
  </jsp-config>
  <resource-env-ref>
    <resource-env-ref-name>jdbc/mssql</resource-env-ref-name>
    <resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
  </resource-env-ref>
  <resource-ref>
    <description>資源描述</description>
    <description>資源名稱</description>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Application</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
  </resource-ref>
</web-app>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章