1. 程式人生 > >ssh的web.xml學習筆記

ssh的web.xml學習筆記

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>

錯誤頁面