淺談JavaWeb中的web.xml配置部署描述符文件
1、 定義頭和根元素
部署描述符文件就像所有XML文件一樣,必須以一個(gè)XML頭開始。這個(gè)頭聲明可以使用的XML版本并給出文件的字符編碼。
DOCYTPE聲明必須立即出現(xiàn)在此頭之后。這個(gè)聲明告訴服務(wù)器適用的servlet規(guī)范的版本(如2.2或2.3)并指定管理此文件其余部分內(nèi)容的語法的DTD(Document Type Definition,文檔類型定義)。
所有部署描述符文件的頂層(根)元素為web-app。請注意,XML元素不像HTML,他們是大小寫敏感的。因此,web-App和WEB-APP都是不合法的,web-app必須用小寫。
2、配置介紹
<discription></discription> 是對站臺的描述
<display-name></display-name> 定義站臺的名稱
distributable><distributable/> 是指定該站臺是否可分布式處理
<context-param></context-param> 用來設(shè)定web站臺的環(huán)境參數(shù),它包含兩個(gè)子元素:
<param-name></param-name> 用來指定參數(shù)的名稱
<param-value></param-value> 用來設(shè)定參數(shù)值
比如:
<context-param>
<param-name>my_param</param-name>
<param-value>hello</param-value>
</context-param>
此設(shè)定的參數(shù),可以在servlet中用 getServletContext().getInitParameter("my_param") 來取得
<filter></filter> 是用來聲明filter的相關(guān)設(shè)定,它包含以下子元素:
<filter-name></filter-name> 這當(dāng)然就是指定filter的名字
<filter-class></filter-class> 這是用來定義filter的類的名稱
<init-param></init-param> 用來定義參數(shù),它有兩個(gè)子元素:
<param-name></param-name> 用來指定參數(shù)的名稱
<param-value></param-value> 用來設(shè)定參數(shù)值
比如:
<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></filter>同時(shí)使用的是<filter-mapping></filter-mapping> 用來定義filter所對應(yīng)的URL,它有兩個(gè)子元素:
<filter-name></filter-name> 指定filter的名字
<url-pattern></url-pattern> 指定filter所對應(yīng)的URL
比如:
<filter-mapping> <filter-name>setCharacterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<listener></listener> 用來設(shè)定Listener接口,它的主要子元素為
<listener-class></listener-class> 定義Listener的類名稱
比如:
<listener> <listener-class>com.myTest.ContextListener</listener-class> </listener>
<servlet></servlet> 用來聲明一個(gè)servlet的數(shù)據(jù),主要有以下子元素:
<servlet-name></servlet-name> 指定servlet的名稱
<servlet-class></servlet-class> 指定servlet的類名稱
<jsp-file></jsp-file> 指定web站臺中的某個(gè)JSP網(wǎng)頁的完整路徑
<init-param></init-param> 用來定義參數(shù),和前面的<init-param>差不多
同樣,與<servlet></servlet>一起使用的是<servlet-mapping></servlet-mapping> 用來定義servlet所對應(yīng)的URL,包含兩個(gè)子元素:
<servlet-name></servlet-name> 指定servlet的名稱
<url-pattern></url-pattern> 指定servlet所對應(yīng)的URL
1、配置servlet路徑
<servlet> <servlet-name>Hello1</servlet-name> <servlet-class>china.hubei.HelloServelt</servlet-class> </servlet> <servlet-mapping> <servlet-name>Hello1</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
2、配置JSP文件路徑
<servlet> <servlet-name>Hello2</servlet-name> <jsp-file>/pages/student.jsp</jsp-file> </servlet> <servlet-mapping> <servlet-name>Hello2</servlet-name> <url-pattern>/nihao.hello</url-pattern> </servlet-mapping>
<session-config></session-config> 用來定義web站臺中的session參數(shù),包含一個(gè)子元素:
<session-timeout></session-timeout> 用來定義這個(gè)web站臺所有session的有效期限,單位為 分鐘
<mime-mapping></mime-mapping> 定義某一個(gè)擴(kuò)展名和某一個(gè)MIME Type做對映,包含兩個(gè)子元素:
<extension></extension> 擴(kuò)展名的名稱
<mime-type></mime-type> MIME格式
比如:
<mime-mapping>
<extension>doc</extension>
<mime-type>application/vnd.ms-word</mime-type>
</mime-mapping>
<mime-mapping>
<extension>xls</extension>
<mime-type>application/vnd.ms-excel</mime-type>
</mime-mapping>
<welcome-file-list></welcom-file-list> 用來定義首頁的列單,包含一個(gè)子元素:
<welcome-file></welcome-file> 指定首頁的文件名稱
比如:
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcom-file-list>
<error-page></error-page> 用來處理錯(cuò)誤代碼或異常的頁面,有三個(gè)子元素:
<error-code></error-code> 指定錯(cuò)誤代碼
<exception-type></exception-type> 指定一個(gè)JAVA異常類型
<location></location> 指定在web站臺內(nèi)的相關(guān)資源路徑
比如:
<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>
<taglib></taglib> 用來設(shè)定JSP網(wǎng)頁所用到的Tag Library路徑,有兩個(gè)子元素:
<taglib-uri></taglib-uri> 定義TLD文件的URI,在JSP網(wǎng)頁中用taglib指令便可取得該URI的 TLD文件
<taglib-location></taglib-location> 指定TLD文件相對于web站臺的存放位置
比如:
<taglib>
<taglib-uri>myTaglib</taglib-uri>
<taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>
</taglib>
<resource-ref></resource-ref> 定義利用JNDI取得站臺可利用的資源,有五個(gè)子元素:
<description></description> 資源說明
<rec-ref-name></rec-ref-name> 資源名稱
<res-type></res-type> 資源種類
<res-auth></res-auth> 資源經(jīng)由Application或Container來許可
<res-sharing-scope></res-sharing-scope> 資源是否可以共享,有Shareable和Unshareable兩個(gè)值,默認(rèn)為Shareable
比如,配置數(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>
<jsp-config> 包括<taglib> 和<jsp-property-group> 兩個(gè)子元素。
其中<taglib>元素在JSP 1.2時(shí)就已經(jīng)存在;而<jsp-property-group>是JSP 2.0 新增的元素。
<jsp-property-group>元素主要有八個(gè)子元素,它們分別為:
1.<description>:設(shè)定的說明;
2.<display-name>:設(shè)定名稱;
3.<url-pattern>:設(shè)定值所影響的范圍,如:/CH2 或 /*.jsp;
4.<el-ignored>:若為true,表示不支持EL 語法;
5.<scripting-invalid>:若為true,表示不支持<% scripting %>語法;
6.<page-encoding>:設(shè)定JSP 網(wǎng)頁的編碼;
7.<include-prelude>:設(shè)置JSP 網(wǎng)頁的抬頭,擴(kuò)展名為.jspf;
8.<include-coda>:設(shè)置JSP 網(wǎng)頁的結(jié)尾,擴(kuò)展名為.jspf。
一個(gè)簡單的<jsp-config>元素完整配置:
<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>
以上這篇淺談JavaWeb中的web.xml配置部署描述符文件就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- JavaWeb項(xiàng)目web.xml中出現(xiàn)Element xxx is not allowed here問題
- Java web.xml之contextConfigLocation作用案例詳解
- JAVA Web.xml加載順序過程詳解
- JavaWeb工程web.xml基本配置過程解析
- web.xml中servlet, bean, filter, listenr 加載順序_動力節(jié)點(diǎn)Java學(xué)院整理
- web.xml詳解_動力節(jié)點(diǎn)Java學(xué)院整理
- JavaWeb中web.xml初始化加載順序詳解
- BUUCTF-easy java WEB-INF/web.xml泄露漏洞及其利用方式
相關(guān)文章
Docker搭建前端Java的開發(fā)環(huán)境詳解
相信每個(gè)人入職第一天就是搭建本地開發(fā)環(huán)境,因?yàn)槲宜居玫氖莏ava,看見了多年不見的eclipse的圖標(biāo)出現(xiàn)我的電腦上,我是難過的。后來知道并不是我一個(gè)人有此感受。這篇文章是為了解決前后端開發(fā)沒有徹底分離的坑,詳細(xì)的給大家介紹了利用Docker搭建前端Java的開發(fā)環(huán)境。2016-10-10

JNDI在JavaEE中的角色_動力節(jié)點(diǎn)Java學(xué)院整理

如何修改HttpServletRequest中header中的信息

springboot實(shí)現(xiàn)全局異常捕獲的使用示例