Java Servlet 运行 调用时进入死循环 request.getRequestDispatcher

Java Servlet running into infinite loop when calling request.getRequestDispatcher

我正在尝试制作一个启动 servlet (Start.java),它是我非常简单的网站的入口点。目前我有这个启动 servlet 的两个映射:

@WebServlet({"/Start", "/Start/*"})

当用户首次加载页面时(调用 http://localhost:8080/MyApp/Start) a form page is loaded (Form.jspx) for them to fill out (the details of the form aren't important to the problem I'm having). What I want to do is have another jsp page (Done.jspx) load when the user loads this page: http://localhost:8080/MyApp/Start/Restore 时)。调用此页面时,我正在使用解组器将一些数据从 XML 文件恢复到我的数据库中。Done.jspx 页面仅显示从 XML 文件中插入了多少行,但我认为这对我的问题并不重要。

我遇到的问题是在我的 doGet 方法中,我在该方法中检查 Restore 是否在 URI 中。如果不是,我加载默认页面 Form.jspx。如果它在 URI 中,我调用请求调度程序转发到 Done.jspx。这会导致无限循环,因为当我在 Done.jspx 上调用调度程序时,它加载 http://localhost:8080/MyApp/Start/Done.jspx 匹配上面的映射 (/Start/*) 并导致 servlet 在行处进入无限循环我正在加载最终导致堆栈溢出的 Form.jspx 页面(见下文)。

下面是我的doGet方法

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String URI = req.getRequestURI();
    System.out.println("URI: " + URI);

    if (URI.endsWith("Restore")) {
        String filename = this.getServletContext().getRealPath("/export/backup.xml");
        model.setPath(this.getServletContext().getRealPath("/export"));
        try {
            int n = model.importXML(filename);
            req.setAttribute("numInserted", n);
            req.getRequestDispatcher("Done.jspx").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }                                                           //load default form page
    else {
        req.getRequestDispatcher("Form.jspx").forward(req, resp);
    }
}

我对 Java EE 和 servlet 还是很陌生,所以我可能以错误的方式解决了这个问题。我想知道是否有解决此问题的方法,或者我是否错误地处理了不同的 URI 模式。

问题与 "/Start/*" 模式有关。它也会 redirect/forward /Start/Done.jspx 到 servlet。这不是你所期待的。

您应该制作两个不同的 servlet,一个用于 /Start,另一个用于 /Start/Restore

--已编辑--

根据你的情况,应该用@WebServlet({"/Start", "/Start/Restore"})代替。