您好,登錄后才能下訂單哦!
本篇內容主要講解“web.xml是如何被解析的”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“web.xml是如何被解析的”吧!
conf/web.xml這個文件是一個全局配置文件,配置的是一些通用的屬性,類似于MIME類型,session超時時間等信息。
而這個文件,最終會和應用自己的web.xml文件merge到一起的。這也就是問題產生的原因:你各性化配置的servlet類的信息并不會在每個應用下都包含,所以加載不到是正?,F(xiàn)象。
基本原因說明了之后,我們來看Tomcat內部是如何解析web.xml文件的。
解析web.xml文件代碼,位于ContextConfig類內。
解析代碼的注釋高屋建瓴的說明了整個功能
/**
* 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()); //首先是默認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,這里是先放進去,后續(xù)自定義的會覆蓋
再向下走到merge應用自定義的web.xml中。
// Step 9. Apply merged web.xml to Context
if (ok) {
configureContext(webXml);
}
而整個應用的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);
}
單獨說下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超時時間外,還可以聲明SessionCookie的一些配置,就是我們前面文章提到的使用Cookie來保持Session的一種方式(后臺回復關鍵字005查看)。我們看到可以定義名稱,域等一系列屬性。
到此,相信大家對“web.xml是如何被解析的”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。