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" >
<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" >
-->
<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" > Automatically show time .... </div>
</body>