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

Introduction to the use of HTML element noscript

noscript definition and usage The noscript elemen...

Detailed explanation of js event delegation

1. Each function is an object and occupies memory...

5 things to note when writing React components using hooks

Table of contents 01. Use useState when render is...

Markup language - web application CSS style

Click here to return to the 123WORDPRESS.COM HTML ...

display:grid in CSS3, an introduction to grid layout

1. Grid layout (grid): It divides the web page in...

User needs lead to marketing-oriented design

<br />For each of our topics, the team will ...

Solution to the problem of installing MySQL compressed version zip

There was a problem when installing the compresse...

How to use not in to optimize MySql

Recently, when using select query in a project, I...

MySQL 8.0.16 winx64 installation and configuration method graphic tutorial

I just started learning about databases recently....

IDEA configuration process of Docker

IDEA is the most commonly used development tool f...

Use docker to build kong cluster operation

It is very simple to build a kong cluster under t...

Mysql 5.7.17 winx64 installation tutorial on win7

Software version and platform: MySQL-5.7.17-winx6...

Table setting background image cannot be 100% displayed solution

The following situations were discovered during d...

Summary of tips for making web pages

Preface This article mainly summarizes some of th...