Java can only get once, to the page time is static, but through the cooperation of JS and Java, cleverly realize this function
Java Get server time, dynamic display to JSP page, everyone is to Java can only get once, to the page time is static, but through the cooperation of JS and Java, cleverly realize this function.
I am to do the system for television, customer Requirements page can show time, because the TV browser to get the time is not, no way can only get time from the server, but the problem comes, the server time to obtain a static, the customer is not satisfied, but there is no way, can only be so. But this problem in my old thinking, today saw an example, said the Java get server time into milliseconds, and then with JS every second refresh can be dynamic display, so there is the following
Copy Code code as follows:
<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%>
<% @page import= "Java.text.SimpleDateFormat;" %>
<%
String Path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";
%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<html>
<head>
<base href= "<%=basePath%>" >
<title>my JSP ' index.jsp ' starting page</title>
<meta http-equiv= "pragma" content= "No-cache" >
<meta http-equiv= "Cache-control" content= "No-cache" >
<meta http-equiv= "expires" content= "0" >
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This are my page" >
<!--
<link rel= "stylesheet" type= "Text/css" href= "Styles.css" >
-->
</head>
<body>
<%
Calendar RightNow = Calendar.getinstance ();
simpledateformat format = new SimpleDateFormat ("Yyyy-mm-dd hh:mm");
%>
<script language= "JavaScript" >
//Get the initial time from the server
var currentdate = new Date (<%=new java.util.Date (). GetTime ()%>);
function Run ()
{
currentdate.setseconds (Currentdate.getseconds () +1);
var time = "";
var year = Currentdate.getfullyear ();
var month = Currentdate.getmonth () + 1;
var day = Currentdate.getdate ();
var hour = currentdate.gethours ();
var minute = currentdate.getminutes ();
var second = Currentdate.getseconds ();
if (Hour < 10) {
time + = "0" + hour;
}else{
time + = hour;
}
Time + = ":";
if (minute < 10) {
time + = "0" + minute;
}else{
time + = minute;
}
Time + = ":";
if (second < 10) {
time + = "0" + second;
}else{
time + = second;
}
document.getElementById ("DT"). InnerHTML = year+ "year" +month+ "month" +day+ "Day" + time;
}
Window.setinterval ("Run ();", 1000);
</script>
<div id= "DT" > Auto Show Time .... </div>
</body>
</html>