Tomcat无法获得另一个webapp的ServletContext

Sha*_*mik 5 tomcat servlets servlet-filters

我正在使用tomcat 6,我有两个webapps.一个是webapp1,另一个是webapp2.从webapp2中的过滤器,我试图访问其他webapp即webapp1.我的过滤器代码如下所示

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);
}
Run Code Online (Sandbox Code Playgroud)

任何想法我做错了什么?我总是将null作为othercontext值.

Tay*_*ese 10

在server.xml中设置crossContext ="true".请参阅上下文容器的文档.

如果要在此应用程序中调用ServletContext.getContext()以成功返回在此虚拟主机上运行的其他Web应用程序的请求调度程序,则设置为true.在安全意识环境中设置为false(默认值),以使getContext()始终返回null.