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

前端之家收集整理的这篇文章主要介绍了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中配置的类名。

原文链接:https://www.f2er.com/ajax/165024.html

猜你在找的Ajax相关文章