Java Servlet响应似乎没有完成

时间:2018-03-11 10:29:18

标签: java servlets

首先,请怜悯这个noobie问题。我目前正在开发一个Web应用程序项目,偶然发现了我无法解决的基本问题。

基本上,我有以下内容:

  • index.html向servlet1发送POST请求。
  • servlet1向浏览器发送重定向到servlet2。
  • servlet2返回内容。

我确实在浏览器中看到了从servlet2返回的内容,但是当我查看IE Developer Tools(F12)时,我发现对servlet2的GET请求永远不会完成(STATUS PENDING)。必须有一些我非常缺乏的基本内容......

在下面的屏幕截图中,“Ausstehend”会转换为“待定”。

IE屏幕截图: IE F12 Screenshot

的index.html:

<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <form action="servlet1" method="post">
            <input type="text" name="test" placeholder="some data">
            <button type="submit"><b>send</b></button>
        </form> 
    </body>
</html>

servlet1:

public class servlet1 extends HttpServlet {
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.sendRedirect("servlet2");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
}

servlet2:

public class servlet2 extends HttpServlet {
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet servlet2</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>HELLO</h1>");
            out.println("</body>");
            out.println("</html>");
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
}

0 个答案:

没有答案