通过refresh响应头,定时刷新或隔n秒跳转页面

时间:2022-12-09 13:50:29
package day08;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 案例【refresh】--定时刷新或隔n秒跳转页面
 * @author mzy
 *
 */
public class ResponseDemo03 extends HttpServlet {

	private static final long serialVersionUID = -541417963050708397L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		
		// 设置refresh响应头
		// 1.设置秒数:每隔2秒进行一次页面刷新
		// response.setHeader("refresh", "2"); 
		
		// 2.设置秒数:每隔n秒进行页面跳转
		response.getWriter().write("注册成功!3秒之后会跳转到主页...");
		response.setHeader("refresh", "3;../adv.html");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
	}

}