j2ee之web.xml (Servlet 3.0)配置講解

web.xml :
是j2ee 項目最基本的配置文件,是服務器加載項目的入口,
固然3.0開始也能夠在代碼中中配置項目所須要的一系列參數。
在tomcat conf中存在web.xml,項目中的配置都會繼承和覆蓋該文件的配置。html


基本配置:
1.歡迎文件設置:java

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

這是tomcat 中web.xml的默認配置,在項目中定義會覆蓋之。web

2.錯誤處理設置:spring

 <error-page>
<error-code>404</error-code>
<location>/notFileFound.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.NullPointerException</exception-type>
<location>/null.jsp</location>
</error-page>


若是某文件資源沒有找到,服務器要報404錯誤,按上述配置則會調用\webapps\ROOT\notFileFound.jsp。
若是執行的某個JSP文件產生NullPointException ,則會調用\webapps\ROOT\null.jspsql

3.回話超時設置:數據庫

 <session-config>
<session-timeout>30</session-timeout>
</session-config>

 

設置session 的過時時間,單位是分鐘,
tomcat 默認值是30min.apache

從servlet3.0 開始帶了許多安全性的設置:tomcat

 <session-config> 
    會話鏈接,保持狀態的設置:
    <cookie-config> 
    <secure>true</secure> 
    只容許在SSL下進行交互,默認值爲false.
    <http-only>true</http-only> 確保cookie不能被客戶端腳本訪問。這幫助減輕了一些常見的XSS攻擊,默認值爲false
    <comment></comment> 會話備註
    <domain></domain> 會話保存域
    <max-age></max-age> 會話保存最大時間,默認值:-1
    <name></name> 會話名稱 默認值:JSESSIONID
    <path></path> 會話保存路徑
    
    </cookie-config>
    <tracking-mode>COOKIE</tracking-mode> 
    容許你定義JSESSIONID是存儲在cookie中仍是URL參數中
</session-config>

 


4.過濾器設置:安全

<filter>
    <filter-name>openEntityManagerInViewFilter</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
    <async-supported>true</async-supported>
    </filter>
    <filter-mapping>
    <filter-name>openEntityManagerInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

 
1) 身份驗證的過濾Authentication Filters
2) 日誌和審覈的過濾Logging and Auditing Filters
3) 圖片轉化的過濾Image conversion Filters
4) 數據壓縮的過濾Data compression Filters
5) 加密過濾Encryption Filters
6) Tokenizing Filters
7) 資源訪問事件觸發的過濾Filters that trigger resource access events XSL/T 過濾XSL/T filters
8) 內容類型的過濾Mime-type chain Filter 注意監聽器的順序,如:先安全過濾,而後資源,
 而後內容類型等,這個順序能夠本身定。服務器

tomcat 內置filter:SSIFilter ,SetCharacterEncodingFilter, FailedRequestFilter ..
默認爲開啓。

5.Servlet 管理:

<servlet>
    <servlet-name>default</servlet-name>
    <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
    <init-param>
    <param-name>debug</param-name>
    <param-value>0</param-value>
    </init-param>
    <init-param>
    <param-name>listings</param-name>
    <param-value>false</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>


tomcat 內置servlet
有默認開啓:DefaultServlet,JspServlet,
默認關閉:SSIServlet,CGIServlet


6.應用 圖片表示:

<icon>
    <small-icon>/images/small.gif</small-icon>
    <large-icon>/images/large.gif</large-icon>
</icon>

 

small :16 X 16,large:32 X 32 。 後綴擴展名:.gif或.jpg.

7.應用全局上下文參數

<context-param>
    <param-name>param_name</param-name>
    <param-value>param_value</param-value>
</context-param>

 

8.監聽器listener:

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

 

監聽器負責監聽服務器和應用的各類事件狀態。

tomcat 中內容的監聽器有
AprLifecycleListener,JasperListener,JreMemoryLeakPreventionListener,
GlobalResourcesLifecycleListener,ThreadLocalLeakPreventionListener
這些監聽器都默認配置在server.xml中。


9.媒體映射<mime-mapping>

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

 

在請求靜態資源時,tomcat 會根據後綴名來映射媒體類型自動加上Content-Type 頭。
tomcat 內置包含了不少映射配置,若是須要自定義在web.xml中追加。
備註:若是自定義代碼下載文件必定得加上響應的Content-Type頭。


10.jsp 配置 <jsp-config>
jsp-config元素主要用來設定JSP的相關配置,<jsp:config>包括<taglib>和<jsp-property-group>兩個子元素。

<jsp-config>
    <taglib>
        taglib元素包含兩個子元素taglib-uri和taglib-location.用來設定JSP網頁用到的Tag Library路徑.
        <taglib-uri>URI</taglib-uri>
        taglib-uri定義TLD文件的URI,JSP網頁的taglib指令能夠經由這個URI存取到TLD文件.
        <taglib-location>/WEB-INF/lib/xxx.tld</taglib-laction>
        TLD文件對應Web站臺的存放位置.
    </taglib>
    <jsp-property-group>
        <description>Description</descrition>
        此設定的說明
        <display-name>Name</display-name>
        此設定的名稱
        <url-pattern>URL</url-pattern>
        設定值所影響的範圍,如:/CH2 或者/*.jsp
        <el-ignored>true|false</el-ignored>
        若爲true,表示不支持EL語法.
        <scripting-invalid>true|false</scripting-invalid>
        若爲true表示不支持<%scription%>語法.
        <page-encoding>encoding</page-encoding>
        設定JSP網頁的編碼
        <include-prelude>.jspf</include-prelude>
        設置JSP網頁的擡頭,擴展名爲.jspf
        <include-coda>.jspf</include-coda>
        設置JSP網頁的結尾,擴展名爲.jspf
    </jsp-property-group>
<jsp-config>

十一、資源管理對象配置   

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

    
十二、資源工廠配置   

 <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> 
    <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>

   
   1三、安全限制配置

<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>

    
  1四、登錄驗證配置   

<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>

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

<security-role> 
    <role-name>tomcat</role-name> 
</security-role>

   

1六、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>

 
可參考
http://www.cnblogs.com/JesseV/archive/2009/11/17/1605015.html

tomcat 如何就加載和解析web.xml?

加載順序爲:

context-param->listener->filter->servlet

能夠參考一下
http://www.csdn123.com/html/blogs/20130919/71581.htm
http://blog.csdn.net/insistgogo/article/details/22598001

相關文章
相關標籤/搜索