本篇內(nèi)容主要講解“web.xml是如何被解析的”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“web.xml是如何被解析的”吧!
創(chuàng)新互聯(lián)公司從2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元潁州做網(wǎng)站,已為上家服務(wù),為潁州各地企業(yè)和個人服務(wù),聯(lián)系電話:028-86922220
conf/web.xml這個文件是一個全局配置文件,配置的是一些通用的屬性,類似于MIME類型,session超時(shí)時(shí)間等信息。
而這個文件,最終會和應(yīng)用自己的web.xml文件merge到一起的。這也就是問題產(chǎn)生的原因:你各性化配置的servlet類的信息并不會在每個應(yīng)用下都包含,所以加載不到是正常現(xiàn)象。
基本原因說明了之后,我們來看Tomcat內(nèi)部是如何解析web.xml文件的。
解析web.xml文件代碼,位于ContextConfig類內(nèi)。
解析代碼的注釋高屋建瓴的說明了整個功能
/**
* Scan the web.xml files that apply to the web application and merge them
* using the rules defined in the spec. For the global web.xml files,
* where there is duplicate configuration, the most specific level wins. ie
* an application's web.xml takes precedence over the host level or global
* web.xml file.
*/
在merge的過程中,自定義的配置覆蓋全局配置。
我們來看主要代碼解析
Set<WebXml> defaults = new HashSet<>();
defaults.add(getDefaultWebXmlFragment()); //首先是默認(rèn)web.xml
WebXml webXml = createWebXml();
// Parse context level web.xml
InputSource contextWebXml = getContextWebXmlSource();
if (!webXmlParser.parseWebXml(contextWebXml, webXml, false)) {
ok = false;
}
再之后是Servlet3的新特性中的web-fragement特性。需要先解析已有jar包中是否包含自定義配置
// Ordering is important here
// Step 1. Identify all the JARs packaged with the application and those
// provided by the container. If any of the application JARs have a
// web-fragment.xml it will be parsed at this point. web-fragment.xml
// files are ignored for container provided JARs.
Map<String,WebXml> fragments = processJarsForWebFragments(webXml);
// Step 2. Order the fragments.
Set<WebXml> orderedFragments = null;
orderedFragments =
WebXml.orderWebFragments(webXml, fragments, sContext);
之后則是把web-fragement.xml和web.xml合到一起。
// Step 7. Apply global defaults
// Have to merge defaults before JSP conversion since defaults
// provide JSP servlet definition.
webXml.merge(defaults); //merge全局web.xml,這里是先放進(jìn)去,后續(xù)自定義的會覆蓋
再向下走到merge應(yīng)用自定義的web.xml中。
// Step 9. Apply merged web.xml to Context
if (ok) {
configureContext(webXml);
}
而整個應(yīng)用的web.xml解析和直觀感受基本一致,逐個解析聲明的各個組件,例如Filter和Listener的解析代碼如下:
for (FilterDef filter : webxml.getFilters().values()) {
if (filter.getAsyncSupported() == null) {
filter.setAsyncSupported("false");
}
context.addFilterDef(filter);
}
for (FilterMap filterMap : webxml.getFilterMappings()) {
context.addFilterMap(filterMap);
}
context.setJspConfigDescriptor(webxml.getJspConfigDescriptor());
for (String listener : webxml.getListeners()) {
context.addApplicationListener(listener);
}
單獨(dú)說下session配置的這一小部分:
SessionConfig sessionConfig = webxml.getSessionConfig();
if (sessionConfig != null) {
if (sessionConfig.getSessionTimeout() != null) {
context.setSessionTimeout(
sessionConfig.getSessionTimeout().intValue());
}
SessionCookieConfig scc =
context.getServletContext().getSessionCookieConfig();
scc.setName(sessionConfig.getCookieName());
scc.setDomain(sessionConfig.getCookieDomain());
scc.setPath(sessionConfig.getCookiePath());
scc.setComment(sessionConfig.getCookieComment());
if (sessionConfig.getCookieHttpOnly() != null) {
scc.setHttpOnly(sessionConfig.getCookieHttpOnly().booleanValue());
}
這里除了會覆蓋全局配置文件中的session超時(shí)時(shí)間外,還可以聲明SessionCookie的一些配置,就是我們前面文章提到的使用Cookie來保持Session的一種方式(后臺回復(fù)關(guān)鍵字005查看)。我們看到可以定義名稱,域等一系列屬性。
到此,相信大家對“web.xml是如何被解析的”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
文章名稱:web.xml是如何被解析的
標(biāo)題網(wǎng)址:http://chinadenli.net/article30/ihodso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、云服務(wù)器、網(wǎng)站策劃、電子商務(wù)、網(wǎng)站排名、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)