加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

反向Ajax

发布时间:2020-12-15 21:56:46 所属栏目:百科 来源:网络整理
导读: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.getWrit
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不支持服务器主推服务

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读