日期:2014-05-16  浏览次数:20361 次

ajax+jsp+servlet 实现动态显示系统当前时间

1. showCurrentTime.jsp

<%@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
		<title>动态显示系统当前时间</title>
	</head>
<% 
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
	<script>
		var xmlHttp;
		function createXMLHttpRequest(){
			if(window.ActiveXObject){
				xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
			}else if(window.XMLHttpRequest){
				xmlHttp = new XMLHttpRequest();
			}
		}
		function exec(){
			createXMLHttpRequest();
			xmlHttp.open("post","<%=basePath %>"+"timeUpdate",true);
			xmlHttp.onReadyStateChange = handleFun;
			xmlHttp.send(null);
		}
		function handleFun(){
			if(xmlHttp.readyState == 4){
				if(xmlHttp.status == 200){
					document.getElementById("div").innerText = xmlHttp.responseTEXT;
				}
			}
		}
		setInterval("exec()",1000);
	</script>
	<body>
		你好!
		TimeUpdateServlet路径:<%=basePath %>
		<div id="div"></div>
	</body>
</html>

?

?

2. TimeUpdateServlet.java

package com.ibm.user.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TimeUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("gb2312");
		response.setContentType("text/html;charset=gb2312");
		Date date = new Date();
		DateFormat dformater = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String currentTime = dformater.format(date);
		
		PrintWriter out = response.getWriter();
		out.println(currentTime);
		out.flush();
		out.close();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}
}

?

3.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>xg3</display-name>
    <servlet>
    <description></description>
    <display-name>TimeUpdateServlet</display-name>
    <servlet-name>TimeUpdateServlet</servlet-name>
    <servlet-class>com.ibm.user.web.TimeUpdateServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>TimeUpdateServlet</servlet-name>
    <url-pattern>/timeUpdate</url-pattern>
  </servlet-mapping>
</web-app>

?