简体   繁体   中英

Tomcat not able to get ServletContext of another webapp

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 ie 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.

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.

Worked in a little different way for Apache Tomcat 8.0.27 . Instead of changing its server.xml , it took me to update the context.xml file of tomcat with the crossContext attribute of Context tag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM