Specify context name in Tomcat

When developing web applications that are distributed as WAR files running in Tomcat most of the times you want to be able to specify the context name to a nicer name than Tomcat provides by default. Which is the name of the WAR file itself. When i.e. you are keeping the version within the name of the WAR this will result in some weird name like myapp-1.2. This is most of the times not what you want.

Most of the times you want a nice context name like myapp without the version in it. According to the Tomcat documentation you can do it in 3 ways:
  1. Edit the server.xml
  2. Add a context.xml to the $CATALINA_HOME/conf/Catalina/localhost with the name of your context.
  3. Add a context.xml to your META-INF directory in your WAR file
The first two options just work fine only the last one doesn't do the job for you for some reason.

Personally the second option is the best option since it's not intrusive to your application and also not to Tomcat since you leave the server.xml alone. This way you define the context name in a separate configuration file and enables you to also keep the environment specific configuration out of the application. You can than simply provide this configuration file in a distribution package for installation. Another benefit is that you provide yourself with more flexibility since you can easily switch to EAR file distribution when running on an application server. On an application server you define your context within your EAR file which will only effect your distribution project and not your application.

So why would you put a context.xml in your META-INF directory in your WAR file? Well it can be useful if you just want to expose some application specific JNDI resources for your application.

So how can I achieve this? Here is a link to the Tomcat documentation where you can find out what exactly you should do to achieve this.
http://tomcat.apache.org/tomcat-4.1-doc/jndi-datasource-examples-howto.html


Comments

Popular posts from this blog

Fastest XML parser

Tomcat behind Apache HTTP server using Spring Security

Cargo-itest open source project