Posted by: ranvijay | March 9, 2010

tomcat-context path different from the war name

Sometimes a simple looking thing takes a lot of time. I knew that a web application can be configured in such a way that it is callable using any custom context path, and not necessarily it’s war name, but I was finding it difficult to configure the context path correctly. For example, if I have a war named ‘test.war’, then I can call it using http://localhost:8080/test, but what if I want to call it using http://localhost:8080/mytest? This can be helpful in cases when we have a war named test-1.2.0-alpha.war but I don’t want to call my application using such a long context path. How can I call it simply by invoking http://localhost:8080/test. So here is how to do it:

1. Since modifying the conf/server.xml is not the recommended way anymore, create a context file mytest.xml in ${TOMCAT_HOME}/conf/Catalina/localhost directory. Note the file name of the context file, it has to be same as the desired context path value.

2. Add the context entry in the context file created in step 1 above.

<Context path=”/sometest” docBase=”/opt/apache-tomcat-6.0.24/test”>

Point to note here is that the ‘path’ attribute is not picked up by tomcat. You can give any value to it, it won’t matter. You can even skip it. But I kept it intentionally to prove that the application is not accessible on http://localhost:8080/sometest, but only on http://localhost:8080/mytest

3. Third and final point, and it took me some time to figure it out. Keep you war or the exloded-war directory in any location except ${TOMCAT_HOME}/webapps. If you will keep the war in the ${TOMCAT_HOME}/webapps, the application will only be available on the context path equal to the war name i.e. http://localhost:8080/test. So, in my case, I have put the exploded war directory at /opt/apache-tomcat-6.0.24/test.war, but you can choose any other directory as per your convenience.

Advertisement

Responses

  1. I tried this solution, but it didn’t work for me.
    Is necessary to restart the server? If so, is there another wary which doesn’t require the restart?
    Thank’s.

    • @Rogerio, can you please explain me a bit more about your problem. What did you try and what error you got. If possible, can you please explain your scenario and give me the context file you created.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Categories

Follow

Get every new post delivered to your Inbox.