|
本人是給電視做系統(tǒng),客戶要求頁面能顯示時(shí)間,因?yàn)殡娨暈g覽器獲取的時(shí)間不對(duì),沒辦法只能從服務(wù)器獲取時(shí)間,但是問題來了,服務(wù)器的時(shí)間獲取一次就成靜態(tài)的了,客戶不滿意,但也沒辦法,只能這樣。不過這個(gè)問題在我這老惦記著,今天看到有個(gè)例子,說把Java獲取服務(wù)器的時(shí)間轉(zhuǎn)化成毫秒,然后用js每一秒刷新一次就可以動(dòng)態(tài)顯示,所以就有了下面的
復(fù)制代碼 代碼如下:
<%@ 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 is 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">
//從服務(wù)器上獲取初始時(shí)間
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+"年"+month+"月"+day+"日" + time;
}
window.setInterval("run();", 1000);
</script>
<div id="dt">自動(dòng)顯示時(shí)間。。。。</div>
</body>
</html>
jsp技術(shù):Java從服務(wù)器上獲取時(shí)間動(dòng)態(tài)顯示在jsp頁面實(shí)現(xiàn)思路,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。