Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define Servlet Context in WAR-File

How can I tell e.g. Tomcat to use a specific context path when given my WAR-File?

Example: I have a war file created by maven build and the resulting name of the file is rather long. So I do not want the tomcat manager application to use the filename of the war as the context.

Supplying a context.xml in META-INF did not produce the desired results

I also found this in the documentation for the path attribute of Context:

The value of this field must not be set except when statically defining a Context in server.xml, as it will be inferred from the filenames used for either the .xml context file or the docBase.

So it does not seem to be the right way to tell the application-server what the path for my WAR should be.

Any more hints?

like image 771
er4z0r Avatar asked Apr 07 '10 14:04

er4z0r


People also ask

What is the servlet context?

Interface ServletContext. public interface ServletContext. Defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file. There is one context per "web application" per Java Virtual Machine.

What is a context in Tomcat?

What is a Tomcat Context. In Tomcat, the Context Container represents a single web application running within a given instance of Tomcat. A web site is made up of one or more Contexts. For each explicitly configured web application, there should be one context element either in server.

What does .WAR file include?

In software engineering, a WAR file (Web Application Resource or Web application ARchive) is a file used to distribute a collection of JAR-files, JavaServer Pages, Java Servlets, Java classes, XML files, tag libraries, static web pages (HTML and related files) and other resources that together constitute a web ...

What is context in Java web application?

A context parameter provides configuration information needed by a web application. An application can define its own context parameters. In addition, JavaServer Faces technology and Java Servlet technology define context parameters that an application can use.


1 Answers

There are two important points in the the documentation of the Context Container:

  • In individual files (with a ".xml" extension) in the $CATALINA_BASE/conf/[enginename]/[hostname]/ directory. The name of the file (less the .xml extension) will be used as the context path. Multi-level context paths may be defined using #, e.g. foo#bar.xml for a context path of /foo/bar. The default web application may be defined by using a file called ROOT.xml.
  • Only if a context file does not exist for the application in the $CATALINA_BASE/conf/[enginename]/[hostname]/, in an individual file at /META-INF/context.xml inside the application files. If the web application is packaged as a WAR then /META-INF/context.xml will be copied to $CATALINA_BASE/conf/[enginename]/[hostname]/ and renamed to match the application's context path. Once this file exists, it will not be replaced if a new WAR with a newer /META-INF/context.xml is placed in the host's appBase.

So, when you bundle a META-INF/context.xml, the file gets renamed to the name of the WAR and this name becomes the context path, regardless of any path defined in the Context element.

I see thus two options here:

  1. Either set the name of the generated war to a shorter name (I suggest using <finalName> over <warName> which is deprecated AFAIK):

    <project>   ...   <build>     <finalName>mycontext</finalName>     ...   </build>   ... </project> 
  2. Or use the maven-tomcat-plugin for the deployment and set the context path in the plugin configuration:

    <project>   ...   <build>     ...     <plugins>       ...       <plugin>         <groupId>org.codehaus.mojo</groupId>         <artifactId>tomcat-maven-plugin</artifactId>         <version>1.0-SNAPSHOT</version>         <configuration>           <path>/mycontext</path>         </configuration>       </plugin>       ...     </plugins>     ...   </build>   ... </project> 
like image 181
Pascal Thivent Avatar answered Oct 02 '22 19:10

Pascal Thivent