如何用JAVA编程“在网页中显示动态时间”
1个回答
展开全部
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();
%>
你试试吧,应该没问题的
<%@ 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();
%>
你试试吧,应该没问题的
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询