savanna
savanna

Reputation:

How to get web application path outside of a JSP page?

I am new to JSP and Java EE. So maybe there is something very obvious that I missed.

I have a web filter class that needs to redirect the web request back to the root of the current web application. But since our application is not deployed in the root, I need to know the path the the current web application.

e.g. http://www.mydomain.com/myapplication/index.htm

I need to get the "myapplicaiton" part.

I have tried ServletContext, ApplicationContext with no success. I realized I can get it from HttpServletRequest but I don't have access to it in my filter class.

Please help. I am stuck. I am from the .NET world. And this is so easy there.

Upvotes: 2

Views: 5425

Answers (5)

idleworx
idleworx

Reputation: 107

Someone mentioned a hack earlier which lets you use the Catalina ApplicationContext object to get the Context Path.

String contextPath = ((org.apache.catalina.core.ApplicationContext)filterConfig.getSevletContext()).getContextPath();

You can also use this inside a ServletContextListener and dump the path when your webapp starts:

ApplicationContext tomcatContext = (ApplicationContext)event.getServletContext();
    String contextPath = tomcatContext.getContextPath();

These hacks will work, however only with the catalina.jar file from Tomcat 5.5.16 or above, as that method was not implemented in previous versions.

Upvotes: 0

seth
seth

Reputation: 37267

If you just want to forward the user to the root context, you can just do this inside of your doFilter method of your filter. This assumes you grabbed the context from the FilterConfig inside of the Filter init method.

    context.getRequestDispatcher("/").forward(request, response);

From the docs for RequestDispatcher:

The pathname must begin with a "/" and is interpreted as relative to the current context root.

If you actually want the string to do something with it, most of the other solutions would suffice.

Upvotes: 0

ZZ Coder
ZZ Coder

Reputation: 75456

Depending on your Servlet version, you might not be able to get it without a request. Before Servlet 2.5, it makes assumption that a servlet may have multiple context paths so you can only get it from a request. This is changed in 2.5 and ServletContext.getContexPath() is added.

If you need this in doFilter(), you have access to the request. If you really want do this in init() on Tomcat 5.5 or earlier version, you can do a hack,

String contextPath = ((org.apache.catalina.core.ApplicationContext)filterConfig.getSevletContext()).getContextPath();

Of course, this wouldn't be portable.

Upvotes: 2

sigget
sigget

Reputation: 927

The part you're looking for is the contextPath, up until Servlet version 2.5 the only way to get the contextPath was by calling the method getContextPath() on the HttpServletRequest class. This means that you can only get it one a request by request basis. It has to do with the fact that your webapp can be mapped to more than one contextPath. As of version 2.5 you can get the contextPath from the ServletContext class, if your webapp is mapped to more than one contextPath you will get the prime or preferred contextPath (as decided by the container).

It's often the case that you want to know the contextPath in an init() method in either a filter or a servlet. With version 2.5 you can but you won't be able to support being mapped to more than one contextPath (although that's rarely a problem).

Upvotes: 0

ChssPly76
ChssPly76

Reputation: 100706

The method you're looking for is getContextPath() of javax.servlet.ServletContext

Upvotes: 1

Related Questions