ajax异步访问服务器

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   
    <title>显示进度</title>
    <script type="text/javascript">
    -
    function a() {
    	
    	var xhr = null;
		if(window.XMLHttpRequest)
			xhr = new XMLHttpRequest();
		if(window.ActiveXObject)
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		
		// 打开与某个资源的连接
		
		xhr.open("get","${pageContext.request.contextPath }/servlet/ProgressServlet?t="+new Date().getTime(), false);
		
		// 调用send 方法发送请求,post方式需要发送消息体,get方式则不用直接传入null值
		xhr.send(null);
		
		// 访问 responseText 属性获得 Servlet 回送的数据
		var data = xhr.responseText;
		
		//可以向这个id的位置写入HTML代码
		window.parent.document.getElementById("info").innerHTML = data;
    }
    
    window.onload = function() {
    	window.parent.document.getElementById("mysubmit").onclick = function() {
    		
    		//即浏览器第隔1称秒都会执行一次a函数。
    		window.setInterval(a, 1000);
    	}
    }
    </script>
  </head>
  
  <body>
    <div id="info">准备上传</div><br>
  </body>
</html>

 

在另一个网页中添加这样的代码就可以实现文档中的文档,或者浮动的框架,来加载上面的网页,从而隐藏起来运行,

 <iframe name="top" src="${pageContext.request.contextPath }/progress.jsp" style="display:none;"></iframe>


添加服务器端的进度监听

ServletFileUpload fileUpload = new ServletFileUpload(
					new DiskFileItemFactory());
			
			// 添加进度条监听
			fileUpload.setProgressListener(new ProgressListener() {
				
				private long temp = -1;
				
				// 上传过程中   不断地触发该方法
				public void update(long pBytesRead, long pContentLength, int pItems) {
					// 获得上传的百分比  100  4   
					long percent = pBytesRead*100/pContentLength;
					
					if(percent==temp)
						return ;
					//System.out.println("已上传:" + percent + "%");
					
					request.getSession().setAttribute("percent", percent);
					
					temp = percent;
				}
			});
			



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值