I am using tomcat 6 and I have two webapps. One is webapp1 and the other one is webapp2. From a filter inside webapp2, I am trying to access the other webapp i.e webapp1. My Filter code is something like below
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest)request;
System.out.println("requeted path = " + req.getRequestURI());
ServletContext othercontext = confg.getServletContext().getContext("/webapp1");
RequestDispatcher dispatcher = othercontext.getRequestDispatcher(req.getRequestURI());
dispatcher.forward(request, response);
chain.doFilter(request, response);
}
Any Idea what I am doing wrong ? I am always getting null as the othercontext value.
The default appBase location is "$CATALINA_BASE/webapps", or "$CATALINA_HOME/webapps", if no base directory has been defined. Next, if the application does not contain a Context fragment, one must be added within the appropriate Host element in Tomcat's server. xml configuration file.
public interface ServletContext. Defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file. There is one context per "web application" per Java Virtual Machine.
Set crossContext = "true" in the server.xml. See the documentation for the context container.
Set to true if you want calls within this application to ServletContext.getContext() to successfully return a request dispatcher for other web applications running on this virtual host. Set to false (the default) in security conscious environments, to make getContext() always return null.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With