ajax调用是浏览器主动请求服务器
反向AJAX调用顾名思义:服务器主动请求浏览器
反向 Ajax 的目的是让服务器将信息推送到客户端。Ajax 请求默认情况下是无状态的,且只能从客户端向服务器端发出请求。您可以通过使用技术模拟服务器端和客户端之间的响应式通信来绕过这一限制。
1.创建一个Web Project项目,导入dwr.jar包。
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Push</display-name> <!-- dwr核心servlet --> <listener> <listener-class>org.directwebremoting.servlet.DwrListener</listener-class> </listener> <servlet> <!-- dwr核心servelt实现类 --> <servlet-name>dwr</servlet-name> <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> <!-- 设置dwr可调式 --> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/classes/dwr.xml</param-value> </init-param> <!-- 设置ajax的反向技术 --> <init-param> <param-name>activeReverseAjaxEnabled</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>initApplicationScopeCreatorsAtStartip</param-name> <param-value>true</param-value> </init-param> <!-- 长连接时保持时间 --> <init-param> <param-name>maxWaitAfterWrite</param-name> <param-value>60</param-value> </init-param> <init-param> <param-name>crossDomainSessionSecurity</param-name> <param-value>false</param-value> </init-param> <init-param> <param-name>allowScriptTagRemoting</param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dwr</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
3.创建一个TestDWR.java类
package com.Metarnet.dwr; import java.text.SimpleDateFormat; import java.util.Date; import org.directwebremoting.Browser; import org.directwebremoting.ScriptSessions; public class TestDWR { public void start(){ if(!client) { client = true; getMessage(); } } public void stop() { if(client) { client = false; } } private static boolean client = false; public void getMessage() { Browser.withCurrentPage(new Runnable(){ public void run() { while(client) { try { SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); ScriptSessions.addFunctionCall("receiveMessage",sf.format(new Date())); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }); } }
4.创建dwr.xml文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd"> <dwr> <allow> <create creator="new" javascript="testDWR" scope="application"> <param name="class" value="com.Metarnet.dwr.TestDWR"></param> </create> </allow> </dwr>
5.创建一个push.jsp文件
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <title>DWR</title> <script src="dwr/interface/testDWR.js"></script> <script src="js/jquery.js"></script> <script src="dwr/engine.js"></script> <script type="text/javascript"> function start(){ dwr.engine.setActiveReverseAjax(true); testDWR.start(); } function receiveMessage(data){ $("#date").val(data); } window.onbeforeunload = function() { testDWR.stop(); } </script> </head> <body onload=""> <input type="button" value="开始" onclick="start()"/> <input id="date" /> </body> </html>
原文链接:https://www.f2er.com/ajax/166099.html