How do I get the path to web applications outside of the 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 installed in the root directory, I need to know the path to the current web application.

eg. http://www.mydomain.com/myapplication/index.htm

I need to get the "myapplicaiton" part.

I have tried ServletContext, ApplicationContext with no success. I figured I could get it from the 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 it's so easy.

+2


source to share


5 answers


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

.

If you need it in doFilter () you have access to the request. If you really want to do this in init()

on Tomcat 5.5 or earlier, you can do the hack,



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

      

Of course it won't be portable.

+2


source


The method you are looking for is getContextPath () javax.servlet.ServletContext



+1


source


The part you are looking for is contextPath, up until Servlet 2.5, the only way to get the context table was by calling the getContextPath () method in the HttpServletRequest class. This means that you can only receive a request upon request. This is due to the fact that your webapp can be mapped to more than one contextPath. Since 2.5 you can get the contextPath from the ServletContext class, if your webapp maps to more than one contextPath, you will get a simple or preferred contextPath (as defined by the container).

It often happens that you want to know the contextPath in the init () method, either in a filter or in a servlet. With version 2.5, you can but cannot support displaying more than one contextPath (although this is rarely an issue).

0


source


If you just want to redirect the user to the context root, you can simply do this inside your method of doFilter

your filter. This assumes you grabbed context

from FilterConfig inside the Filter method.

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

      

From the docs for RequestDispatcher :

The path name must start with a "/" character and is interpreted as relative to the current context root.

If you really want the string to do something with it, most other solutions would be sufficient.

0


source


Someone mentioned a hacked hack earlier that allows you to 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 ServletContextListener and dump the path when your web server starts up:

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 higher, as this method was not implemented in previous versions.

0


source







All Articles