【问题标题】:How can i open a window popup in Servlet and then Redirect a Page如何在 Servlet 中打开一个弹出窗口然后重定向页面
【发布时间】:2012-12-18 06:51:51
【问题描述】:

我想在调用 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

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

【问题讨论】:

  • 你不能使用sendRedirect(),它的作用是客户端要求A,但服务器说你真的想要B。您正在做的是返回 A 但以这样一种方式加载 B 您需要使用 javascript 或元标记。

标签: java javascript servlets popup


【解决方案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】:

    你可以调用一个jsp页面而不是servlet,并且可以使用一个jQuery插件thickbox

    http://thickbox.net/

    【讨论】:

      【解决方案3】:

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

      window.location.href = 'pageB.jsp'

      【讨论】:

        猜你喜欢
        • 2014-02-23
        • 1970-01-01
        • 1970-01-01
        • 2010-12-27
        • 1970-01-01
        • 1970-01-01
        • 2010-09-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多