我正在使用 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/