Detailed explanation of setting Context Path in Web application

Detailed explanation of setting Context Path in Web application

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

Jetty

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

warName.war

Without XML IoC file:

  • If the WAR file name is myapp.war, then the context path is: /myapp;
  • If the WAR file name is ROOT.war, then 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

Individual <context> elements can be defined in a number of ways:

  • Defined in the /META-INF/context.xml file of the web application. When the copyXML attribute of the Host is true, the context.xml file will be copied to the $CATALINA_BASE/conf/[enginename]/[hostname] directory and renamed. The naming convention refers to the description of the /META-INF/context.xml section.
  • Place an XML file directly in the $CATALINA_BASE/conf/[enginename]/[hostname] directory. The file name specification and content refer to the description of the META-INF/context.xml section. This file has a higher priority than /META-INF/context.xml.
  • Define a <Context> element in the <Host> element of the Tomcat server's conf/server.xml file.

META-INF/context.xml

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

  • If there is no contextversion, then the context name and context path are the same;
  • 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 has been removed and the / character has been replaced with 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
Empty string 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
Empty string 42 ##42 ROOT##42 ROOT##42.xml, ROOT##42.war, ROOT##42

Server.xml

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

Default context.xml

Tomcat has a default context element, which applies to all applications, but its priority is also the lowest, and all configurations can be overwritten by the application.

There are two default context.xml files:

  • One is $CATALINA_BASE/conf/context.xml. The information in this context element will be loaded by all applications.
  • One is $CATELINA_BASE/conf/[enginename]/[hostname]/context.xml.default. The information in this <context> element will be loaded by all applications in this virtual host.

This is the end of this article about setting Context Path in Web Applications. For more information about setting Context Path in Web Applications, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Springcloud gateway sets context-path operation
  • Analyze the problem of Spring Boot embedded tomcat about getServletContext().getRealPath to obtain the temporary path
  • Detailed explanation of the use of server.servlet.context-path in Springboot 2.x
  • ${pageContext.request.contextPath} value problem and solution in JavaScript

<<:  Teach you how to build the vue3.0 project architecture step by step

>>:  MySQL series 6 users and authorization

Recommend

Summary of MySql import and export methods using mysqldump

Export database data: First open cmd and enter th...

Uniapp uses Baidu Voice to realize the function of converting recording to text

After three days of encountering various difficul...

Detailed installation and configuration tutorial of MySQL 5.7 under Win10

1. Unzip MySQL 5.7 2. Create a new configuration ...

Solution to the lack of my.ini file in MySQL 5.7

What is my.ini? my.ini is the configuration file ...

Tutorial on installing MySQL 5.7.28 on CentOS 6.2 (mysql notes)

1. Environmental Preparation 1.MySQL installation...

Command to remove (delete) symbolic link in Linux

You may sometimes need to create or delete symbol...

How to copy MySQL table

Table of contents 1.mysqldump Execution process: ...

Detailed explanation of nginx upstream configuration and function

Configuration Example upstream backend { server b...

Detailed explanation of various join summaries of SQL

SQL Left Join, Right Join, Inner Join, and Natura...

JavaScript recursion detailed

Table of contents 1. What is recursion? 2. Solve ...

Several ways to solve the 1px border problem on mobile devices (5 methods)

This article introduces 5 ways to solve the 1px b...

Detailed steps to install python3.7 on CentOS6.5

1. Download Python 3 wget https://www.python.org/...

Analysis and treatment of scroll bars in both HTML and embedded Flash

We often encounter this situation when doing devel...

js to realize payment countdown and return to the home page

Payment countdown to return to the home page case...