Java – application.getContext() returns null when context path is ”

apachejavajsptomcat

I am currently running a Java web application on Tomcat 6, and have set up a virtual host in Apache HTTP Server to forward requests to the Tomcat. The context paths are therefore different. When the web app is accessed at

http://server:8080/WebApp

the context path is '/WebApp' and the context is retrieved. When accessing the same JSP via the proxy at

http://webapp/

the context path is '' and application.getContext() returns null.

Is there any configuration I might have missed when setting up the virtual host, or is this a problem with the application code?

Best Answer

There's no point in calling

application.getContext(application.getContextPath())

The getContext method is for getting a reference to the ServletContext for a different web application (and the container is allowed by the spec to return null if the current app does not have permission to access other contexts - this is the default in Tomcat unless you've specified crossContext="true" in the context configuration file).

You already have a reference to the ServletContext for the current webapp - in the application variable.