public class ShowTime extends HttpServlet { public void doGet(HttpServletRequest request,HttpServletResponse response) throws servletexception,IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss"); String time = sdf.format(new Date()); out.println(time); } }
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>动态显示时钟</title> </head> <body> <input type="button" id="start" value="开始显示"> <input type="button" id="stop" value="停止显示"> <div id="showtime"></div> </body>
<script type="text/javascript"> window.onload = function(){ var t; document.getElementById("start").onclick = showtime; document.getElementById("stop").onclick = function(){ clearTimeout(t); } function showtime(){ var xhr = ajaxFunction(); xhr.onreadystatechange = function(){ if(xhr.readyState==4){ if(xhr.status==200){ var data = xhr.responseText; document.getElementById("showtime").innerHTML = "<h1>"+data+"</h1>"; } } } xhr.open("get","showTime?timeStamp="+new Date().getTime(),true); xhr.send(null); t = setTimeout(showtime,1000); } function ajaxFunction(){ var xmlHttp; try{ // Firefox,Opera 8.0+,Safari xmlHttp=new XMLHttpRequest(); } catch (e){ try{// Internet Explorer xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e){ try{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){} } } return xmlHttp; } } </script>
</html>
总结 其实 b/s不支持服务器主推服务
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。