DWR第三篇之逆向Ajax升级
2017-09-02 14:37
417 查看
1. 本示例在第二篇架构基础上添加代码
2. 本示例需要做的是定向推送,那么就需要浏览器进行登录,从而进行定向的推送功能,为了节省时间,这里不做登录模块了,在url后拼接一个参数作为登录标识。在client.jsp开头的地方添加如下代码:
这样,在登录的时候在url后边拼接id=xxx,就可以模拟登录功能,并将登陆信息存在session中。
3. 此外,client.jsp中还要添加如下代码(修改原来的地方)
4. 修改index.jsp内容:
5. 在CoreServlet.java里添加两个方法
6. 开三个窗口进行测试:
http://localhost:8080/dwr_demo/client.jsp?id=zhangsan http://localhost:8080/dwr_demo/client.jsp?id=lisi http://localhost:8080/dwr_demo/
2. 本示例需要做的是定向推送,那么就需要浏览器进行登录,从而进行定向的推送功能,为了节省时间,这里不做登录模块了,在url后拼接一个参数作为登录标识。在client.jsp开头的地方添加如下代码:
1 <% 2 String id = request.getParameter("id"); 3 session.setAttribute("id", id); 4 %>
这样,在登录的时候在url后边拼接id=xxx,就可以模拟登录功能,并将登陆信息存在session中。
3. 此外,client.jsp中还要添加如下代码(修改原来的地方)
1 <script type="text/javascript"> 2 window.onload = function() { 3 //开启逆向Ajax功能 4 dwr.engine.setActiveReverseAjax(true); 5 //开启关闭页面提醒服务器功能 6 dwr.engine.setNotifyServerOnPageUnload(true); 7 //对当前用户进行注册 8 CoreServlet.regist(); 9 } 10 </script>
4. 修改index.jsp内容:
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>dwr_demo</title> 13 <script type='text/javascript' src='dwr/engine.js'></script> 14 <script type='text/javascript' src='dwr/util.js'></script> 15 <script type='text/javascript' src='dwr/interface/CoreServlet.js'></script> 16 </head> 17 18 <body> 19 <input type="button" value="发送" onclick="sayHello();"> 20 <br> 21 <input type="text" id="getHello"> 22 <input type="button" value="发送" onclick="getHello();"> 23 <input type="text" id="getHelloValue"> 24 <br> 25 <input type="text" id="sendMsg"> 26 <input type="button" value="推送" onclick="sendMsg();"> 27 <br> 28 发送目标:<input type="text" id="target"> 29 发送内容:<input type="text" id="msg"> 30 <input type="button" value="发送" onclick="directionalSendMsg();"> 31 </body> 32 <script type="text/javascript"> 33 function sayHello() { 34 CoreServlet.sayHello(); 35 } 36 function getHello() { 37 var name = dwr.util.getValue("getHello"); 38 CoreServlet.getHello(name, function(data) { 39 dwr.util.setValue("getHelloValue", data); 40 }); 41 } 42 function sendMsg() { 43 var msg = dwr.util.getValue("sendMsg"); 44 CoreServlet.send(msg); 45 } 46 function directionalSendMsg() { 47 var target = dwr.util.getValue("target"); 48 var msg = dwr.util.getValue("msg"); 49 CoreServlet.directionalSendMsg(target + ":" + msg); 50 } 51 </script> 52 </html>
5. 在CoreServlet.java里添加两个方法
1 /** 2 * 定向推送消息 3 */ 4 public void directionalSendMsg(String msg) { 5 final String[] param = msg.split(":"); 6 Browser.withAllSessionsFiltered(new ScriptSessionFilter() { 7 @Override 8 public boolean match(ScriptSession session) { 9 boolean flag = param[0].equals(session.getAttribute("key")); 10 return flag; 11 } 12 }, new Runnable() { 13 @Override 14 public void run() { 15 Util.setValue("msg", param[1]); 16 } 17 }); 18 } 19 20 /** 21 * 用户注册 22 */ 23 public void regist() { 24 // 获取当前的scriptSession 25 ScriptSession scriptSession = WebContextFactory.get().getScriptSession(); 26 // 获取HttpSession 并获得其中的userId 27 HttpSession session = WebContextFactory.get().getSession(); 28 String id = (String) session.getAttribute("id"); 29 // 对当前scriptSession的key设置指定的值 30 scriptSession.setAttribute("key", id); 31 }
6. 开三个窗口进行测试:
http://localhost:8080/dwr_demo/client.jsp?id=zhangsan http://localhost:8080/dwr_demo/client.jsp?id=lisi http://localhost:8080/dwr_demo/
相关文章推荐
- DWR之DWR是面向web的RPC(或者AJAX-RPC)
- 最新升级的火狐38.0.6识别ajax调用返回的""空值可能有异常。
- 深入理解ajax系列第三篇——响应解码
- 深入理解ajax系列第三篇——响应解码
- DWR高级主题之反向Ajax(DWR3介绍)
- ajax/dwr/struts实例开发(下)
- 利用 DWR 开发基于 Ajax 的文件上载 portlet
- DWR与AJAX的微妙关系
- 如何使用 Ajax 的DWR框架获得 Session
- Ajax之DWR框架使用小结(1)
- DWR 逆ajax 实现网页聊天
- 第三篇:逆向之快速排序
- Ajax框架 DWR(Easy Ajax for Java)
- 使用DWR反转AJAX的失败经验和教训
- [JavaScript][AJAX][DWR](Dwr教程)dwr_ajax入门教程
- AJAX的HelloWorld--使用DWR
- DWR 制作 ajax 国家地区级联菜单
- Ajax之DWR框架使用小结(1)
- 在Struts 2中使用DWR实现AJAX表单校验。
- 深入理解ajax系列第三篇——响应解码