ImageVerifierCode 换一换
格式:DOCX , 页数:18 ,大小:247KB ,
资源ID:22839411      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/22839411.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(跟我学AJAX技术应用AJAX技术实现Web网站的实时在线用户计数的应用示例第一部分Word格式文档下载.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

跟我学AJAX技术应用AJAX技术实现Web网站的实时在线用户计数的应用示例第一部分Word格式文档下载.docx

1、 private static int onlineUserCounter = 0; /体现共享 public static synchronized void incrementCounter() OnLineCounter.onlineUserCounter+; public static synchronized void reduceCounter() if(OnLineCounter.onlineUserCounter=0) return; OnLineCounter.onlineUserCounter-; public static synchronized int getOnLi

2、neUserCounter() return OnLineCounter.onlineUserCounter;(3)改进的实现方法上面实现的方法是应用static !?它是面向过程技术中的,应该采用面向对象!有没有其他的方法能够满足上面的要求?采用“单例对象”创建技术-某个类只能创建出该类的一个对象实例 private int onlineUserCounter = 0; /不再需要设置为static 类型的变量private static OnLineCounter oneOnLineCounter = null; /(1) private OnLineCounter() /(2)publi

3、c static OnLineCounter newInstance() /(3) if(oneOnLineCounter=null) oneOnLineCounter=new OnLineCounter(); return oneOnLineCounter;/* 下面的所有方法也不再需要设置为static方法*/ public synchronized void incrementCounter() oneOnLineCounter.onlineUserCounter+; public synchronized void reduceCounter() if(oneOnLineCounter

4、.onlineUserCounter=0) oneOnLineCounter.onlineUserCounter-; public synchronized int getOnLineUserCounter() return oneOnLineCounter.onlineUserCounter;2、编程实现对HttpSessionListener事件监听器程序(1)在项目中添加一个HttpSessionListener事件监听器程序类程序类名称为OnLineCounterListener ,程序包名称为com.px1987.webcrm.listener,实现javax.servlet.htt

5、p.HttpSessionListener、javax.servlet.http.HttpSessionAttributeListener接口。(2)将产生出下面的程序结果(3)编程OnLineCounterListener程序类中的功能实现代码package com.px1987.webcrm.listener;import javax.servlet.http.HttpSessionAttributeListener;import javax.servlet.http.HttpSessionBindingEvent;import javax.servlet.http.HttpSession

6、Event;import javax.servlet.http.HttpSessionListener;import com.px1987.webcrm.model.vo.UserInfoVO;import com.px1987.webcrm.util.OnLineCounter;public class OnLineCounterListener implements HttpSessionListener, HttpSessionAttributeListener public OnLineCounterListener() Override public void sessionCrea

7、ted(HttpSessionEvent arg0) /创建会话 OnLineCounter.incrementCounter(); public void sessionDestroyed(HttpSessionEvent arg0) /结束会话 OnLineCounter.reduceCounter(); public void attributeAdded(HttpSessionBindingEvent event) public void attributeRemoved(HttpSessionBindingEvent event) public void attributeRepla

8、ced(HttpSessionBindingEvent arg0) 由于session监听的是HTTP连接,只要有用户与后台服务器连接,就算连接的是一个空白的JSP页面,也会触发session事件,所以此处的session实际上指的是连接数(Connection)。(4)部署该监听器 com.px1987.webcrm.listener.OnLineCounterListener/listener3、在页面中定位显示计数的标签 authorInfo.jsp% page pageEncoding=GB18030% taglib prefix=c uri=!DOCTYPE HTML PUBLIC

9、-/W3C/DTD HTML 4.01 Transitional/ENhtmlheadtitle版权信息页link href=$pageContext.request.contextPath/css/commonStyle.css rel=stylesheet type=text/css /headbodyhr /div id=pageFooter 版权所有:蓝梦集团网络信息资源中心,中国最优秀的J2EE平台开源社区,提供丰富的B2B 和B2C平台下的各种技术资料和在线服务 footerCopyrightA href=# 北京 ICP备0123456789号网站统计站长统计 /span人, s

10、howErrorInfoDivTag style=background-color:#FF9900;display:none/body/html4、测试目前的功能的效果(1)部署该Web应用(2)启动服务器(3)输入http:/127.0.0.1:8080/webcrm/index.jsp并打开多个不同的浏览器来模拟多个不同的用户,系统将能够及时并准确地计数。(4)目前的在线记数功能所存在的问题不能达到“实时”的效果1) 不能实时显示新的在线人数2) 如果某个用户离线(关闭浏览器、会话超时),不能实时显示变化后的新的在线人数因为用户关闭了浏览器窗口就表明用户已经不在线了,所有的对话都应该要结束

11、。但由于服务器端并不能够及时地知道这个状态的变化。此时只要依赖于Session会话过期超时,但导致在线记数将不准确。1.1.3 利用AJAX技术实现“实时动态”显示在线用户数1、JavaScript语言中的定时器 在项目中应用JavaScript语言中的定时器达到定时向服务器查询(异步)在线用户数,每隔一定的时间去获得新的用户记数值。2、最后的页面内容如下rel=script language=javascriptsrc=$pageContext.request.contextPath/javascript/ajaxonLineCounter.js/script type=text/javas

12、cript var xmlHttpRequest; var showErrorInfoDivTag; var showOnLineUserCounterDivTag; var requestServerURL=$pageContext.request.contextPath/onLineUserInfoServlet.action showErrorInfoDivTag=; showOnLineUserCounterDivTag= /每隔1秒钟轮询一次 window.setInterval(showOnLineUserCounter(),1000); function showOnLineUs

13、erCounter() xmlHttpRequest=createXMLHTTPRequestObject(); xmlHttpRequest.onreadystatechange =processXMLHttpResponse; / Post 请求方式的代码 xmlHttpRequest.open(POST, requestServerURL, true); /设置该请求的响应不需要缓存 xmlHttpRequest.setRequestHeader(If-Modified-Since,0);Content-Type, application/x-www-form-urlencoded/模拟

14、表单请求 xmlHttpRequest.send(action=showOnLineUserCounter a href=/a 目前在线人数是:人, 由于在本示例中需要定时向服务器发送请求,因此在并发请求比较高时,将会造成服务器的负载。需要注意用 Ajax 实现代替普通的基于表单的 UI,会大大提高对服务器发出的请求数量。例如,一个普通的 Google Web 搜索对服务器只有一个请求,是在用户提交搜索表单时出现的。而 Google Suggest 试图自动完成搜索术语,它要在用户输入时向服务器发送多个请求。在开发 Ajax 应用程序时,要注意将要发送给服务器的请求数量以及由此造成的服务器负荷

15、。降低服务器负载的办法是,在客户机上对请求进行缓冲并且缓存服务器响应(如果可能的话)。还应该尝试将 Ajax Web 应用程序设计为在客户机上执行尽可能多的逻辑,而不必联络服务器。3、添加外部的ajaxonLineCounter.js文件(1)在项目中添加外部的ajaxonLineCounter.js文件(2)编程该*.js文件中的代码function createXMLHTTPRequestObject() var httpRequestObject; if (window.XMLHttpRequest) / Mozilla, Safari, . httpRequestObject = ne

16、w XMLHttpRequest(); else if (window.ActiveXObject) / IE try httpRequestObject = new ActiveXObject(Microsoft.XMLHTTP catch (e) try httpRequestObject= new ActiveXObject(Msxml2.XMLHTTP catch (e2) window.alert(您所使用的浏览器不支持AJAX技术! return httpRequestObject;function processXMLHttpResponse() if (xmlHttpReque

17、st.readyState = 4)document.getElementById(showErrorInfoDivTag).style.display = if (xmlHttpRequest.status = 200) /检查是否成功接收了服务器响应 parseServerResponseTextToBrowse(); else if(xmlHttpRequest.status = 404)所请求的服务器端的URL不存在! else你所请求的页面发生异常,错误代码为:+xmlHttpRequest.status); else document.getElementById(showErro

18、rInfoDivTag).style.display = inline document.getElementById(showErrorInfoDivTag).innerHTML = 正在向服务器发送请求,请稍等.function parseServerResponseTextToBrowse() /动态显示出在线人数 document.getElementById(showOnLineUserCounterDivTag).innerHTML =xmlHttpRequest.responseText;4、添加会话管理的Servlet组件OnLineUserInfoServlet(1)添加On

19、LineUserInfoServlet程序类,程序包名称为com.px1987.webcrm.servlet(2)URL-Pattern设置为:/onLineUserInfoServlet.action(3)最后产生出下面的结果(4)编程实现OnLineUserInfoServlet程序类中的功能实现方法package com.px1987.webcrm.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.

20、http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class OnLineUserInfoServlet extends HttpServlet public OnLineUserInfoServlet() super(); public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletExcept

21、ion, IOException String requestAction=request.getParameter(action if(requestAction.equals(showOnLineUserCounter) doShowOnLineUserCounter(request,response); public void doShowOnLineUserCounter(HttpServletRequest request, HttpServletResponse response) response.setContentType(text/html;charset=gb2312 PrintWriter out = response.getWriter(); int onlineUserCounter=OnLineCounter.getOnLineUserCounter(); out.print(onlineUserCounter); out.flush(); out.close();

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1