Web.xml的配置及加載 .

Web.xml 常用元素(網上資源整理)

<web-app>

1.上下文初始化參數

<context-param></context-param>

 

   在sevlet的方法中可以通過如下方式獲取context-param的參數

 

 

this.getServletConfig().getServletContext().getInitParameter("name");

 

   在jsp中 直接使用頁面隱含對象application或者上面的方式;

 

 

2. 過濾器    

 

<filter> 
        <filter-name>setCharacterEncoding</filter-name> 
        <filter-class>com.myTest.setCharacterEncodingFilter</filter-class> 
        <init-param> 
            <param-name>encoding</param-name> 
            <param-value>GB2312</param-value> 
        </init-param> 
</filter> 
<filter-mapping> 
        <filter-name>setCharacterEncoding</filter-name> 
        <url-pattern>/*</url-pattern> 
</filter-mapping>

   作用:將一個名字與一個實現javax.servlet.Filter 接口的類相關聯,一旦命名了一個過濾器,就要利用filter-mapping元素把它與一個或多個servlet或JSP頁面相關聯。

 

 

3. 指示服務器在收到【應用服務名時目錄名】訪問的文件,以welcome-file的順序依次尋找,下面配置將會先打開index.htm文件,找不到時,纔會打開index.jsp文件;

 

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

 

 

4. 在返回特定HTTP 狀態代碼 時,或者特定類型的異常被拋出時,能夠制定將要顯示的頁面。

<error-page>
    <error-code>404</error-code>
    <location>/error.htm</location>
</error-page>
方式二:當系統發生java.lang.NullException時,頁面跳轉到error.jsp
<error-page> 
       <exception-type>java.lang.NullException</exception-type> 
       <location>/error.jsp</location> 
  </error-page> 

 

5.監聽 servlet API 的版本2.3+才支持監聽,讓建立、修改和刪除會話request,response ,session等 環 境時得到通知。 
   listener-class 爲實現了Listener接口的類

 

<listener> 
<listener-class></listener-class> 
</listener> 

 

6.回話超時設置  可通過使用HttpSession的setMaxInactiveInterval方法明確設置單個會話對象的超時值,會話在一定時間內未被訪問,服務器可以拋棄它以節省內存,或者可利用session-config元素制定缺省超時值。

 

寫道
<session-config> 
<session-timeout>30</session-timeout> 
</session-config> 

 

 7、如果Web應用具有想到特殊的文件 ,希望能保證給他們分配特定的MIME類型,則mime-mapping元素提供這種保證。      

 

 

<mime-mapping> 
       <extension>doc</extension> 
       <mime-type>application/msword</mime-type> 
</mime-mapping> 


 

8.對標記庫 描述符文件指定別名。此功能使你能夠更改TLD文件的位置,而不用編輯使用這些文件的JSP頁面

 

寫道
<taglib> 
    <taglib-uri>/applus</taglib-uri> 
    <taglib-location>/WEB-INF/taglib/applus.tld</taglib-location> 
</taglib> 
如果MyEclipse一直在報錯,應該向如下配置 
<jsp-config> 
    <taglib> 
         <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri> 
         <taglib-location>/WEB-INF/pager-taglib.tld</taglib-location> 
    </taglib> 
</jsp-config>

 

9.資源管理對象 配置

 

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

 

10.資源工廠配置 

寫道
<resource-ref> 
    <res-ref-name>mail/Session</res-ref-name> 
    <res-type>javax.mail.Session</res-type> 
    <res-auth>Container</res-auth> 
</resource-ref> 

數據源配置: 

<resource-ref> 
    <res-ref-name>webglDS</res-ref-name> 
    <res-type>javax.sql.DataSource</res-type> 
    <res-auth>Container</res-auth> 
    <res-sharing-scope>Shareable</res-sharing-scope> 
</resource-ref>

  

 11.高級servlet配置

 

    寫道

<servlet> 
    <servlet-name>snoop</servlet-name> 
    <servlet-class>com.SnoopServlet</servlet-class> 
    <init-param> 
         <param-name>foo</param-name> 
         <param-value>bar</param-value> 
    </init-param> 
    <run-as> 
         <description>Security role for anonymous access</description> 
         <role-name>tomcat</role-name> 
    </run-as> 
</servlet> 


<servlet-mapping> 
      <servlet-name>snoop</servlet-name> 
      <url-pattern>/snoop</url-pattern> 
</servlet-mapping> 


部分元素說明: 
<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,相同的值按照web.xml順序加載; 
當值爲負或未定義:Servlet容器將在Web客戶首次訪問這個servlet時加載它 

 

12.Web應用圖標:指出IDE和GUI工具用來表示Web應用的大圖標和小圖標

 

寫道
<icon> 
    <small-icon>/images/app_small.gif</small-icon> 
    <large-icon>/images/app_large.gif</large-icon> 
</icon> 

 

13.安全限制配置 

寫道
<security-constraint> 
      <display-name>Example Security Constraint</display-name> 
     <web-resource-collection> 
     <web-resource-name>Protected Area</web-resource-name> 
            <url-pattern>/jsp/security/protected/*</url-pattern> 
            <http-method>DELETE</http-method> 
           <http-method>GET</http-method> 
           <http-method>POST</http-method> 
           <http-method>PUT</http-method> 
    </web-resource-collection> 
    <auth-constraint> 
    <role-name>tomcat</role-name> 
    <role-name>role1</role-name> 
    </auth-constraint> 
</security-constraint>

 

 

14.登陸驗證配置 

   

寫道
<login-config> 
<auth-method>FORM</auth-method> 
<realm-name>Example-Based Authentiation Area</realm-name> 
<form-login-config> 
<form-login-page>/jsp/security/protected/login.jsp</form-login-page> 
<form-error-page>/jsp/security/protected/error.jsp</form-error-page> 
</form-login-config> 
</login-config> 

 

 

 15.配置DWR

<servlet>
    <servlet-name>dwr-invoker</servlet-name>
    <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
</servlet>
<servlet-mapping>
   <servlet-name>dwr-invoker</servlet-name>
   <url-pattern>/dwr/*</url-pattern>
</servlet-mapping>

 

 

16.配置Struts 

  

<servlet> 
    <servlet-name>webwork</servlet-name> 
    <servlet-class>com.opensymphony.webwork.dispatcher.ServletDispatcher</servlet-class> 
</servlet> 
<servlet-mapping> 
   <servlet-name>webwork</servlet-name> 
   <url-pattern>*.action</url-pattern> 
</servlet-mapping>

 

 

17.spring 配置

寫道
<!-- 指定spring配置文件位置 --> 
<context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/applicationContext.xml, /WEB-INF/action-servlet.xml <!--加載多個spring配置文件 --></param-value> 
</context-param> 

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

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

<listener> 
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 
</listener> 

 

   

17、安全角色:security-role元素給出安全角色的一個列表,這些角色將出現在servlet元素內的security-role-ref元素的role-name子元素中。 
    分別地聲明角色可使高級IDE處理安全信息更爲容易。
 
  <security-role> 
     <role-name>tomcat</role-name> 
  </security-role> 
18、Web環境參數:env-entry元素聲明Web應用的環境項 
  <env-entry> 
     <env-entry-name>minExemptions</env-entry-name> 
     <env-entry-value>1</env-entry-value> 
     <env-entry-type>java.lang.Integer</env-entry-type> 
  </env-entry> 
19、EJB 聲明 
  <ejb-ref> 
     <description>Example EJB reference</decription> 
     <ejb-ref-name>ejb/Account</ejb-ref-name> 
     <ejb-ref-type>Entity</ejb-ref-type> 
     <home>com.mycompany.mypackage.AccountHome</home> 
     <remote>com.mycompany.mypackage.Account</remote> 
  </ejb-ref> 
20、本地EJB聲明 
  <ejb-local-ref> 
     <description>Example Loacal EJB reference</decription> 
     <ejb-ref-name>ejb/ProcessOrder</ejb-ref-name> 
     <ejb-ref-type>Session</ejb-ref-type> 
     <local-home>com.mycompany.mypackage.ProcessOrderHome</local-home> 
     <local>com.mycompany.mypackage.ProcessOrder</local> 
  </ejb-local-ref>


下面附加一個項目中的完整的Web.xml配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  3.     <display-name>arch4</display-name>  
  4.       
  5.     <!-- Spring ApplicationContext配置文件的路徑,可使用通配符,多個路徑用,號分隔   此參數用於後面的Spring-Context loader -->  
  6.     <context-param>  
  7.         <param-name>contextConfigLocation</param-name>  
  8.         <param-value>classpath*:spring/*.xml</param-value>  
  9.     </context-param>  
  10.       
  11.     <!--Spring ApplicationContext 載入 -->  
  12.     <listener>  
  13.         <listener-class>  
  14.             org.springframework.web.context.ContextLoaderListener  
  15.         </listener-class>  
  16.     </listener>  
  17.       
  18.     <servlet>  
  19.         <display-name>InitEbsConfig</display-name>  
  20.         <servlet-name>InitEbsConfig</servlet-name>  
  21.         <servlet-class>com.suptools.util.InitEbsConfig</servlet-class>  
  22.         <load-on-startup>1</load-on-startup>  
  23.     </servlet>  
  24.       
  25.     <filter>    
  26.         <filter-name>struts-cleanup</filter-name>    
  27.         <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>    
  28.     </filter>    
  29.     <filter-mapping>    
  30.         <filter-name>struts-cleanup</filter-name>    
  31.         <url-pattern>/*</url-pattern>    
  32.     </filter-mapping>    
  33.       
  34.       
  35.     <!-- 著名 Character Encoding filter -->  
  36.     <filter>  
  37.         <filter-name>encodingFilter</filter-name>  
  38.         <filter-class>  
  39.             org.springframework.web.filter.CharacterEncodingFilter  
  40.         </filter-class>  
  41.         <init-param>  
  42.             <param-name>encoding</param-name>  
  43.             <param-value>GBK</param-value>  
  44.         </init-param>  
  45.     </filter>  
  46.       
  47.     <!--Hibernate Open Session in View Filter,在每次的整個request過程中,對數據庫的多次操作使用同一個Hibernate Session   
  48.      hibernateFilter位置放在此處是爲解決延遲加載和事務提交的問題   
  49.     <filter>  
  50.         <filter-name>hibernateFilter</filter-name>  
  51.         <filter-class>  
  52.             org.springframework.orm.hibernate3.support.OpenSessionInViewFilter  
  53.         </filter-class>  
  54.     </filter>  
  55.     <filter-mapping>  
  56.         <filter-name>hibernateFilter</filter-name>  
  57.         <url-pattern>*.action</url-pattern>  
  58.     </filter-mapping>  
  59. -->  
  60.     <filter>  
  61.         <filter-name>struts2</filter-name>  
  62.         <filter-class>  
  63.             org.apache.struts2.dispatcher.FilterDispatcher  
  64.         </filter-class>  
  65.     </filter>  
  66.   
  67.     <filter-mapping>  
  68.         <filter-name>encodingFilter</filter-name>  
  69.         <url-pattern>*.action</url-pattern>  
  70.     </filter-mapping>  
  71.     <filter-mapping>  
  72.         <filter-name>encodingFilter</filter-name>  
  73.         <url-pattern>*.jsp</url-pattern>  
  74.     </filter-mapping>  
  75.   
  76.      <!--  
  77. 說明1:在Servlet2.4版本的web.xml中添加了<dispatcher>標籤,這個元素可能有4種值,即REQUEST、FORWARD、INCLUDE、ERROR,可以在一個<filter-mapping>元素中添加任意數目的<dispatcher>,使得filter將會作用於直接從客戶端發過來的request,通過forward過來的request,通過include過來的request,和通過<error-page>過來的request,如果沒指定任何<dispatcher>則默認爲REQUEST  
  78. 說明2:jsp本身就是一個擴展後的servlet,所以它所具有的標籤都是採用servlet機制的,struts1支持,因爲它本身就是採用servlet的api開發的。但struts2不支持,因爲它是採用filter開發的,所以在jsp中使用forward方式的請求跳轉不到Action類。需要添加<dispatcher>標籤纔行。   
  79. -->  
  80.     <filter-mapping>  
  81.         <filter-name>struts2</filter-name>  
  82.         <url-pattern>*.action</url-pattern>  
  83.         <dispatcher>REQUEST</dispatcher>      
  84.         <dispatcher>FORWARD</dispatcher>   
  85.     </filter-mapping>  
  86.       
  87.     <!-- Spring 刷新Introspector防止內存泄露 -->  
  88.     <listener>  
  89.         <listener-class>  
  90.             org.springframework.web.util.IntrospectorCleanupListener  
  91.         </listener-class>  
  92.     </listener>  
  93.           
  94.     <!-- session超時定義,單位爲分鐘,清除服務端我們存儲在Session中的對象,不清除Tomcat容器存儲在Session中的對象 -->  
  95.     <session-config>  
  96.         <session-timeout>30</session-timeout>  
  97.     </session-config>  
  98.     <!-- 自動對賬監聽器 -->  
  99.     <listener>  
  100.         <listener-class>com.suptools.timer.TimerListener</listener-class>  
  101.     </listener>  
  102.     <!-- 默認首頁定義 -->  
  103.     <welcome-file-list>  
  104.         <welcome-file>logon.jsp</welcome-file>  
  105.     </welcome-file-list>  
  106. </web-app>  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章