Java get time from the server dynamic display in JSP page implementation ideas

Source: Internet
Author: User
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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.