Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How web.xml is parsed

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how web.xml is parsed". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how web.xml is parsed.

The conf/web.xml file is a global configuration file that configures some common properties, such as MIME type, session timeout, and so on.

And this file will eventually come together with the application of your own web.xml file merge. This is the cause of the problem: the information about the servlet class of your individualized configuration is not included in every application, so it is normal that it cannot be loaded.

After explaining the basic reasons, let's take a look at how the web.xml file is parsed internally by Tomcat.

Parse the web.xml file code, located in the ContextConfig class.

The comments of the parsing code illustrate the whole function in a strategically advantageous position.

/ * *

* 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.

, /

During the merge process, the custom configuration overrides the global configuration.

Let's look at the main code parsing.

Set defaults = new HashSet ()

Defaults.add (getDefaultWebXmlFragment ()); / / first, the default web.xml

WebXml webXml = createWebXml ()

/ / Parse context level web.xml

InputSource contextWebXml = getContextWebXmlSource ()

If (! webXmlParser.parseWebXml (contextWebXml, webXml, false) {

Ok = false

}

Then there is the web-fragement feature in the new features of Servlet3. You need to first parse whether the existing jar package contains a custom configuration

/ / 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 fragments = processJarsForWebFragments (webXml)

/ / Step 2. Order the fragments.

Set orderedFragments = null

OrderedFragments =

WebXml.orderWebFragments (webXml, fragments, sContext)

Then web-fragement.xml and web.xml are put together.

/ / Step 7. Apply global defaults

/ / Have to merge defaults before JSP conversion since defaults

/ / provide JSP servlet definition.

WebXml.merge (defaults); / / merge global web.xml. Here, it is put in first, and subsequent customizations will be overwritten.

Then go down to the custom web.xml of the merge application.

/ / Step 9. Apply merged web.xml to Context

If (ok) {

ConfigureContext (webXml)

}

The web.xml parsing and intuitive perception of the entire application are basically the same. The parsing code for each declared component, such as Filter and Listener, is as follows:

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)

}

Let's talk about this small part of the session configuration separately:

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 ())

}

In addition to overriding the session timeout in the global configuration file, you can also declare some of the configurations of SessionCookie, which is one of the ways we mentioned in the previous article to use Cookie to maintain Session (background reply keyword 005 check). We see that you can define a series of properties, such as names, domains, and so on.

At this point, I believe you have a deeper understanding of "how web.xml is parsed". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report