Tomcat 无法获取另一个 webapp 的 ServletContext

标签 tomcat servlets servlet-filters

我正在使用 tomcat 6,并且有两个 Web 应用程序。一个是 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);
}

知道我做错了什么吗?我总是将 null 作为 othercontext 值。

最佳答案

在 server.xml 中设置 crossContext = "true"。查看documentation对于上下文容器。

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.

关于Tomcat 无法获取另一个 webapp 的 ServletContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2088633/

相关文章:

spring - Tomcat [9.0.26] - 调用安全服务 - TrustStore 与 KeyStore 配置差异和问题

php - 将端口 80 更改为 8085 后,wamp 服务器 localhost 无法正常工作

css - 无法在 Spring MVC 4 (eclipse) 中加载本地 css

java - Spring Framework 中的 NullPointerException(或什么?)

java - 从某些网址中排除过滤器

eclipse - 如何在Eclipse Juno中运行Servlet?

java - 在 Tomcat 6 上部署 webapp

java - 使用 servlet 从 mysql 检索数据的空指针异常

java - 我们可以在 servlet 中使用来自 ajax 的 request.get 参数吗?

spring - 需要帮助解决 Spring 应用程序中使用 Tomcat 的异步支持启用问题