Changing Context Path Of Web Application In Tomcat Server

Introduction

Here I am going to show you how to change context path of web application in Tomcat server. So, basically web applications are deployed into Tomcat server and meaningful, memorable context paths are assigned for web applications. Even if you do not want to set any context path for an application, you can do so.

For every web application there should be a context path by which you can uniquely access its URL in the browser. To change context path for an application you can directly do so in the server.xml file which is located under conf folder of the Tomcat server installation path or you can do so in the GUI (Graphical User Interface) while uploading a war file and deploying through GUI.

If you do not specify any context path then the context path is same as the war file name. The war file generally put under the webapps folder of the Tomcat installation directory. If you are using startup.bat/startup.sh file to start Tomcat server then, during server startup using startup.bat/startup.sh file, the war file gets deployed into the server. So, in this case war name becomes the context path.

Context Path

Let’s say I have a simple web application with a simple JSP page (index.jsp):

<html>
<body>
	<h2>Simple App</h2>
</body>
</html>

And deployment descriptor (web.xml) file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="4.0"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
   http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd">

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

</web-app>

After generating war file (web-app.war), the war file is kept under webapps folder. The server is started using startup.bat (Windows OS) file, so the war file gets deployed. The war file also gets unpacked during deployment.

Once server is up and running. You can access the Tomcat server page at http://localhost:8080 and you can access the web application at http://localhost:8080/web-app address.

context path tomcat server

If you want to access Server Status, Manager App, Host Manager, you need to have user(s) with proper roles.

tomcat context path

If you do not want to manage using GUI then you can do so by updating the server.xml file.

Let’s say I want to access my web-app at http://localhost:8080 instead of http://localhost:8080/web-app address. So, I need to add the following line into server.xml file under the tag <Host> ... </Host>.

<Context docBase="${catalina.base}\webapps\web-app" path="" reloadable="true"/>

web-app is the war file or web application’s name, context path is set using path="", reloadable="true" because I want to reload the new context path.

The <Host/> tag may look like similar to the below:

<Host name="localhost"  appBase="webapps"
	unpackWARs="true" autoDeploy="true">
	
	<Context docBase="${catalina.base}\webapps\web-app" path="" reloadable="true"/>

	...

</Host>

Now you can access the app using http://localhost:8080:

context path of web app

If you want to change the context path to app then use the following line:

<Context docBase="${catalina.base}\webapps\web-app" path="app" reloadable="true"/>
web app context path in tomcat

Remember after each change you must restart your Tomcat server.

You can also deploy war file using the following section, you can mention context path during deployment. Later you stop and start the application using this GUI. You can even undeploy the application.

deployment in tomcat

If you are deploying by uploading the war file then you don’t have option to change the context path in the GUI but you can change using server.xml file.

If you are deploying by specifying the war file path then you can specify the context path as shown below:

change context path tomcat

You will also get success message in the GUI:

tomcat context path

Now you can access application at address http://localhost:8080/web.

tomcat server deploy web app

Now you can manage your app using GUI:

manage app using tomcat gui

That’s all about changing context path of web application in Tomcat server.

Source Code

You can download sample application.

Leave a Reply

Your email address will not be published.