servlet xml 配置詳解

2013-11-08 09:04 161人閱讀 評論(0) 收藏 舉報html

web.xmlmyeclipsejava

首先能夠確定的是,加載順序與它們在 web.xml 文件中的前後順序無關。即不會由於 filter 寫在 listener 的前面而會先加載 filter。最終得出的結論是:listener -> filter -> servletweb

同時還存在着這樣一種配置節:context-param,它用於向 ServletContext 提供鍵值對,即應用程序上下文信息。咱們的 listener, filter 等在初始化時會用到這些上下文中的信息,那麼 context-param 配置節是否是應該寫在 listener 配置節前呢?實際上 context-param 配置節可寫在任意位置,所以真正的加載順序爲:context-param -> listener -> filter -> servletspring

對於某類配置節而言,與它們出現的順序是有關的。以 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() 方法的。sql

servlet 同 filter 相似,此處再也不贅述。數據庫

由此,能夠看出,web.xml 的加載順序是:context-param -> listener -> filter -> servlet ,而同個類型之間的實際程序調用的時候的順序是根據對應的 mapping 的順序進行調用的。apache

web.xml經常使用元素:tomcat

[html] view plaincopy安全

  1. <web-app>   服務器

  2. <display-name></display-name>定義了WEB應用的名字   

  3. <description></description> 聲明WEB應用的描述信息   

  4.   

  5. <context-param></context-param> context-param元素聲明應用範圍內的初始化參數。   

  6. <filter></filter> 過濾器元素將一個名字與一個實現javax.servlet.Filter接口的類相關聯。   

  7. <filter-mapping></filter-mapping> 一旦命名了一個過濾器,就要利用filter-mapping元素把它與一個或多個servlet或JSP頁面相關聯。   

  8. <listener></listener>servlet API的版本2.3增長了對事件監聽程序的支持,事件監聽程序在創建、修改和刪除會話或servlet環境時獲得通知。   

  9.                      Listener元素指出事件監聽程序類。   

  10. <servlet></servlet> 在向servlet或JSP頁面制定初始化參數或定製URL時,必須首先命名servlet或JSP頁面。Servlet元素就是用來完成此項任務的。   

  11. <servlet-mapping></servlet-mapping> 服務器通常爲servlet提供一個缺省的URL:http://host/webAppPrefix/servlet/ServletName。   

  12.               可是,經常會更改這個URL,以便servlet能夠訪問初始化參數或更容易地處理相對URL。在更改缺省URL時,使用servlet-mapping元素。   

  13.   

  14. <session-config></session-config> 若是某個會話在必定時間內未被訪問,服務器能夠拋棄它以節省內存。   

  15.           可經過使用HttpSession的setMaxInactiveInterval方法明確設置單個會話對象的超時值,或者可利用session-config元素制定缺省超時值。   

  16.   

  17. <mime-mapping></mime-mapping>若是Web應用具備想到特殊的文件,但願能保證給他們分配特定的MIME類型,則mime-mapping元素提供這種保證。   

  18. <welcome-file-list></welcome-file-list> 指示服務器在收到引用一個目錄名而不是文件名的URL時,使用哪一個文件。   

  19. <error-page></error-page> 在返回特定HTTP狀態代碼時,或者特定類型的異常被拋出時,可以制定將要顯示的頁面。   

  20. <taglib></taglib> 對標記庫描述符文件(Tag Libraryu Descriptor file)指定別名。此功能使你可以更改TLD文件的位置,   

  21.                   而不用編輯使用這些文件的JSP頁面。   

  22. <resource-env-ref></resource-env-ref>聲明與資源相關的一個管理對象。   

  23. <resource-ref></resource-ref> 聲明一個資源工廠使用的外部資源。   

  24. <security-constraint></security-constraint> 制定應該保護的URL。它與login-config元素聯合使用   

  25. <login-config></login-config> 指定服務器應該怎樣給試圖訪問受保護頁面的用戶受權。它與sercurity-constraint元素聯合使用。   

  26. <security-role></security-role>給出安全角色的一個列表,這些角色將出如今servlet元素內的security-role-ref元素   

  27.                    的role-name子元素中。分別地聲明角色可以使高級IDE處理安全信息更爲容易。   

  28. <env-entry></env-entry>聲明Web應用的環境項。   

  29. <ejb-ref></ejb-ref>聲明一個EJB的主目錄的引用。   

  30. < ejb-local-ref></ ejb-local-ref>聲明一個EJB的本地主目錄的應用。   

  31. </web-app>   


相應元素配置 
一、Web應用圖標:指出IDE和GUI工具用來表示Web應用的大圖標和小圖標 

[html] view plaincopy

  1. <icon>   

  2. <small-icon>/images/app_small.gif</small-icon>   

  3. <large-icon>/images/app_large.gif</large-icon>   

  4. </icon>   

二、Web 應用名稱:提供GUI工具可能會用來標記這個特定的Web應用的一個名稱 

[html] view plaincopy

  1. <display-name>Tomcat Example</display-name>   

三、Web 應用描述: 給出於此相關的說明性文本 

[html] view plaincopy

  1. <disciption>Tomcat Example servlets and JSP pages.</disciption>   

四、上下文參數:聲明應用範圍內的初始化參數。 

[html] view plaincopy

  1. <context-param>   

  2.   <param-name>ContextParameter</para-name>   

  3.   <param-value>test</param-value>   

  4.   <description>It is a test parameter.</description>   

  5. </context-param>   

  在servlet裏面能夠經過getServletContext().getInitParameter("context/param")獲得 

五、過濾器配置:將一個名字與一個實現javaxs.servlet.Filter接口的類相關聯。 

[html] view plaincopy

  1. <filter>   

  2.       <filter-name>setCharacterEncoding</filter-name>   

  3.       <filter-class>com.myTest.setCharacterEncodingFilter</filter-class>   

  4.       <init-param>   

  5.           <param-name>encoding</param-name>   

  6.           <param-value>GB2312</param-value>   

  7.       </init-param>   

  8. </filter>   

  9. <filter-mapping>   

  10.       <filter-name>setCharacterEncoding</filter-name>   

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

  12. </filter-mapping>   

六、監聽器配置 

[html] view plaincopy

  1. <listener>   

  2.     <listerner-class>listener.SessionListener</listener-class>   

  3. </listener>   

七、Servlet配置 
   基本配置 

[html] view plaincopy

  1. <servlet>   

  2.    <servlet-name>snoop</servlet-name>   

  3.    <servlet-class>SnoopServlet</servlet-class>   

  4. </servlet>   

  5. <servlet-mapping>   

  6.    <servlet-name>snoop</servlet-name>   

  7.    <url-pattern>/snoop</url-pattern>   

  8. </servlet-mapping>   

   高級配置 

[html] view plaincopy

  1. <servlet>   

  2.    <servlet-name>snoop</servlet-name>   

  3.    <servlet-class>SnoopServlet</servlet-class>   

  4.    <init-param>   

  5.       <param-name>foo</param-name>   

  6.       <param-value>bar</param-value>   

  7.    </init-param>   

  8.    <run-as>   

  9.       <description>Security role for anonymous access</description>   

  10.       <role-name>tomcat</role-name>   

  11.    </run-as>   

  12. </servlet>   

  13. <servlet-mapping>   

  14.    <servlet-name>snoop</servlet-name>   

  15.    <url-pattern>/snoop</url-pattern>   

  16. </servlet-mapping>   

   元素說明 
     <servlet></servlet> 用來聲明一個servlet的數據,主要有如下子元素: 
     <servlet-name></servlet-name> 指定servlet的名稱 
     <servlet-class></servlet-class> 指定servlet的類名稱 
     <jsp-file></jsp-file> 指定web站臺中的某個JSP網頁的完整路徑 
     <init-param></init-param> 用來定義參數,可有多個init-param。在servlet類中經過getInitParamenter(String name)方法訪問初始化參數 
     <load-on-startup></load-on-startup>指定當Web應用啓動時,裝載Servlet的次序。 
                                 當值爲正數或零時:Servlet容器先加載數值小的servlet,再依次加載其餘數值大的servlet. 
                                 當值爲負或未定義:Servlet容器將在Web客戶首次訪問這個servlet時加載它 
     <servlet-mapping></servlet-mapping> 用來定義servlet所對應的URL,包含兩個子元素 
       <servlet-name></servlet-name> 指定servlet的名稱 
       <url-pattern></url-pattern> 指定servlet所對應的URL 
八、會話超時配置(單位爲分鐘) 

[html] view plaincopy

  1. <session-config>   

  2.    <session-timeout>120</session-timeout>   

  3. </session-config>   

九、MIME類型配置 

[html] view plaincopy

  1. <mime-mapping>   

  2.    <extension>htm</extension>   

  3.    <mime-type>text/html</mime-type>   

  4. </mime-mapping>   

十、指定歡迎文件頁配置 

[html] view plaincopy

  1. <welcome-file-list>   

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

  3.    <welcome-file>index.html</welcome-file>   

  4.    <welcome-file>index.htm</welcome-file>   

  5. </welcome-file-list>   

十一、配置錯誤頁面 
 11.一、 經過錯誤碼來配置error-page 

[html] view plaincopy

  1. <error-page>   

  2.    <error-code>404</error-code>   

  3.    <location>/NotFound.jsp</location>   

  4. </error-page>   

  上面配置了當系統發生404錯誤時,跳轉到錯誤處理頁面NotFound.jsp。 
11.二、經過異常的類型配置error-page 

[html] view plaincopy

  1. <error-page>   

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

  3.     <location>/error.jsp</location>   

  4. </error-page>   

  上面配置了當系統發生java.lang.NullException(即空指針異常)時,跳轉到錯誤處理頁面error.jsp 
十二、TLD配置 

[html] view plaincopy

  1. <taglib>   

  2.     <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>   

  3.     <taglib-location>/WEB-INF/jsp/debug-taglib.tld</taglib-location>   

  4. </taglib>   

   若是MyEclipse一直在報錯,應該把<taglib> 放到 <jsp-config>中 

[html] view plaincopy

  1. <jsp-config>   

  2.    <taglib>   

  3.        <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>   

  4.        <taglib-location>/WEB-INF/pager-taglib.tld</taglib-location>   

  5.    </taglib>   

  6. </jsp-config>   

1三、資源管理對象配置 

[html] view plaincopy

  1. <resource-env-ref>   

  2.     <resource-env-ref-name>jms/StockQueue</resource-env-ref-name>   

  3. </resource-env-ref>   

1四、資源工廠配置 

[html] view plaincopy

  1. <resource-ref>   

  2.     <res-ref-name>mail/Session</res-ref-name>   

  3.     <res-type>javax.mail.Session</res-type>   

  4.     <res-auth>Container</res-auth>   

  5. </resource-ref>   

   配置數據庫鏈接池就可在此配置: 

[html] view plaincopy

  1. <resource-ref>   

  2.     <description>JNDI JDBC DataSource of shop</description>   

  3.     <res-ref-name>jdbc/sample_db</res-ref-name>   

  4.     <res-type>javax.sql.DataSource</res-type>   

  5.     <res-auth>Container</res-auth>   

  6. </resource-ref>   

1五、安全限制配置 

[html] view plaincopy

  1. <security-constraint>   

  2.    <display-name>Example Security Constraint</display-name>   

  3.    <web-resource-collection>   

  4.       <web-resource-name>Protected Area</web-resource-name>   

  5.       <url-pattern>/jsp/security/protected/*</url-pattern>   

  6.       <http-method>DELETE</http-method>   

  7.       <http-method>GET</http-method>   

  8.       <http-method>POST</http-method>   

  9.       <http-method>PUT</http-method>   

  10.    </web-resource-collection>   

  11.    <auth-constraint>   

  12.      <role-name>tomcat</role-name>   

  13.      <role-name>role1</role-name>   

  14.    </auth-constraint>   

  15. </security-constraint>   

1六、登錄驗證配置 

[html] view plaincopy

  1. <login-config>   

  2.   <auth-method>FORM</auth-method>   

  3.   <realm-name>Example-Based Authentiation Area</realm-name>   

  4.   <form-login-config>   

  5.      <form-login-page>/jsp/security/protected/login.jsp</form-login-page>   

  6.      <form-error-page>/jsp/security/protected/error.jsp</form-error-page>   

  7.   </form-login-config>   

  8. </login-config>   

1七、安全角色:security-role元素給出安全角色的一個列表,這些角色將出如今servlet元素內的security-role-ref元素的role-name子元素中。 
    分別地聲明角色可以使高級IDE處理安全信息更爲容易。 

[html] view plaincopy

  1. <security-role>   

  2.    <role-name>tomcat</role-name>   

  3. </security-role>   

1八、Web環境參數:env-entry元素聲明Web應用的環境項 

[html] view plaincopy

  1. <env-entry>   

  2.    <env-entry-name>minExemptions</env-entry-name>   

  3.    <env-entry-value>1</env-entry-value>   

  4.    <env-entry-type>java.lang.Integer</env-entry-type>   

  5. </env-entry>   

1九、EJB 聲明 

[html] view plaincopy

  1. <ejb-ref>   

  2.    <description>Example EJB reference</decription>   

  3.    <ejb-ref-name>ejb/Account</ejb-ref-name>   

  4.    <ejb-ref-type>Entity</ejb-ref-type>   

  5.    <home>com.mycompany.mypackage.AccountHome</home>   

  6.    <remote>com.mycompany.mypackage.Account</remote>   

  7. </ejb-ref>   

20、本地EJB聲明 

[html] view plaincopy

  1. <ejb-local-ref>   

  2.    <description>Example Loacal EJB reference</decription>   

  3.    <ejb-ref-name>ejb/ProcessOrder</ejb-ref-name>   

  4.    <ejb-ref-type>Session</ejb-ref-type>   

  5.    <local-home>com.mycompany.mypackage.ProcessOrderHome</local-home>   

  6.    <local>com.mycompany.mypackage.ProcessOrder</local>   

  7. </ejb-local-ref>   

2一、配置DWR 

[html] view plaincopy

  1. <servlet>   

  2.     <servlet-name>dwr-invoker</servlet-name>   

  3.     <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>   

  4. </servlet>   

  5. <servlet-mapping>   

  6.     <servlet-name>dwr-invoker</servlet-name>   

  7.     <url-pattern>/dwr/*</url-pattern>   

  8. </servlet-mapping>   

2二、配置Struts 

[html] view plaincopy

  1. <filter>  

  2.     <filter-name>struts</filter-name>  

  3.     <filter-class>  

  4.         org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  

  5. </filter>  

  6. <filter-mapping>  

  7.     <filter-name>struts</filter-name>  

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

  9. </filter-mapping>  

2三、配置Spring(基本上都是在Struts中配置的) 

[html] view plaincopy

  1.  <!-- 指定spring配置文件位置 -->   

  2.  <context-param>   

  3.     <param-name>contextConfigLocation</param-name>   

  4.     <param-value>   

  5.      <!--加載多個spring配置文件 -->   

  6.       /WEB-INF/applicationContext.xml, /WEB-INF/action-servlet.xml   

  7.     </param-value>   

  8.  </context-param>   

  9.  <!-- 定義SPRING監聽器,加載spring -->   

  10. <listener>   

  11.    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   

  12. </listener>   

  13. <listener>   

  14.    <listener-class>   

  15.      org.springframework.web.context.request.RequestContextListener   

  16.    </listener-class>   

  17. </listener>

相關文章
相關標籤/搜索