@WebServlet("/")
public class RootServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String pathInfo = request.getServletPath();
switch(pathInfo) {
case "/":
this.handleHomePage(request, response);
break;
default:
request.getRequestDispatcher(pathInfo).forward(request, response);
}
}
我正在尝试使用默认 servlet 来捕获上下文根 url。所以当它是一个根 url 时,它会被 handleHomePage 方法处理。如果没有,它将被转发到相应的文件。例如 css、html、图像文件。但这会导致一个永无止境的异常发生。getRequestDispatcher 是否允许转发到静态页面?