1

Is there a way to define alias for a Servlet context?

If I want that

  • http://server/a/foo-servlet and http://server/b/foo-servlet point to the same Servlet (or http://server/a/foo.jsp and http://server/b/foo.jsp for that matter)
  • calling HttpServletRequest#getContextPath() in the foo Servlet (or foo.jsp) returns /a or /b respectively

my only option seems to be to deploy the web application containing the foo Servlet twice. Correct?

If I define two contexts a and b both pointing to the same doc base the container would essentially still deploy two individual applications.

Marcel Stör
  • 22,695
  • 19
  • 92
  • 198
  • Why you need different alias for the same application? What you want to achieve? – Braj Jun 02 '14 at 21:44
  • 1
    Add two [`url-pattern` elements](http://stackoverflow.com/a/16867899/2071828). Map the exact same servlet to two different url patterns. – Boris the Spider Jun 02 '14 at 21:45
  • @BoristheSpider I think, OP is talking about the different context root not just different path to the same Servlet but I am confused with the `server` in the OP's sample and talking about `getContextPath()` method as well. – Braj Jun 02 '14 at 21:53
  • You could deploy it to /foo, then use a ROOT context with HttpRewriteFilter to rewrite the URLs. There might be a better way, though. – David Ehrmann Jun 02 '14 at 23:11
  • "different context root not just different path to the same Servlet" - yes, that's correct. I updated the question that make that a little clearer. – Marcel Stör Jun 03 '14 at 05:08
  • You can use apache to create aliases. – Leos Literak Jun 03 '14 at 06:55
  • http://stackoverflow.com/q/11721966/289396 is what you're looking for? – JoseK Jun 03 '14 at 08:48
  • @LeosLiterak, "use apache to create aliases" - true, but that doesn't solve the `HttpServletRequest#getContextPath()` problem. – Marcel Stör Jun 03 '14 at 09:19
  • @JoseK, the question you referenced describes how to deploy the same WAR file more than once under different contexts. I know I _could_ do that but I was wondering whether there's a way around it. – Marcel Stör Jun 03 '14 at 09:21
  • @MarcelStör: I assumed it's still a single WAR rather than deploying twice - I thought which was what you wanted? Okay doesn't look to be so straightforward then – JoseK Jun 03 '14 at 09:36

0 Answers0