web.xml 中的listener、 filter、servlet 加載順序及其詳解
首先可以肯定的是,加載順序與它們在 web.xml 文件中的先後順序無關。即不會因爲 filter 寫在 listener 的前面而會先加載 filter。最終得出的結論是:listener -> filter -> servlet
同時還存在着這樣一種配置節:context-param,它用於向 ServletContext 提供鍵值對,即應用程序上下文信息。我們的 listener, filter 等在初始化時會用到這些上下文中的信息,那麼 context-param 配置節是不是應該寫在 listener 配置節前呢?實際上 context-param 配置節可寫在任意位置,因此真正的加載順序爲:context-param -> listener -> filter -> servlet
對於某類配置節而言,與它們出現的順序是有關的。以 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 的順序進行調用的。
web.xml配置詳解
Web.xml常用元素
<display-name></display-name>
定義了WEB應用的名字
<description></description>
聲明WEB應用的描述信息
<context-param></context-param>
context-param元素聲明應用範圍內的初始化參數。
<filter></filter>
過濾器元素將一個名字與一個實現javax.servlet.Filter接口的類相關聯。
<filter-mapping></filter-mapping>
一旦命名了一個過濾器,就要利用filter-mapping元素把它與一個或多個servlet或JSP頁面相關聯。
<listener></listener>
servlet API的版本2.3增加了對事件監聽程序的支持,事件監聽程序在建立、修改和刪除會話或servlet環境時得到通知。
Listener元素指出事件監聽程序類。
<servlet></servlet>
在向servlet或JSP頁面制定初始化參數或定製URL時,必須首先命名servlet或JSP頁面。Servlet元素就是用來完成此項任務的。
<servlet-mapping></servlet-mapping>
服務器一般爲servlet提供一個缺省的URL:http://host/webAppPrefix/servlet/ServletName。
但是,常常會更改這個URL,以便servlet可以訪問初始化參數或更容易地處理相對URL。在更改缺省URL時,使用servlet-mapping元素。
<session-config></session-config>
如果某個會話在一定時間內未被訪問,服務器可以拋棄它以節省內存。
可通過使用HttpSession的setMaxInactiveInterval方法明確設置單個會話對象的超時值,或者可利用session-config元素制定缺省超時值。
<mime-mapping></mime-mapping>
如果Web應用具有想到特殊的文件,希望能保證給他們分配特定的MIME類型,則mime-mapping元素提供這種保證。
<welcome-file-list></welcome-file-list>
指示服務器在收到引用一個目錄名而不是文件名的URL時,使用哪個文件。
<error-page></error-page>
在返回特定HTTP狀態代碼時,或者特定類型的異常被拋出時,能夠制定將要顯示的頁面。
<taglib></taglib>
對標記庫描述符文件(Tag Libraryu Descriptor file)指定別名。此功能使你能夠更改TLD文件的位置,
而不用編輯使用這些文件的JSP頁面。
<resource-env-ref></resource-env-ref>
聲明與資源相關的一個管理對象。
<resource-ref></resource-ref>
聲明一個資源工廠使用的外部資源。
<security-constraint></security-constraint>
制定應該保護的URL。它與login-config元素聯合使用
<login-config></login-config>
指定服務器應該怎樣給試圖訪問受保護頁面的用戶授權。它與sercurity-constraint元素聯合使用。
<security-role></security-role>
給出安全角色的一個列表,這些角色將出現在servlet元素內的security-role-ref元素
的role-name子元素中。分別地聲明角色可使高級IDE處理安全信息更爲容易。
<env-entry></env-entry>
聲明Web應用的環境項。
<ejb-ref></ejb-ref>
聲明一個EJB的主目錄的引用。
<ejb-local-ref></ ejb-local-ref>
聲明一個EJB的本地主目錄的應用。
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root</param-value>
</context-param>
web.xml文件中webAppRootKey屬性是web項目的絕對路徑,默認值是webApp.root,可以通過System.getProperty(“webApp.root”)來獲取屬性值或者在配置文件中通過${webApp.root}獲得。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring-context*.xml</param-value>
</context-param>
在web.xml中通過contextConfigLocation配置spring,contextConfigLocation
參數定義了要裝入的 Spring 配置文件。在指定位置加載spring-context.
<listener>
<description>spring監聽器</description>
<listenerclass>com.zttech.sys.listener.WebContextListener</listener-class>
</listener>
com.zttech.sys.listener.WebContextListener繼承了org.springframework.web.context.ContextLoaderListener
ContextLoaderListener的作用就是啓動Web容器時,自動裝配spring-context.xml的配置信息
<listener>
<description>Introspector緩存清除監聽器</description>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
org.springframework.web.util.IntrospectorCleanupListener監聽器主要負責處理由JavaBean Introspector使用而引起的緩衝泄露。
<listener>
<description>request監聽器</description>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
在整合spring容器時使用ContextLoaderListener,它實現了ServletContextListener監聽器接口,
ServletContextListener只負責監聽web容器啓動和關閉的事件。
而RequestContextListener實現ServletRequestListener監聽器接口,該監聽器監聽HTTP請求事件,web服務器
接收的每一次請求都會通知該監聽器。
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
OpenSessionInViewFilter的主要功能是用來把一個Hibernate Session和一次完整的請求過程對應的線程相綁定。目的是爲了實現”Open Session in View”的模式。
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
授權與權限管理,DelegatingFilterProxy類的一些內部運行機制,其實主要作用就是一個代理模式的應用,可以把servlet 容器中的filter同spring容器中的bean關聯起來。
<filter>
<filter-name>DruidWebStatFilter</filter-name>
<filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class>
<init-param>
<param-name>exclusions</param-name>
<param-value>*.js,*.gif,*.jpg,*.png,*.GIF,*.JPG,*.PNG,*.woff,*.eot,*.ttf,*.css,*.ico,*.ICO,*.pdf,*.rar,*.zip,*.doc,*.docx,*.xml,*.xls,*.xlsx,*.ppt,*.pptx,/druid/*</param-value>
</init-param>
<init-param>
<param-name>sessionStatMaxCount</param-name>
<param-value>1000</param-value>
</init-param>
<init-param>
<param-name>principalSessionName</param-name>
<param-value>_curr_emp</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>DruidWebStatFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
WebStatFilter用於採集web-jdbc關聯監控的數據。
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring-mvc*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>*.jhtml</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>*.htmlx</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/im/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
DispatcherServlet是前端控制器設計模式的實現,提供Spring Web MVC的集中訪問點,而且負責職責的分派,而且與Spring IoC容器無縫集成,從而可以獲得Spring的所有好處。
url-pattern:表示哪些請求交給Spring Web MVC處理, “/” 是用來定義默認servlet映射的。也可以如“*.html”表示攔截所有以html爲擴展名的請求。
spring-mvc加載spring-mvc*.xml來初始化上下文。
<!-- 如果某個會話在一定時間內未被訪問,服務器可以拋棄它以節省內存 -->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<error-page>
<error-code>500</error-code>
<location>/WEB-INF/views/error/500.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/views/error/404.jsp</location>
</error-page>
<error-page>
<error-code>400</error-code>
<location>/WEB-INF/views/error/400.jsp</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/WEB-INF/views/error/403.jsp</location>
</error-page>
錯誤頁面