Java gets the time from the server and displays it dynamically on the jsp page

  • 2020-06-03 08:03:00
  • OfStack

Java gets the server time and displays it dynamically to jsp page. Everyone can only get to Java once. The time to page is static.

I am to make a system for TV, the customer requires the page can display time, because the TV browser access time is wrong, there is no way to only get time from the server, but the problem comes, the server time access once into a static, the customer is not satisfied, but there is no way, only so. However, this problem has been on my mind for a long time. Today I saw an example that converts Java's acquisition time to milliseconds and then USES js to refresh once per second to dynamically display it

<%@ 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 is my page"> 
<link rel="stylesheet" type="text/css" href="styles.css"> 
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() 
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; 
time += hour; 
time += ":"; 
if(minute < 10){ 
time += "0" + minute; 
time += minute; 
time += ":"; 
if(second < 10){ 
time += "0" + second; 
time += second; 
document.getElementById("dt").innerHTML = year+" years "+month+" month "+day+" day " + time; 
window.setInterval("run();", 1000); 
<div id="dt"> Automatic display time... </div> 

Related articles: