温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

web.xml是如何被解析的

发布时间:2021-12-24 16:02:12 来源:亿速云 阅读:216 作者:iii 栏目:大数据

本篇内容主要讲解“web.xml是如何被解析的”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“web.xml是如何被解析的”吧!

conf/web.xml这个文件是一个全局配置文件,配置的是一些通用的属性,类似于MIME类型,session超时时间等信息。

而这个文件,最终会和应用自己的web.xml文件merge到一起的。这也就是问题产生的原因:你各性化配置的servlet类的信息并不会在每个应用下都包含,所以加载不到是正常现象。

基本原因说明了之后,我们来看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,这里是先放进去,后续自定义的会覆盖

再向下走到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是如何被解析的”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI