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

Web form creation skills

In fact, the three tables above all have three ro...

Causes and solutions for slow MySQL queries

There are many reasons for slow query speed, the ...

The concept of MySQL tablespace fragmentation and solutions to related problems

Table of contents background What is tablespace f...

js to realize a simple disc clock

This article shares the specific code of js to im...

In-depth understanding of the matching logic of Server and Location in Nginx

Server matching logic When Nginx decides which se...

Zabbix implements monitoring of multiple mysql processes

Three MySQL instance processes are started on one...

Tomcat components illustrate the architectural evolution of a web server

1. Who is tomcat? 2. What can tomcat do? Tomcat i...

How MySQL handles implicit default values

Some students said that they encountered the prob...

How to manually install MySQL 5.7 on CentOS 7.4

MySQL database is widely used, especially for JAV...

Example code for CSS pseudo-classes to modify input selection style

Note: This table is quoted from the W3School tuto...

How to avoid duplication of data when inserting in MySql batch

Table of contents Preface 1. insert ignore into 2...

Modify the style of HTML body in JS

Table of contents 1. Original Definition 2. JS op...