DWR3.0推送技术实现QQ即时通信功能1

DWR框架:direct web remoting的缩写;

它是一个远程服务器端的Ajax开源框架,其能改善web界面与java类的交互。它可以允许在浏览器里的代码使用运行在WEB服务器上的JAVA函数,就像它就在浏览器里一样。

首先实现实现Java函数调用

1.新建web工程:

2.导入jar包:引入dwr.jar和commons-logging-1.1.3.jar

3.在web.xml中加入dwr使用功能

<servlet>  
        <servlet-name> dwr-invoker </servlet-name>  
        <servlet-class> org.directwebremoting.servlet.DwrServlet</servlet-class > 
        <init-param>  
            <param-name> debug</param-name > 
            <param-value> true</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>
</servlet> 

<servlet-mapping>  
        <servlet-name> dwr-invoker </servlet-name>  
        <url-pattern>/dwr/*</url-pattern> 
</servlet-mapping>

4.在src下新建MessagePush类

package com.DWR.dwr;

public class MessagePush {
	public void sendMessage(){
		System.out.println("dwr success!");
	}
}

5.在web.xml同一目录下新建dwr.xml配置js函数java类的映射关系。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC
    "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN"
    "http://www.getahead.ltd.uk/dwr/dwr30.dtd">
    <dwr>
         <allow>
               <create creator="new" javascript="MessagePush">
                 <param name="class" value="com.trigo.biz.MessagePush"/>
              </create>
          </allow>
     </dwr> 


6.在index.jsp中写入js逻辑(该处使用到jquery,请自行添加):

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getcontextpath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<Meta http-equiv="pragma" content="no-cache">
	<Meta http-equiv="cache-control" content="no-cache">
	<Meta http-equiv="expires" content="0">    
	<Meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<Meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<script src="jquery-1.7.js"></script>
	<script type= "text/javascript" src ="dwr/util.js"></script>     
     <script type="text/javascript" src= "dwr/engine.js"></script >   
     <script type="text/javascript" src= "dwr/interface/messagePush.js" ></script>  
	
	<script>
		$('document').ready(function(){
			messagePush.sendMessage();
		});
	</script>
  </head>
  
  <body>
    This is my JSP page. <br>
  </body>
</html>

说明:jsp文件中必须引入几个js,它们都是隐含存在的,不用考虑它们在哪儿。其中engine.js和util.js是固定的。另外的一个js的名称就是dwr.xml中配置的类名。

相关文章

IE6是一个非常老旧的网页浏览器,虽然现在很少人再使用它,但...
PHP中的count()函数是用来计算数组或容器中元素的个数。这个...
使用 AJAX(Asynchronous JavaScript and XML)技术可以在不...
Ajax(Asynchronous JavaScript and XML)是一种用于改进网页...
本文将介绍如何通过AJAX下载Excel文件流。通过AJAX,我们可以...
Ajax是一种用于客户端和服务器之间的异步通信技术。通过Ajax...