实现自动刷新和重定向
// 自动刷新
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);
}
}
网友评论