在部署在tomcat10上的上下文
/inbox
中,我添加了包含以下内容的文件META-INF/context.xml
:
<?xml version="1.0" encoding="UTF-8" ?>
<Context crossContext="true" />
但是当我尝试转到另一个上下文时,:
getServletContext().getContext("/auth").getRequestDispatcher("/login").forward(request, response);
我收到错误:
Cannot invoke "jakarta.servlet.ServletContext.getRequestDispatcher(String)" because the return value of "jakarta.servlet.ServletContext.getContext(String)" is null
我在这里缺少什么?
上面的调用在此 servlet 中使用,来自
/inbox
:
@WebServlet(name = "Home", urlPatterns = "/home")
public class Home extends HttpServlet {
protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
...
}
}
我还尝试了字符串
/../auth
和 /../../auth
作为上下文,但出现了同样的错误。
包含
META-INF
的 context.xml
文件夹需要是 WAR 文件根目录中的文件夹,而不是 WEB-INF/classes
文件夹中的文件夹。这只是因为它是特定于服务器的配置文件,不应该在 Web 应用程序的类路径中可用。对于默认 Maven 项目,源文件路径预计为 src/main/webapp/META-INF/context.xml
,因此不是例如src/main/resources/META-INF/context.xml
。