重定向和js代码实现定时跳转

重定向:

你向张三借钱,张三说我没有,李四有,你向李四借,然后你就去向李四借。

代码:

public class Servlet1 extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

//没有响应 告知客户端去重定向到servlet2

//1、设置状态码302

//response.setStatus(302);

//2、设置响应头Location

//response.setHeader(“Location”, “/WEB14/servlet2”);

   //封装成一个重定向的方法sendRedirect(url)

              response.sendRedirect(“/WEB14/servlet2”);         

}

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

}

}

 

Java代码实现定时刷新:

//设置定时刷新的头

response.setHeader(“refresh”, “5;url=http://www.baidu.com”);

js代码实现定时刷新

<!DOCTYPE html>

<html>

<head>

<meta charset=”UTF-8″>

<title>Insert title here</title>

<script type=”text/javascript”>

window.onload = function(){

var time = 5;

var secondEle = document.getElementById(“second”);

var timer = setInterval(function(){

secondEle.innerHTML = time;

time–;

if(time==0){

clearInterval(timer);

location.href=”http://www.baidu.com”;

}

},1000);

}

</script>

</head>

<body>

恭喜你,注册成功,<span style=”color:red” id=”second”>5</span>秒钟后跳转,如不跳转点击<a href=”http://www.baidu.com”>这里</a>!

</body>

</html>

 

发表评论