//參考 http://mianhuaman.iteye.com/blog/1105522html
本項目的web.xml文件內容java
<!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> <display-name>CQR</display-name> <filter> <filter-name>CharacterEncodingFilter</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> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
詳解web.xml文件web
1、開篇
spring
1.每次啓動web項目的時候,web容器都會去讀取它的配置文件web.xml,讀取<listener>和<context-param>兩個節點。--個人項目只配了<listener>
sql
2.緊接着容器建立一個servletContext(servlet上下文),這個web項目的全部部分都將共享這個上下文。數據庫
3.容器將<context-param>轉換爲鍵值對,並交給servletContext。session
4.容器建立<listener>中的類實例,建立監聽器。app
2、load-on-startupjsp
load-on-startup配置在<servlet>中,見本工程配置。工具
load-on-startup元素在web應用啓動的時候指定了servlet被加載的順序,它的值必須是一個整數。
在servlet的配置中,<load-on-startup>1</load-on-startup> 的含義是:容器在啓動的時候就加載這個servlet。
3、加載順序
加載順序與它們在web.xml文件中的前後順序無關,即不會由於filter寫在listener以前就先加載filter。它們的加載順序是:context-param -->listener -->filter -->servlet。
4、web.xml文件詳解
web.xml文件中全部的元素都放在<web -app>與</web -app>之間。
<display-name></display-name> 定義站臺的名稱 --本項目配置中有
<discription></discription> 對站臺的描述,能夠不要
<context-param></context-param> 設定web站臺的環境參數,它有兩個子元素
<param-name></param-name> 指定參數的名稱
<param-value></param-value> 設定參數值
eg:
<context-param> <param-name>my_param</param-name> <param-value>hello</param-value> </context-param>
在此設定的參數,能夠在servlet中用getServletContext().getInitParameter("my_param") 來取得
<filter></filter>包含以下子元素
<filter-name></filter-name> 指定filter名字
<filter-class></filter-class> 指定filter的類的名稱
<init-param></init-param>用來定義參數,它有兩個子元素
<param-name></param-name> 指定參數的名稱
<param-value></param-value> 設定參數值
eg:本項目中字符編碼過濾
<filter> <filter-name>CharacterEncodingFilter</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> </filter>
與<filter></filter>同時使用的是<filter-mapping></filter-mapping> 用來定義filter所對應的URL,它有兩個子元素
<filter-name></filter-name> 指定filter的名字
<url-pattern></url-pattern> 指定filter所對應的URL
eg:
<filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<listener></listener>用來設定listener接口,主要子元素有
<listener-class></listener-class>定義listener的類的名稱
eg:
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
<servlet></servlet>用來聲明一個servlet的數據,主要有如下子元素
<servlet-name></servlet-name> 指定servlet名稱
<servlet-class></servlet-class> 指定servlet的類的名稱
eg:
<servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet>
與servlet一塊兒使用的是<servlet-mapping>用來定義servlet所對應的URL,包含兩個子元素
<servlet-name></servlet-name> 指定servlet的名稱
<url-pattern></url-pattern> 指定servlet所對應的URL
<servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
<welcome-file-list></welcome-file-list> 用來定義首頁的列單,包含一個子元素
<welcome-file></welcome-file> 指定首頁的文件名稱
例如:
<welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> </welcome-file-list>
<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>
5、相應元素配置
1.web應用圖標:指出IDE和GUI工具用來表示web應用的大圖標和小圖標
<icon>
<small-icon>/images/app_small.gif</small-icon>
<large-icon>/images/app_large.gif</large-icon>
</icon>
2.web應用名稱:提供給GUI工具展示時該web應用的名稱
<display-name> QQZQ </display-name>
3.web應用描述:給出這個web應用的一些說明性文本
<disciption> CQR servlet and JSP pages</disciption>
4.上下文參數:聲明應用範圍內的初始化參數
<context-param> <param-name>ContextParameter</para-name> <param-value>test</param-value> <description>It is a test parameter.</description> </context-param>
在servlet裏面能夠經過getServletContext().getInitParameter("context/param")獲得
5.過濾器設置:將一個名字與一個實現javaxs.servlet.Filter接口的類相關聯
<filter> <filter-name>CharacterEncodingFilter</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> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
6.監聽器設置
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
7.servlet配置
<servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
8.會話超時配置(單位爲分鐘)
<session-config> <session-timeout>120</session-timeout> </session-config>
9.數據庫鏈接池配置
<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>
自此,結束。