发送http头,控制浏览器定时跳转网页(五)

package com.it.servlet.response;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 发送http头,控制浏览器定时跳转网页。
 * 功能:当访问此servlet时,5秒后跳转到一个指定的页面。
 */
public class Demo2Servlet extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//通知浏览器5秒后,跳转到指定的URL
		response.setHeader("refresh", "5;url=/day8_1/refresh.html");
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

其中:response.setHeader("refresh", "5;url=/day8_1/refresh.html");

服务器端通知浏览器5秒后跳转到URL指定的路径。

扩展:在实际操作中,很少使用服务器端进行定时跳转,一般都是在浏览器端完成。

<meta http-equiv="refresh" content="5;url=/day8_1/index.jsp">

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="5;url=http://www.baidu.com">
<title>hello world</title>
</head>
<body>
<h1>Hello</h1>
<h6>World</h6>
</body>
</html>

要求浏览器在5秒后跳转,并显示跳转倒计时

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="5;url=/day8_1/index.jsp">
<title>hello world</title>
<script type="text/javascript">
	var time = 5;
	function onchange(){
		var span = document.getElementById("seconds");
		span.innerHTML = time;
		time--;
		setTimeout("onchange()",1000);
	}
</script>
</head>
<body onload="onchange()">
<span id="seconds"></span>
</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值