Java web之web.xml配置详解
什么是web.xml
web.xml是web項(xiàng)目的配置文件,一般的web工程都會(huì)用到web.xml來(lái)配置,方便大型開(kāi)發(fā)。web.xml主要用來(lái)配置Filter,Listener,Servlet等。但是web.xml并不是必須的,一個(gè)web工程可以沒(méi)有web.xml文件。
web工程加載web.xml過(guò)程
web容器的加載順序ServletContext -> context-param -> listener -> filter -> servlet。并且這些元素可以配置在文件中的任意位置,不會(huì)因?yàn)閒ilter在web.xml文件中寫在listener前面就先加載filter。
加載過(guò)程順序如下
啟動(dòng)一個(gè)web項(xiàng)目,web容器會(huì)讀取它的配置文件web.xml,讀取<listener>和<context-param>兩個(gè)結(jié)點(diǎn)。
創(chuàng)建一個(gè)ServletContext(Servlet上下文),這個(gè)web項(xiàng)目的所有部分都將共享這個(gè)上下文
容器將<context-param>轉(zhuǎn)換為鍵值對(duì),并交給ServletContext
容器創(chuàng)建<listener>中的類實(shí)例,創(chuàng)建監(jiān)聽(tīng)器
web.xml配置詳解
1、schema
web.xml的模式文件是由Sun公司定義的,每個(gè)web.xml文件的根元素<web-app>中,都必須標(biāo)明這個(gè)web.xml使用的是哪個(gè)模式文件。其他的元素都放在<web-app></web-app>中
2、<display-name>Web應(yīng)用名稱
用于標(biāo)記這個(gè)特定的Web應(yīng)用的名稱
<display-name>Tomcat Example</display-name>3、<discription>Web應(yīng)用描述
<disciption>Tomcat Example servlets and JSP pages.</disciption>4、<context-param>上下文參數(shù)
聲明應(yīng)用范圍內(nèi)的初始化參數(shù)。用于向Servlet+Context提供鍵值對(duì),即應(yīng)用程序上下文信息。后續(xù)的listener,filter在初始化時(shí)會(huì)用到這些上下文信息。在servlet里面可以通過(guò)getServletContext().getInitParameter(“context/param”)得到
<context-param><param-name>log4jConfiguration</param-name><param-value>/WEB-INF/log4j2.xml</param-value></context-param>5、<filter>過(guò)濾器
filter可以認(rèn)為是servlet的一種加強(qiáng)版,主要用于對(duì)用戶請(qǐng)求request進(jìn)行預(yù)處理,也可以對(duì)response進(jìn)行后處理,是個(gè)典型的處理鏈。使用filter的完整流程是,filter對(duì)用戶請(qǐng)求進(jìn)行預(yù)處理,接著將請(qǐng)求HttpServletRequest交給Servlet處理并生成響應(yīng)。最后Filter再對(duì)服務(wù)器響應(yīng)HttpServletResponse進(jìn)行后處理。Servlet與Servlet具有完全相同的生命周期,而Filter也可以通過(guò)進(jìn)行初始化參數(shù)的配置,并通過(guò)FilterConfig傳送給filter
Filter的配置就是將此項(xiàng)目與一個(gè)實(shí)現(xiàn)javax.servlet.Filter接口的類相關(guān)聯(lián)
6、監(jiān)聽(tīng)器
<listener> <listerner-class>com.listener.SessionListener</listener-class> </listener>7、<servlet>
servlet老生常談啦,是運(yùn)行在服務(wù)器端的程序。傳送門
<!-- 基本配置 --> <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> <!-- 高級(jí)配置 --> <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站臺(tái)中的某個(gè)JSP網(wǎng)頁(yè)的完整路徑 <init-param></init-param> 用來(lái)定義參數(shù),可有多個(gè)init-param。在servlet類中通過(guò)getInitParamenter(String name)方法訪問(wèn)初始化參數(shù) <load-on-startup></load-on-startup>指定當(dāng)Web應(yīng)用啟動(dòng)時(shí),裝載Servlet的次序。當(dāng)值為正數(shù)或零時(shí):Servlet容器先加載數(shù)值小的servlet,再依次加載其他數(shù)值大的servlet。當(dāng)值為負(fù)或未定義:Servlet容器將在Web客戶首次訪問(wèn)這個(gè)servlet時(shí)加載它。 <servlet-mapping></servlet-mapping> 用來(lái)定義servlet所對(duì)應(yīng)的URL,包含兩個(gè)子元素 <servlet-name></servlet-name> 指定servlet的名稱 <url-pattern></url-pattern> 指定servlet所對(duì)應(yīng)的URL -->8、<session-config>會(huì)話超時(shí)配置
單位為秒
<session-config><session-timeout>120</session-timeout> </session-config>9、<welcome-file-list>歡迎文件頁(yè)
<welcome-file-list><welcome-file>index.jsp</welcome-file><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file> </welcome-file-list>關(guān)于歡迎頁(yè)面:
訪問(wèn)一個(gè)網(wǎng)站時(shí),默認(rèn)看到的第一個(gè)頁(yè)面就叫歡迎頁(yè),一般情況下是由首頁(yè)來(lái)充當(dāng)歡迎頁(yè)的。一般情況下,我們會(huì)在web.xml中指定歡迎頁(yè)。但web.xml并不是一個(gè)Web的必要文件,沒(méi)有web.xml,網(wǎng)站仍然是可以正常工作的。只不過(guò)網(wǎng)站的功能復(fù)雜起來(lái)后,web.xml的確有非常大用處,所以,默認(rèn)創(chuàng)建的動(dòng)態(tài)web工程在WEB-INF文件夾下面都有一個(gè)web.xml文件。
當(dāng)你只指定一個(gè)web的根名,沒(méi)有指定具體頁(yè)面,去訪問(wèn)時(shí)一個(gè)web時(shí), 如果web.xml文件中配置了歡迎頁(yè),那么就返回指定的那個(gè)頁(yè)面作為歡迎頁(yè),而在文中沒(méi)有web.xml文件,或雖然有web.xml,但web.xml也沒(méi)指定歡迎頁(yè)的情況下,那么不同的應(yīng)用服務(wù)器可能會(huì)有不同的行為,對(duì)于tomcat來(lái)說(shuō),它默認(rèn)先查找index.html文件,如果找到了,就把index.html作為歡迎頁(yè)還回給瀏覽器。如果沒(méi)找到index.html,tomcat就去找index.jsp。找到index.jsp就把它作為歡迎頁(yè)面返回。而如果index.html和index.jsp都沒(méi)找到,又沒(méi)有用web.xml文件指定歡迎頁(yè)面,那此時(shí)tomcat就不知道該返回哪個(gè)文件了,它就顯示The requested resource (/XXX) is not available的頁(yè)面。其中XXX表示web的根名。但如果你指定了具體頁(yè)面,是可以正常訪問(wèn)的。(如果web根名下存在index.html和index.jsp,而某些應(yīng)用服務(wù)器在web.xml中沒(méi)指定歡迎頁(yè)的情況下默認(rèn)先查找index.jsp的話,其行為跟tomcat就不一樣了,因此可能造成沒(méi)配置web.xml歡迎頁(yè)的項(xiàng)目,部署到不同的應(yīng)用服務(wù)器看到不一樣的首頁(yè)的現(xiàn)象)。
10、<jsp-config>設(shè)置jsp
<jsp-config> 包括 <taglib> 和 <jsp-property-group> 兩個(gè)子元素。
<jsp-property-group> 元素主要有八個(gè)子元素,它們分別為:
對(duì)于Web 應(yīng)用程式來(lái)說(shuō),Scriptlet 是個(gè)不樂(lè)意被見(jiàn)到的東西,因?yàn)樗鼤?huì)使得HTML 與Java 程式碼交相混雜,對(duì)于程式的維護(hù)來(lái)說(shuō)相當(dāng)?shù)穆闊?#xff0c;必要的時(shí)候,可以在web.xml 中加上 標(biāo)簽,設(shè)定所有的JSP 網(wǎng)頁(yè)都不可以使用Scriptlet。
11、指定錯(cuò)誤處理頁(yè)面,可以通過(guò)“異常類型”或“錯(cuò)誤碼”來(lái)指定錯(cuò)誤處理頁(yè)面。
<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應(yīng)用圖標(biāo):指出IDE和GUI工具用來(lái)表示W(wǎng)eb應(yīng)用的大圖標(biāo)和小圖標(biāo)
<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一直在報(bào)錯(cuò),應(yīng)該把<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、資源管理對(duì)象配置
<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>配置數(shù)據(jù)庫(kù)連接池就可在此配置:
<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、登陸驗(yàn)證配置
<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元素給出安全角色的一個(gè)列表,這些角色將出現(xiàn)在servlet元素內(nèi)的security-role-ref元素的role-name子元素中。
分別地聲明角色可使高級(jí)IDE處理安全信息更為容易。
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>總結(jié)
以上是生活随笔為你收集整理的Java web之web.xml配置详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 长城汽车公布广州车展参展阵容,坦克 70
- 下一篇: Java8新特性解析