日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java web之web.xml配置详解

發布時間:2023/12/4 java 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java web之web.xml配置详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

什么是web.xml

web.xml是web項目的配置文件,一般的web工程都會用到web.xml來配置,方便大型開發。web.xml主要用來配置Filter,Listener,Servlet等。但是web.xml并不是必須的,一個web工程可以沒有web.xml文件。

web工程加載web.xml過程

web容器的加載順序ServletContext -> context-param -> listener -> filter -> servlet。并且這些元素可以配置在文件中的任意位置,不會因為filter在web.xml文件中寫在listener前面就先加載filter。
加載過程順序如下

啟動一個web項目,web容器會讀取它的配置文件web.xml,讀取<listener>和<context-param>兩個結點。
創建一個ServletContext(Servlet上下文),這個web項目的所有部分都將共享這個上下文
容器將<context-param>轉換為鍵值對,并交給ServletContext
容器創建<listener>中的類實例,創建監聽器

web.xml配置詳解

1、schema
web.xml的模式文件是由Sun公司定義的,每個web.xml文件的根元素<web-app>中,都必須標明這個web.xml使用的是哪個模式文件。其他的元素都放在<web-app></web-app>中

<?xml version="1.0" encoding="UTF-8"?> <web-app 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"> </web-app>

2、<display-name>Web應用名稱

用于標記這個特定的Web應用的名稱

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

3、<discription>Web應用描述

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

4、<context-param>上下文參數

聲明應用范圍內的初始化參數。用于向Servlet+Context提供鍵值對,即應用程序上下文信息。后續的listener,filter在初始化時會用到這些上下文信息。在servlet里面可以通過getServletContext().getInitParameter(“context/param”)得到

<context-param><param-name>log4jConfiguration</param-name><param-value>/WEB-INF/log4j2.xml</param-value></context-param>

5、<filter>過濾器
filter可以認為是servlet的一種加強版,主要用于對用戶請求request進行預處理,也可以對response進行后處理,是個典型的處理鏈。使用filter的完整流程是,filter對用戶請求進行預處理,接著將請求HttpServletRequest交給Servlet處理并生成響應。最后Filter再對服務器響應HttpServletResponse進行后處理。Servlet與Servlet具有完全相同的生命周期,而Filter也可以通過進行初始化參數的配置,并通過FilterConfig傳送給filter
Filter的配置就是將此項目與一個實現javax.servlet.Filter接口的類相關聯

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

6、監聽器

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

7、<servlet>

servlet老生常談啦,是運行在服務器端的程序。傳送門

<!-- 基本配置 --> <servlet><servlet-name>snoop</servlet-name><servlet-class>SnoopServlet</servlet-class> </servlet> <servlet-mapping><servlet-name>snoop</servlet-name><url-pattern>/snoop</url-pattern> </servlet-mapping> <!-- 高級配置 --> <servlet><servlet-name>snoop</servlet-name><servlet-class>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> <!-- <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 -->

8、<session-config>會話超時配置

單位為秒

<session-config><session-timeout>120</session-timeout> </session-config>

9、<welcome-file-list>歡迎文件頁

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

關于歡迎頁面:

訪問一個網站時,默認看到的第一個頁面就叫歡迎頁,一般情況下是由首頁來充當歡迎頁的。一般情況下,我們會在web.xml中指定歡迎頁。但web.xml并不是一個Web的必要文件,沒有web.xml,網站仍然是可以正常工作的。只不過網站的功能復雜起來后,web.xml的確有非常大用處,所以,默認創建的動態web工程在WEB-INF文件夾下面都有一個web.xml文件。

當你只指定一個web的根名,沒有指定具體頁面,去訪問時一個web時, 如果web.xml文件中配置了歡迎頁,那么就返回指定的那個頁面作為歡迎頁,而在文中沒有web.xml文件,或雖然有web.xml,但web.xml也沒指定歡迎頁的情況下,那么不同的應用服務器可能會有不同的行為,對于tomcat來說,它默認先查找index.html文件,如果找到了,就把index.html作為歡迎頁還回給瀏覽器。如果沒找到index.html,tomcat就去找index.jsp。找到index.jsp就把它作為歡迎頁面返回。而如果index.html和index.jsp都沒找到,又沒有用web.xml文件指定歡迎頁面,那此時tomcat就不知道該返回哪個文件了,它就顯示The requested resource (/XXX) is not available的頁面。其中XXX表示web的根名。但如果你指定了具體頁面,是可以正常訪問的。(如果web根名下存在index.html和index.jsp,而某些應用服務器在web.xml中沒指定歡迎頁的情況下默認先查找index.jsp的話,其行為跟tomcat就不一樣了,因此可能造成沒配置web.xml歡迎頁的項目,部署到不同的應用服務器看到不一樣的首頁的現象)。

10、<jsp-config>設置jsp

<jsp-config> 包括 <taglib> 和 <jsp-property-group> 兩個子元素。
<jsp-property-group> 元素主要有八個子元素,它們分別為:

<description>:設定的說明 <display-name>:設定名稱 <url-pattern>:設定值所影響的范圍,如: /CH2 或 /*.jsp <el-ignored>:若為 true,表示不支持 EL 語法 <scripting-invalid>:若為 true,表示不支持 <% scripting %>語法 <page-encoding>:設定 JSP 網頁的編碼 <include-prelude>:設置 JSP 網頁的抬頭,擴展名為 .jspf <include-coda>:設置 JSP 網頁的結尾,擴展名為 .jspf <jsp-config><taglib><taglib-uri>Taglib</taglib-uri><taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location></taglib><jsp-property-group><description>Special property group for JSP Configuration JSP example.</description><display-name>JSPConfiguration</display-name><url-pattern>/jsp/* </url-pattern><el-ignored>true</el-ignored><page-encoding>GB2312</page-encoding><scripting-invalid>true</scripting-invalid><include-prelude>/include/prelude.jspf</include-prelude><include-coda>/include/coda.jspf</include-coda></jsp-property-group> </jsp-config>

對于Web 應用程式來說,Scriptlet 是個不樂意被見到的東西,因為它會使得HTML 與Java 程式碼交相混雜,對于程式的維護來說相當的麻煩,必要的時候,可以在web.xml 中加上 標簽,設定所有的JSP 網頁都不可以使用Scriptlet。

11、指定錯誤處理頁面,可以通過“異常類型”或“錯誤碼”來指定錯誤處理頁面。

<error-page><error-code>404</error-code><location>/error404.jsp</location> </error-page><error-page><exception-type>java.lang.Exception<exception-type><location>/exception.jsp<location> </error-page>

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

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

13、MIME類型配置

<mime-mapping> <extension>htm</extension> <mime-type>text/html</mime-type> </mime-mapping>

14、TLD配置

<taglib> <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri> <taglib-location>/WEB-INF/jsp/debug-taglib.tld</taglib-location> </taglib> 如果MyEclipse一直在報錯,應該把<taglib> 放到 <jsp-config><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>

15、資源管理對象配置

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

16、資源工廠配置

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

17、安全限制配置

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

18、登陸驗證配置

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

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

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

20、配置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>

總結

以上是生活随笔為你收集整理的Java web之web.xml配置详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。