java ee 中 Jsp 页面的定时的跳转(数字倒数)
java ee 中 Jsp 页面的定时的跳转,实现数字倒计时跳转固定页面
1,Servlet类
RefreshServlet类实现
package org.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 页面的定时的跳转 */ @WebServlet("/RefreshServlet") public class RefreshServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 先输出内容 System.out.println("Refresh1Servlet执行了..."); // 设置头信息 response.setHeader("refresh", "5;url=url=/Web_01/main.html"); // 输出一句内容 response.getWriter().print("5 miao hou tiao zhuan"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
2,Jsp页面调用script方法实现
Jsp页面设置:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <!-- 完成页面定时的跳转 --> <meta http-equiv="refresh" content="5;url=/Web_01/main.html"> <title>Insert title here</title> </head> <body onload="run()"> 页面将在<span id="spanId">5</span>秒后跳转!! </body>
<script type="text/javascript"> // 页面一加载完成,该方法就会执行 // 读秒,一秒钟数字改变一次 var x = 5; function run(){ // 获取到的是span标签的对象 var span = document.getElementById("spanId"); // 获取span标签中间的文本 span.innerHTML = x; x--; // 再让run方法执行呢,一秒钟执行一次 window.setTimeout("run()", 1000); } </script>
</html>