1、taskActionType); int returnCurrentPercent; if (taskActionType.equals(getCurrentStatus) HttpSession session=request.getSession(); if(session.getAttribute(counterKeyName)=null) session.setAttribute(,1); else counter= (Integer)session.getAttribute().intValue(); returnCurrentPercent = doSomeOneTask(coun
2、ter); counter+; session.setAttribute(,counter); if(counter 10) session.removeAttribute( returnCurrentPercent = 0; response.setContentType(text/html /* * 清除浏览器中可能的缓存的设置不产生缓存的效果,从而达到立即显示服务器返送的新的数据 */ response.setHeader(Cache-Control, no-cache PrintWriter out = response.getWriter(); out.println(returnC
3、urrentPercent); out.flush(); out.close(); /* 修改该方法为带一个参数*/ private int doSomeOneTask(int counter) return counter*10; public void init() throws ServletException 1.1.2 采用XML进行数据交换实现Web实时任务进度条1、设计客户端页面 taskRequest.jsp(包括CSS和JS) 为了简化实现的过程,本示例将CSS和JS代码都包含在当前的页面中,正常应该分离出来。!DOCTYPE html PUBLIC -/W3C/DTD XH
4、TML 1.0 Transitional/EN http:/www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtdhtml xmlns=/www.w3.org/1999/xhtmlheadmeta http-equiv=Content-Type content=text/html; charset=gb18030 /title任务请求的客户端页面style #displayLoadingChartTextID font-family:Arial; font-weight:bolder; color:black; background-color:#EA
5、EAEA; padding:0px; border-style:none; width:80px; #displayPercentValueTextID text-align:center; border-width:medium;/style/headbody onload=sendAsynchronousRequest(); span id=displayLoadingChartTextID /spandisplayPercentValueTextID/div/bodyscript type=text/javascript language=javascript var xmlHttpRe
6、quest; var currentTotalBarAmount =; function sendAsynchronousRequest() xmlHttpRequest=createXMLHttpRequest(); xmlHttpRequest.onreadystatechange = processXMLHttpResponse_SecondTime; var requestServerURL = $pageContext.request.contextPath/responeTaskProgressServlet.action?taskActionType=getCurrentStat
7、us xmlHttpRequest.open(GET, requestServerURL, true); xmlHttpRequest.send(null); function createXMLHttpRequest() var localXMLHttpRequest; if(window.XMLHttpRequest) /* * * 表示用户的浏览器为fireFox等非IE系列,则应用XMLHttpRequest对象模板创建出对象实例 */ localXMLHttpRequest=new XMLHttpRequest(); else if(window.ActiveXObject) * 表
8、示用户的浏览器为IE系列,则应用ActiveXObject对象模板创建出对象实例 try localXMLHttpRequest=new ActiveXObject(Msxml2.XMLHTTP catch(e) try localXMLHttpRequest=new ActiveXObject(Microsoft.XMLHTTP catch(e) window.alert(你的IE浏览器不支持XMLHttpRequest相关的技术实现! return localXMLHttpRequest; function processXMLHttpResponse_SecondTime() if(xm
9、lHttpRequest.readyState = 4) if (xmlHttpRequest.status = 200) showCurrentTaskProgress(); else if(xmlHttpRequest.status =400)你的请求无效! else if(xmlHttpRequest.status =404) window.alert(你向服务端程序请求的URL不正确,未找到请求的目标文件! else if(xmlHttpRequest.status =500)服务器由于遇到错误而不能完成该请求! else if(xmlHttpRequest.status =505)H
10、TTP版本不受支持! else出现了其它不知名的错误! function showCurrentTaskProgress() 获得服务器端程序返回的XML文档数据所对应的DOM对象 */ var xmlDOMReturnResult=xmlHttpRequest.responseXML; 获得XML文档中指定标签名称的标签中所包含的数据,该数据为任务进度量 var barConterValue =xmlDOMReturnResult.getElementsByTagName(currentPercent)0.firstChild.data; currentTotalBarAmount =cur
11、rentTotalBarAmount + | ; document.getElementById().innerHTML=载入中.+currentTotalBarAmount ;barConterValue +% if (barConterValue 99) window.location.href = # /任务结束时自动跳转到目标页面中 setTimeout(sendAsynchronousRequest(), 1000);/script/html注意:本示例是应用XML传递数据,但应用XML技术会存在有一些问题。其中一个就是延迟浏览器不能立即解析 XML 文档并生成 DOM 模型,所以这
12、会降低某些 Ajax 组件需要的“迅捷”感,特别是在较慢的机器上解析大型文档的时候更是如此。比如“现场搜索”就是一个示例,在这种搜索中,当用户输入搜索术语时,就会从服务器提取搜索结果并显示给用户。对于现场搜索组件来说,迅速地响应输入是非常重要的,但是同时它还需要迅速而持续地解析服务器的响应。 避免使用 XML 的最大原因其实还是在客户端需要应用复杂的DOM API进行XML解析。目前比较好的方法是应用JSON(JavaScript Object Notation),它是一种轻量级的数据交换格式。JSON 可以将JavaScript对象中表示的一组数据转换为字符串,然后就可以在函数之间轻松地传递
13、这个字符串,或者在异步应用程序中将字符串从 Web 客户机传递给服务器端程序。2、修改后的ResponeTaskProgressServlet程序代码 throws ServletException, IOException text/xml out.println(?xml version= 1.0 encoding= UTF-8 ?responsecurrentPercent+returnCurrentPercent+/currentPercent/response/* 也就是动态创建出下面的XML文件xml version= 1.0 encoding= UTF-8 20 private
14、int doSomeOneTask(int counter) /该方法为完成具体任务的模板方法1.1.3 将客户端的AJAX请求改变为Post请求1、修改taskRequest.jsp页面中的JS代码中的请求类型 将如下的代码:var requestServerURL = 改变为如下的代码:var requestServerURL = $pageContext.request.contextPath/responeTaskProgressServlet.actionxmlHttpRequest.open(post xmlHttpRequest.setRequestHeader(,applica
15、tion/x-www-form-urlencodedxmlHttpRequest.send(requestParameters);2、在后台ResponeTaskProgressServlet类中添加一个doPost方法public void doPost(HttpServletRequest request, HttpServletResponse response) doGet(request, response);1.1.4 将JavaScript代码从页面中分离到一个独立的JS文件中1、修改taskRequest.jsp页面src=$pageContext.request.contex
16、tPath/javascript/taskProgressBar.jssendAsynchronousRequest($pageContext.request.contextPath/responeTaskProgressServlet.action,taskActionType=getCurrentStatus2、javascript目录中的taskProgressBar.js文件 var xmlHttpRequest; var requestServerURL; var requestParameters; function sendAsynchronousRequest(requestServerURLString,requestParametersString) requestServerURL=requestServerURLString; requestParameters=requestParametersString; createXMLHttpRequestAndSendRequest(); function createXMLHttpRequestAndSendRequest() xmlHttpRequest=createXMLHttpReques