本文目的:
利用开源DWR,实现客户端页面不用刷新就可以显示服务器发送过来的数据。这是一个jsp+tomcat+dwr的简单例子。具体的理论性知识参考dwr官网。
文章最后附带MyEclipse的源工程下载。
准备工作:
1、DWR官网下载3.0的jar包
地址: https://java.net/downloads/dwr/DWR_3.x/3.0_RC2/dwr.jar
2、在web工程中加入所依赖的文件
具体添加依赖文件的过程可以参考:DWR中文文档的第一章“DWR入门”,或者参考DWR官网Getting Started with DWR 中的配置。
依赖的文件有:刚才下载的dwr.jar包、Commons Logging jar
编辑配置文件
1、配置web.xml
将如下的代码添加到WEB-INF/web.xml,在web.xml中所有的<servlet>要放在一起,所有的<servlet-mapping>区域也是一样。关于web.xml的具体配置可以参考上述提供的文档。
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>dwr_test</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <listener> <listener-class>org.directwebremoting.servlet.DwrListener</listener-class> </listener> <servlet> <display-name>DWR Servlet</display-name> <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> <!-- Remove this unless you want to use active reverse ajax --> <init-param> <param-name>activeReverseAjaxEnabled</param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 下面是我在后文将要提到的一个servlet的设置,注意根据自己程序设置servlet-class和servlet-name区域的值 --> <servlet> <servlet-name>MySevlet</servlet-name> <servlet-class>dwr_test.MySevlet</servlet-class> </servlet> <!-- 下面是我在后文将要提到的一个servlet的设置,注意根据自己程序设置url-pattern区域的值 --> <servlet-mapping> <servlet-name>MySevlet</servlet-name> <url-pattern>/server/push</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> </web-app>
2、配置dwr.xml
创建新的xml文件,命名为dwr。具体字段含义请参考上述给出的参考文档。
<?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"> <!-- 要根据自己demo的实际情况具体设置如下的各个字段的值 --> <dwr> <allow> <create creator="new" javascript="service"> <param name="class" value="dwr_test.Service" /> </create> </allow> </dwr>
3、推送程序
//Service.java package dwr_test; /*public class Service { public String sayHello(String yourName) { return "Hello World " + yourName; } }*/ import org.directwebremoting.Browser; import org.directwebremoting.ScriptSessions; import org.directwebremoting.ServerContextFactory; import org.directwebremoting.WebContext; import org.directwebremoting.WebContextFactory; import org.directwebremoting.ui.dwr.Util; import java.text.*; import java.util.*; public class Service{ public static void sendMsg(final String msg){ //WebContext wctx = WebContextFactory.get(); //Browser.withPage("/MyDWRSample/",new Runnable(){ //针对具体的页面first_test.jsp页面进行推送 String page = ServerContextFactory.get().getContextPath() + "/first_test.jsp"; //System.out.println(page); Browser.withPage(page,new Runnable(){ public void run(){ //ScriptSessions.addFunctionCall("show",msg); //messageid字段是向jsp页面推送时所对应的字段,msg是要推送的消息 Util.setValue("messageid",msg); } }); } }这个程序包含的函数sendMsg负责向客户端推送消息。
4、模拟servlet,模拟请求服务
//MyServlet.java,注意工程中这个文件的名字书写错误,好囧 package dwr_test; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /*import org.directwebremoting.ServerContext; import org.directwebremoting.ServerContextFactory; import org.directwebremoting.WebContextFactory.WebContextBuilder; import org.directwebremoting.impl.DefaultWebContextBuilder;*/ public class MySevlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { /* ServerContext sc = ServerContextFactory.get () ; WebContextBuilder webBuilder = new DefaultWebContextBuilder () ; webBuilder.engageThread (sc.getContainer (),request,response) ;*/ doPost(request,response); } protected void doPost(HttpServletRequest request,IOException { // PrintWriter out = response.getWriter(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Service.sendMsg( "time: " + df.format(new Date()) + " aaaaaa"); // out.write("aaaaa"); } }这个servlet用来模拟请求,当请求到来的时候,servlet调用sendMsg方法来向客户端推送当前时间以及字符串“aaaaaa”。
5、客户端页面first_test.jsp
这个页面用来接收服务器的推送。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <Meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script type='text/javascript' src='/dwr_test/dwr/engine.js'></script> <script type='text/javascript' src='/dwr_test/dwr/util.js'></script> <script type='text/javascript' src='/dwr_test/dwr/interface/service.js'></script> <script> function my_onload(){ dwr.engine.setActiveReverseAjax(true); } function show(message){ alert(message); document.getElementById('messageid').value=message; } </script> </head> <body onload="my_onload();"> 从服务端得到的消息:<br /> <input type="text" style="width:230px" id="messageid" value="" /> </body> </html>
程序演示:
输入链接http://localhost:8080/dwr_test/first_test.jsp,打开客户端页面:
浏览器输入链接http://localhost:8080/dwr_test/server/push,模拟请求,这时客户端会显示当前时间以及字符串“aaaaaa":
MyEclipse工程下载:
http://download.csdn.net/detail/u013074465/9338055
本人刚刚接触web的东西,java也是刚开始学习,代码很劣质,希望指教。