Java RequestDispatcher returns Jetty Embedded 应用程序中为 null

Java RequestDispatcher returns null in Jetty Embedded app

我有以下代码

//response.sendRedirect("Cmmdc");
RequestDispatcher requestDispatcher=request.getRequestDispatcher("Cmmdc");
System.out.println(requestDispatcher);--- **NULL**
requestDispatcher.forward(request,response);

我的主要:

Server server = new Server(8080);
ServletHandler handler = new ServletHandler();
server.setHandler(handler);

handler.addServletWithMapping(Start.class, "/Start");
handler.addServletWithMapping(Cmmdc.class, "/Cmmdc");
handler.addServletWithMapping(OptiuneServlet.class, "/OptiuneServlet");
handler.addServletWithMapping(Prim.class, "/Prim");
handler.addServletWithMapping(ValidatorCMMDC.class, "/ValidatorCMMDC");
handler.addServletWithMapping(ValidatorPrim.class, "/ValidatorPrim");
System.out.println("Start server jetty embedded");
server.start();
server.join();

我的问题是我应该如何将 cmmdc servlet 调用为 return 一个引用而不是 null?

RequestDispatcher 需要一个ServletContext(没有办法解决)。

您的设置有 none.

不要像那样直接使用ServletHandler,而是使用ServletContextHandler

Server server = new Server(8080);
ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
server.setHandler(context);
context.addServlet(Start.class, "/Start");
// etc ...
server.start();
server.join();