title: Forwarding requests between contexts in Tomcat
body: <p>I'd like to be able to do cross-context request forwarding in Tomcat with the Tuckey URLRewrite filter.  For example, I'd like to be able to route an incoming request with an SEO-/user-friendly URL like <a href="http://example.com/group-elements/300245/some-descriptive-text" rel="nofollow noreferrer">http://example.com/group-elements/300245/some-descriptive-text</a>, where "group-elements" isn't the name of a deployed application, to a URL mapped to a Java Spring controller method for application 'foo', like <a href="http://example.com/foo/app/group/300245/elements" rel="nofollow noreferrer">http://example.com/foo/app/group/300245/elements</a>.  I'm using Tomcat 7.0.27 and URLRewrite 3.2.0; I'm working with Java Spring 3.1 web applications. </p>
  
  <p>The <a href="http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/3.2/index.html#to" rel="nofollow noreferrer">URLRewrite 3.20 documentation</a> notes an optional 'context' attribute for the 'to' filter parameter element: </p>
  
  <pre><code>If your application server is configured to allow "cross context" communication then this attribute can be used to forward (and only forward, not redirect or other "to" types) requests to a named servlet context.
  
  On Tomcat, for instance, the application contexts in the server configuration (server.xml or context.xml) need the option crossContext="true". For instance, the two applications mentioned before ("app" and "forum") have to be defined as:
  
  &lt;Context docBase="app" path="/app" reloadable="true" crossContext="true"/&gt;
  &lt;Context docBase="forum" path="/forum" reloadable="true" crossContext="true"/&gt;
  </code></pre>
  
  <p>Given that and the <a href="http://code.google.com/p/urlrewritefilter/issues/detail?id=17" rel="nofollow noreferrer">original discussion about the feature</a>, the 'context' attribute seems to be what I'm looking for.  However, I haven't been able to properly enable cross-context request forwarding.  </p>
  
  <p>Here's my 'Context' entry application 'foo' in conf/server.xml:</p>
  
  <pre><code>&lt;Context docBase="foo" path="/foo" reloadable="true" crossContext="true"/&gt;
  </code></pre>
  
  <p>I have my urlrewrite.xml file and web.xml file in webapps/ROOT/WEB-INF/.  Here's what they look like:</p>
  
  <p>urlrewrite.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN" 
  "http://tuckey.org/res/dtds/urlrewrite3.2.dtd"&gt;
  
  &lt;urlrewrite&gt;    
      &lt;rule&gt;
          &lt;from&gt;baz&lt;/from&gt;
          &lt;!-- Note: this 'to' element's value has an error.  See the edit at bottom of this post for corrected version. --&gt;
          &lt;to context="foo"&gt;/foo/app/group/300245/elements&lt;/to&gt; 
      &lt;/rule&gt; 
  &lt;/urlrewrite&gt;
  </code></pre>
  
  <p>web.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  &lt;web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd" version="2.5"&gt;
  
      &lt;filter&gt;
          &lt;filter-name&gt;UrlRewriteFilter&lt;/filter-name&gt;
          &lt;filter-class&gt;org.tuckey.web.filters.urlrewrite.UrlRewriteFilter&lt;/filter-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;logLevel&lt;/param-name&gt;
              &lt;param-value&gt;WARN&lt;/param-value&gt;
          &lt;/init-param&gt;
      &lt;/filter&gt;
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;UrlRewriteFilter&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>The rule defined above in urlrewrite.xml is intentionally basic and hard-coded.  In this case I'm simply trying to get the cross-context aspect of the rule working before developing the regular expressions in 'to' and 'from'.</p>
  
  <p>When I request <a href="http://example.com/baz" rel="nofollow noreferrer">http://example.com/baz</a> with that rule in place, Tomcat returns a 404 error saying "The requested resource (/baz) is not available."  I've tried a few variations in the 'to' filter parameter, but nothing has worked yet.  And I haven't been able to find any examples of how 'context' should be used.  </p>
  
  <p>Any ideas on how I could get this kind of cross-context request filtering to work?  Is it even possible?  I imagine I might be able to achieve what I'm trying to do by renaming foo.war to ROOT.war or changing the root application as mentioned <a href="https://stackoverflow.com/questions/715506/tomcat-6-how-to-change-the-root-application">here</a>, but I'd like to try doing this via URLRewrite unless doing so is infeasible or a bad idea on its face.</p>
  
  <p>If showing more of my configuration would help, please let me know.  Thanks in advance for any input.</p>
  
  <p><strong>Edit</strong>:</p>
  
  <p>Thanks to Christopher Schultz for the helpful answer.  In my case, the problem was caused by two things: 1) not having a context.xml file in webapps/ROOT/META-INF, and 2) having an error in the 'to' element in the URL rewrite rule in webapps/ROOT/WEB-INF/urlrewrite.xml.  </p>
  
  <p>The fix involved putting a proper context.xml file in webapps/ROOT/META-INF.  For reference for anyone else that encounters this problem, that file ended up looking like this:</p>
  
  <p>webapps/ROOT/META-INF/context.xml</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  
  &lt;Context docBase="ROOT" path="/" reloadable="true" crossContext="true" /&gt;
  </code></pre>
  
  <p>As Schultz mentions, it's only necessary for a context with crossContext="true" to be defined for the context implied in the 'from' element in a given URL rewrite rule (here, that's ROOT).  It is not necessary to explicitly define a context for the application in the 'to' URL rewrite rule.  In other words, you shouldn't need to manually create a context.xml file for that application -- so continuing the example above, you would not need to manually define and put a context.xml file into webapps/foo/META-INF/.</p>
  
  <p>Schultz's answer reflects the recommendations for defining a context in the official Tomcat documentation: <a href="http://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Defining_a_context" rel="nofollow noreferrer">http://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Defining_a_context</a>.</p>
  
  <p>The problem was also caused by the fact that the URL rewrite rule in my initial post had an error.  The correct version should have been:</p>
  
  <p>urlrewrite.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN" 
  "http://tuckey.org/res/dtds/urlrewrite3.2.dtd"&gt;
  
  &lt;urlrewrite&gt;    
      &lt;rule&gt;
          &lt;from&gt;baz&lt;/from&gt;
          &lt;!-- Note: the use of '/app' instead of '/foo/app/' below --&gt;
          &lt;to context="foo"&gt;/app/group/300245/elements&lt;/to&gt; 
      &lt;/rule&gt; 
  &lt;/urlrewrite&gt;
  </code></pre>