0

I can't figure out a scenario when getServletContext.getContextPath() and request.getContextPath() would return different Strings.

HttpServletRequest.getContextpath() javadoc:

It is possible that a servlet container may match a context by more than one context path. In such cases this method will return the actual context path used by the request and it may differ from the path returned by the ServletContext.getContextPath() method. The context path returned by ServletContext.getContextPath() should be considered as the prime or preferred context path of the application.

ServletContext.getContextPath() javadoc:

It is possible that a servlet container may match a context by more than one context path. In such cases the HttpServletRequest.getContextPath() will return the actual context path used by the request and it may differ from the path returned by this method. The context path returned by this method should be considered as the prime or preferred context path of the application.

When my application is within Tomcat's webapps/ROOT folder both methods return "" (empty String).

In all other cases, if my app is in myapp.war (put in Tomcat's webapps folder), both methods return "/myapp" as expected.

When I put my.war and mymy.war into Tomcat's webapps folder, Tomcat sees two distinct web applications, so I cannot model this "servlet container may match a context by more than one context path" case.

I cannot call my WARs my.war and my/my.war because "/" is not allowed in file names both on Windows and Unix.

Code Complete
  • 3,146
  • 1
  • 15
  • 38

1 Answers1

0

This is supported because it's pretty straightforward to have multiple context paths mapped to the same webapp.

Here some other ways to achieve the same thing:

kolossus
  • 20,559
  • 3
  • 52
  • 104