有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在Servlet中打开弹出窗口,然后重定向页面

我想在调用servlet时打开一个弹出窗口,然后将servlet重定向到某个.jsp页面

这就是我所做的:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        PrintWriter out = response.getWriter();
        out.println("<html><body>");
        out.println("<script type=\"text/javascript\">");
        out.println("window.open(\"pageA.jsp\")");
        out.println("</script>");
        out.println("</body></html>");
        response.sendRedirect("pageB.jsp");
    }

只有当response.sendRedirect("error.jsp");不存在或没有注释时,此代码才会弹出窗口。目前使用这段代码,它不会弹出窗口并直接将此页面重定向到error.jsp

我怎样才能同时做以上两件事


共 (2) 个答案

  1. # 1 楼答案

    你可以使用JavaScript来实现这一点。例如:

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("<html><body>");
        out.println("<script type=\"text/javascript\">");
        out.println("var popwin = window.open(\"pageA.jsp\")");
        out.println("setTimeout(function(){ popwin.close(); window.location.href='pageB.jsp';},5000)");
        out.println("</script>");
        out.println("</body></html>");
    }
    
  2. # 2 楼答案

    不要使用sendRedirect重定向页面,而是使用

    window.location.href = 'pageB.jsp'