Sunday 9 July 2017

Change context root of a dynamic web project in Eclipse

In this post, I am going to explain how to change the context root of a dynamic project that deployed in tomcat server.

For example, I created new dynamic web project, ‘HelloWorld’ and deployed in tomcat. By default, my project name is the context name.



I am going to show you two approaches to change the context root.
a.   By directly updating the server.xml file of tomcat
b.   By using Eclipse IDE

Since I deployed the application using Eclipse, my server.xml is located in below location.
<ECLIPSE_WORKSPACE>\Servers\Tomcat v7.0 Server at localhost-config

Approach1: By directly updating the server.xml file of tomcat
When you deploy the ‘HelloWorld’ application in tomcat, there is an entry like below is created in server.xml file of tomcat.

<Context docBase="HelloWorld" path="/HelloWorld" reloadable="true" source="org.eclipse.jst.jee.server:HelloWorld"/>

The Context element represents a web application.

Below table summarizes the attributes associated with the Context element.

Attribute
Description
docBase
Pathname to the web application archive file. You may specify an absolute pathname for this directory or WAR file, or a pathname that is relative to the appBase directory of the owning Host
path
The context path of this web application, which is matched against the beginning of each request URI to select the appropriate web application for processing.
reloadable
Set to true, if you want to reload the changes automatically.

Now, open the ‘server.xml’ file, change the value of the attribute ‘path’ to whatever the name you want to give to the context.

For example, I given the name to ‘myDemoApp’.

<Context docBase="HelloWorld" path="/myDemoApp" reloadable="true" source="org.eclipse.jst.jee.server:HelloWorld"/>

Stop the tomcat server.
Start the server


Now hit the url ‘http://localhost:8080/myDemoApp/’, you can able to see below message.


Approach2: By using Eclipse IDE
Right click on the project -> Properties

Go to ‘Web project settings’ and change the context root name to the name that you want.

Apply the settings.

It opens below window. Press Ok.

Clean the server. Right click on the server -> Clean.

Press the button Yes.

Now start the application on server and hit the below url.

You can able to see below window again.


You can open the server.xml file and see the below Context element.

<Context docBase="HelloWorld" path="/testDemo" reloadable="true" source="org.eclipse.jst.jee.server:HelloWorld"/>


Previous                                                    Next                                                    Home

No comments:

Post a Comment