美文网首页
【Java】【web】实现自动刷新和重定向

【Java】【web】实现自动刷新和重定向

作者: JerichoPH | 来源:发表于2017-04-08 09:57 被阅读372次

    实现自动刷新和重定向

    // 自动刷新
    public class Demo1 extends HttpServlet {
        
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setHeader("content-type", "text/html;charset=utf-8");
            response.getWriter().write("注册成功,3秒钟后自动跳转!");
            response.setHeader("refresh", "3;url=http://www.baidu.com");
        }
        
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            this.doGet(request, response);
        }
    
    }
    
    // 重定向
    public class Demo1 extends HttpServlet {
    
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            // 方法1
            response.setStatus(302);// 告诉客户端,重定向
            response.setHeader("location", "/JDemo/demo2");// 告诉客户端,重定向的位置
            
            // 方法2
            response.sendRedirect("/JDemo/demo2");
        }
        
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            this.doGet(request, response);
        }
    
    }
    

    相关文章

      网友评论

          本文标题:【Java】【web】实现自动刷新和重定向

          本文链接:https://www.haomeiwen.com/subject/joclattx.html