Detailed explanation of setting Context Path case in Web application

  • 2021-11-01 05:50:06
  • OfStack

URL: http://hostname.com/contextPath/servletPath/pathInfo

Jetty

If there is no contextPath, the root context is used by default, and the path of the root context is "/".

warName.war

Without the XML IoC file:

If the WAR file name is myapp. war, the context path is:/myapp; If the WAR file name is ROOT. war, the context path is:/; If the WAR file name is ROOT-foobar. war, then the context path is/and the virtual host is foobar.

WEB-INF/jetty-web.xml


<Configureclass="org.eclipse.jetty.webapp.WebAppContext">
  <Setname="contextPath">/test</Set>
  ...
</Configure>

Tomcat

Separate < context > Elements can be defined in several ways:

Defined in the/META-INF/context. xml file of the

application, when the copyXML attribute of Host is true, the context. xml file will be copied to the directory $CATALINA_BASE/conf/[enginename]/[hostname] and renamed, referring to the description in the section /META-INF/context. xml for the naming specification. Place an XML file directly under the $CATALINA_BASE/conf/ [enginename]/[hostname] directory, with the file name specification and contents described in the section META-INF/context. xml. This file has a higher priority than the/META-INF/context. xml. Of the conf/server. xml file on the Tomcat server < Host > A is defined in the element < Context > Element.

META-INF/context.xml

There is a close relationship between Context name, context path, contextversion and base file name:

If there is no contextversion, then context name and context path are one sample; If contextpath is an empty string, base name is ROOT; If contextpath is not an empty string, base name = context path, where the first/character is removed and the/character is replaced by the # character.

For example:

Context Path Context Version Context Name Base File Name Example File Names (.xml, .war & directory)
/foo None /foo foo foo.xml, foo.war, foo
/foo/bar None /foo/bar foo#bar foo#bar.xml, foo#bar.war, foo#bar
空字符串 None Empty String ROOT ROOT.xml, ROOT.war, ROOT
/foo 42 /foo##42 foo##42 foo##42.xml, foo##42.war, foo##42
/foo/bar 42 /foo/bar##42 foo#bar##42 foo#bar##42.xml, foo#bar##42.war, foo#bar##42
空字符串 42 ##42 ROOT##42 ROOT##42.xml, ROOT##42.war, ROOT##42

Server.xml

It is not recommended to add in the conf/server. xml file < context > Element, because this file can only be reloaded by restarting Tomcat, which affects the normal use of other WebAPP.

Default context. xml

Tomcat has the default context element, and this context is suitable for all applications, but it has the lowest priority, and all configurations can be completely overridden by the application.

There are two default context. xml

One is $CATALINA_BASE/conf/context. xml, and the information in this context element will be loaded by all applications;   One is $CATELINA_BASE/conf/[ enginename]/[hostname]/context. xml. default, and this < context > Element will be loaded by all applications in this virtual Host.

Related articles: