1. 程式人生 > >web.xml 各個標籤詳解

web.xml 各個標籤詳解

對於web專案,web.xml可有,也可以沒有。但是對於大型的web專案使用web.xml會很方便的。

1.web.xml是專案的入口

2.web.xml可以做的事:

  • 指定歡迎頁面
  • 指定錯誤頁面、
  • 命名與定製url
  • 定製初始化引數
  • 設定過濾器
  • .設定監聽器
  • 設定session過期時間

 

1<?xml version="1.0" encoding="UTF-8"?> 

     宣告xml的版本

2<web-app> 

    指明xml使用的是哪個模式檔案(即xsd,就是描述xml的規範)

3<display-name>

    設定站點的標題顯示內容

4<session-config>

    設定使用者登入的session時間 

5<welcome-file-list>

    設定訪問站點專案名顯示的歡迎頁面,第一個不存在就顯示第二個。第一個存在第二個就會不起作用。

6<servlet>

   指定訪問的所有路徑都經過TestServlet.do處理器。

7<context-param />
 

用來設定web站臺的環境引數,引數也可以xml檔案。即載入各種配置檔案。

8  <listener>

設定監聽器

9<filter>

設定過濾器。

10<error-page>

指定錯誤的頁面,可以通過“異常型別”或“錯誤碼”指定錯誤處理頁面

<?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">
    <display-name>CLNContract</display-name>
    <!-- session週期30分鐘失效 -->
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
    <!-- session週期30分鐘失效 -->

<welcome-file-list>

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

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

</welcome-file-list>

<servlet>

<error-page>

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

    <location>/exception.jsp<location>

</error-page>

<error-page>  

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

      <location>/error.jsp</location>  

</error-page> 

<error-page>

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

    <location>/error404.jsp</location>

</error-page>

    <servlet-name>servlet1</servlet-name>

    <servlet-class>com.TestServlet</servlet-class>

</servlet>

<servlet-mapping>

    <servlet-name>servlet1</servlet-name>

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

</servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value> 
        classpath*:applicationContext*.xml
        </param-value>
    </context-param>
  <filter>
    <filter-name>systemFilter</filter-name>
    <filter-class>com.chinalife.util.UrlFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>systemFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <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>
        </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>
       <filter>
        <filter-name>struts-cleanup</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.ActionContextCleanUp
        </filter-class>
    </filter>
<!--
    <filter>
        <filter-name>loginAndStrutsFilter</filter-name>
        <filter-class>com.chinalife.common.web.filter.LoginAndStrutsFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>loginAndStrutsFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
  -->
    <!-- <filter> <filter-name>loginAndStrutsFilter</filter-name>
     <filter-class>com.chinalife.common.web.filter.LoginAndStrutsFilter</filter-class> 
        </filter> <filter-mapping> 
        <filter-name>loginAndStrutsFilter</filter-name> 
        <url-pattern>/*</url-pattern> </filter-mapping> -->
    <!-- struts 2.3.15.1 Config begin -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>

    <!-- Spring 3.2.4.RELEASE -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
    <listener-class>com.chinalife.common.web.filter.SpringInit</listener-class>
    </listener>
    <listener>
        <listener-class>com.chinalife.contract.userPermissionQuery.utils.NFDFlightDataTaskListener</listener-class>
    </listener>
    <filter>
        <filter-name>OpenSessionInView</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
        <init-param>
            <param-name>singleSession</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>flushMode</param-name>
            <param-value>AUTO</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>OpenSessionInView</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping><!--

    <filter>
        <filter-name>urlFilter</filter-name>
        <filter-class>com.chinalife.common.web.filter.UrlFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>urlFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    --><servlet>
        <servlet-name>XFireServlet</servlet-name>
        <servlet-class>
            org.codehaus.xfire.spring.XFireSpringServlet
        </servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>services.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>XFireServlet</servlet-name>
        <url-pattern>/service/*</url-pattern>
    </servlet-mapping>

    <jsp-config>
        <taglib>
            <taglib-uri>/WEB-INF/ecside.tld</taglib-uri>
            <taglib-location>/WEB-INF/ecside.tld</taglib-location>
        </taglib>
        <taglib>
            <taglib-uri>/user-defined-tags</taglib-uri>
            <taglib-location>/common/tlds/user-defined-tags.tld</taglib-location>
        </taglib>
    </jsp-config>

    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>
    
   <mime-mapping>
    <extension>xls</extension>
    <mime-type>application/msexcel</mime-type>
  </mime-mapping> 
  <mime-mapping>
    <extension>txt</extension>
    <mime-type>applcation/txt</mime-type>
  </mime-mapping> 
</web-app>