Recently, I migrated my web application from Resin to Tomcat. In one of the filter init methods, I had to access the web application context name i.e if the URI is then the context name would be bar.

I was using getServletContextName() method of servlet context object. In Resin, I used to get the application context name. After migrating to Tomcat, I started getting the same as null.

Servlet 2.4 api spec says the following regarding getServletContextName():

Returns the name of this web application corresponding to this ServletContext as specified in the deployment descriptor for this web application by the display-name element.

This was a revelation to me as, until now I had assumed that getContextName() returns the starting part of the URI. Resin does do this. Resin implementation of getServletContextName() says this:

Returns the URL prefix for the ServletContext.

Then, I took a look at Tomcat implementation. Tomcat confirms to servlet api specifications.

Now, I started thinking as to how to make my application container independent. If my code was inside the doFilter method instead of init, I could use request.getServletPath() to access the application context name. But, my code was in the init method with no access to request response objects. A user in Tomcat mailing list pointed out that Tomcat 6.x implemets servlet api 2.5 specs and in 2.5 specs there is a method in ServletContext object to get the application context name. Upgrading to 2.5 servlet api resolved my problem.