How can i open a window popup in Servlet and then Redirect a Page

孤街浪徒 提交于 2019-12-23 22:42:46

问题


I want to open a popup window on calling a servlet and then want to redirect servlet to some .jsp page.

This is what i've done:

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");
    }

This code will only popup window when the response.sendRedirect("error.jsp"); is not present or commented. Currently with this code, it is not popping a window and directly redirecting this page to error.jsp

How can i do both of the above things at the same time?


回答1:


You can use JavaScript do the trick. For example:

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:


Instead of redirecting a page using sendRedirect, Use

window.location.href = 'pageB.jsp'




回答3:


You can call a jsp page instead of servlet and canuse a jQuery plugin thickbox

http://thickbox.net/



来源:https://stackoverflow.com/questions/14146883/how-can-i-open-a-window-popup-in-servlet-and-then-redirect-a-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!