Java web之web.xml配置详解
什么是web.xml
web.xml是web項目的配置文件,一般的web工程都會用到web.xml來配置,方便大型開發(fā)。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>兩個結(jié)點。
創(chuàng)建一個ServletContext(Servlet上下文),這個web項目的所有部分都將共享這個上下文
容器將<context-param>轉(zhuǎn)換為鍵值對,并交給ServletContext
容器創(chuàng)建<listener>中的類實例,創(chuàng)建監(jiān)聽器
web.xml配置詳解
1、schema
web.xml的模式文件是由Sun公司定義的,每個web.xml文件的根元素<web-app>中,都必須標(biāo)明這個web.xml使用的是哪個模式文件。其他的元素都放在<web-app></web-app>中
2、<display-name>Web應(yīng)用名稱
用于標(biāo)記這個特定的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提供鍵值對,即應(yīng)用程序上下文信息。后續(xù)的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可以認(rèn)為是servlet的一種加強(qiáng)版,主要用于對用戶請求request進(jìn)行預(yù)處理,也可以對response進(jìn)行后處理,是個典型的處理鏈。使用filter的完整流程是,filter對用戶請求進(jìn)行預(yù)處理,接著將請求HttpServletRequest交給Servlet處理并生成響應(yīng)。最后Filter再對服務(wù)器響應(yīng)HttpServletResponse進(jìn)行后處理。Servlet與Servlet具有完全相同的生命周期,而Filter也可以通過進(jìn)行初始化參數(shù)的配置,并通過FilterConfig傳送給filter
Filter的配置就是將此項目與一個實現(xiàn)javax.servlet.Filter接口的類相關(guān)聯(lián)
6、監(jiān)聽器
<listener> <listerner-class>com.listener.SessionListener</listener-class> </listener>7、<servlet>
servlet老生常談啦,是運行在服務(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> <!-- 高級配置 --> <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網(wǎng)頁的完整路徑 <init-param></init-param> 用來定義參數(shù),可有多個init-param。在servlet類中通過getInitParamenter(String name)方法訪問初始化參數(shù) <load-on-startup></load-on-startup>指定當(dāng)Web應(yīng)用啟動時,裝載Servlet的次序。當(dāng)值為正數(shù)或零時:Servlet容器先加載數(shù)值小的servlet,再依次加載其他數(shù)值大的servlet。當(dāng)值為負(fù)或未定義:Servlet容器將在Web客戶首次訪問這個servlet時加載它。 <servlet-mapping></servlet-mapping> 用來定義servlet所對應(yīng)的URL,包含兩個子元素 <servlet-name></servlet-name> 指定servlet的名稱 <url-pattern></url-pattern> 指定servlet所對應(yīng)的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>關(guān)于歡迎頁面:
訪問一個網(wǎng)站時,默認(rèn)看到的第一個頁面就叫歡迎頁,一般情況下是由首頁來充當(dāng)歡迎頁的。一般情況下,我們會在web.xml中指定歡迎頁。但web.xml并不是一個Web的必要文件,沒有web.xml,網(wǎng)站仍然是可以正常工作的。只不過網(wǎng)站的功能復(fù)雜起來后,web.xml的確有非常大用處,所以,默認(rèn)創(chuàng)建的動態(tài)web工程在WEB-INF文件夾下面都有一個web.xml文件。
當(dāng)你只指定一個web的根名,沒有指定具體頁面,去訪問時一個web時, 如果web.xml文件中配置了歡迎頁,那么就返回指定的那個頁面作為歡迎頁,而在文中沒有web.xml文件,或雖然有web.xml,但web.xml也沒指定歡迎頁的情況下,那么不同的應(yīng)用服務(wù)器可能會有不同的行為,對于tomcat來說,它默認(rèn)先查找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,而某些應(yīng)用服務(wù)器在web.xml中沒指定歡迎頁的情況下默認(rèn)先查找index.jsp的話,其行為跟tomcat就不一樣了,因此可能造成沒配置web.xml歡迎頁的項目,部署到不同的應(yīng)用服務(wù)器看到不一樣的首頁的現(xiàn)象)。
10、<jsp-config>設(shè)置jsp
<jsp-config> 包括 <taglib> 和 <jsp-property-group> 兩個子元素。
<jsp-property-group> 元素主要有八個子元素,它們分別為:
對于Web 應(yīng)用程式來說,Scriptlet 是個不樂意被見到的東西,因為它會使得HTML 與Java 程式碼交相混雜,對于程式的維護(hù)來說相當(dāng)?shù)穆闊?#xff0c;必要的時候,可以在web.xml 中加上 標(biāo)簽,設(shè)定所有的JSP 網(wǎng)頁都不可以使用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應(yīng)用圖標(biāo):指出IDE和GUI工具用來表示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一直在報錯,應(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、資源管理對象配置
<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ù)庫連接池就可在此配置:
<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元素給出安全角色的一個列表,這些角色將出現(xiàn)在servlet元素內(nèi)的security-role-ref元素的role-name子元素中。
分別地聲明角色可使高級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配置详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 长城汽车公布广州车展参展阵容,坦克 70
- 下一篇: 倍思 MagPro 磁吸无线充 15W