當前位置:編程學習大全網 - 編程語言 - 如何用JAVA編程“在網頁中顯示動態時間”

如何用JAVA編程“在網頁中顯示動態時間”

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%

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>

<script type="text/javascript">

var xmlHttp;

function creatRequest()

{

try

{

// Firefox, Opera 8.0+, Safari

xmlHttp=new XMLHttpRequest();

}

catch (e)

{

// Internet Explorer

try

{

xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");

}

catch (e)

{

try

{

xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

}

catch (e)

{

alert("您的瀏覽器不支持AJAX!");

return false;

}

}

}

}

function startRequest()

{

creatRequest();

xmlHttp.open("GET","time.jsp",true);

xmlHttp.onreadystatechange = handleRequest;

xmlHttp.send(null);

}

function handleRequest()

{

if(xmlHttp.readyState==4)

{

if(xmlHttp.status==200)

{

document.getElementById("res").innerHTML = xmlHttp.responseText;

}

}

}

window.setInterval("startRequest()",1000);

</script>

</head>

<body>

<div id="res"></div>

</body>

</html>

time.jsp

<%@ page contentType="text/html; charset=utf-8" %>

<%@ page import="java.util.Date"%>

<%@ page import="java.text.SimpleDateFormat"%>

<%

response.setContentType("text/xml; charset=UTF-8");

response.setHeader("Cache-Control","no-cache");

String str = request.getSession().getServletContext().getRealPath(request.getRequestURI());

Date d = new Date();

String sf = "北京時間:H:mm:ss";

SimpleDateFormat sdf = new SimpleDateFormat(sf);

out.println(sdf.format(d.getTime()));

//out.println(d.getSeconds());

out.close();

%>

妳試試吧,應該沒問題的

  • 上一篇:不朽英雄章節目錄
  • 下一篇:什麽樣的人適合做程序員?
  • copyright 2024編程學習大全網