java钟使用dwr_使用DWR框架实现服务器反推技术

使用DWR框架实现服务器反推技术

[1 背景 “服务器推送技术”(ServerPushing)是最近Web技术中最热门的一个流行术语。它是继“Ajax”之后又一个倍受追捧的Web技术。“服务器推送技术”最� ...]看到使用Ajax的框架DWR可以实现此功能。现将有关代码贴出来,供大家参考。

示例1:由服务器每一秒钟给发一个信息过来客户端

1、  首先将dwr.jar文件放到工程的lib中。

2、  配置web.xml

xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

dwr-invoker

org.directwebremoting.servlet.DwrServlet

调试DWR,发布系统时应将其设为false

debug

true

使用服务器推技术(反转AJAX)

activeReverseAjaxEnabled

true

initApplicationScopeCreatorsAtStartup

true

maxWaitAfterWrite

100

4

This is the description of my J2EE component

This is the display name of my J2EE component

ServerClientServlet

com.lhq.ServerClientServlet

This is the description of my J2EE component

This is the display name of my J2EE component

ServerClinetStartServlet

com.lhq.ServerClinetStartServlet

dwr-invoker

/dwr/*

ServerClientServlet

/servlet/ServerClientServlet

ServerClinetStartServlet

/servlet/ServerClinetStartServlet

index.jsp

3、  新建两个jsp页面

启动页server_client_test_start.jsp代码如下:

server_client_test_start.jsp

点击此处开启服务器反推技术

结果显示页server_client_test1.jsp代码如下:

server_client _test.jsp

dwr.engine.setActiveReverseAjax(true);//起动dwr反推,在接收页面这句必须要有

function showMessage(msg)

{

//DWRUtil.setValue("showMsg",msg);

//换行转回车

var haha="
";

haha=haha.replace('
','\n');

var showMsg=document.getElementById("showMsg");

showMsg.value=showMsg.value+haha+msg;

}

下面的信息来自于服务器端

4、 创建包com.lhq

5、  创建ServerClientServlet.servlet,代码如下:

package com.lhq;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Collection;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.directwebremoting.ScriptBuffer;

import org.directwebremoting.ScriptSession;

import org.directwebremoting.ServerContext;

import org.directwebremoting.ServerContextFactory;

public class ServerClientServlet extends HttpServlet {

/**

* Constructor of the object.

*/

public ServerClientServlet() {

super();

}

/**

* Destruction of the servlet.

*/

public void destroy() {

super.destroy(); // Just puts "destroy" string in log

// Put your code here

}

/**

* The doGet method of the servlet.

*

* This method is called when a form has its tag value method equals to get.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

//WebContext wctx1 =  WebContextFactory.get();

ServerContext wctx = ServerContextFactory.get(getServletContext());

Collection sessions = wctx.getAllScriptSessions();

// Collection sessions =

// wctx.getScriptSessionsByPage("/poject/test.html");

request.setCharacterEncoding("ISO-8859-1");

response.setCharacterEncoding("ISO-8859-1");

String msg = new String(request.getParameter("msg").getBytes("ISO-8859-1"),"GBK");

if(msg==null)

{

msg = "服务器没有值出现";

}

ScriptBuffer script = new ScriptBuffer();

script.appendScript("showMessage(").appendData(msg)

.appendScript(");");

for (ScriptSession session : sessions) {

session.addScript(script);

}

}

/**

* The doPost method of the servlet.

*

* This method is called when a form has its tag value method equals to post.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out

.println(""-//W3C//DTD HTML 4.01 Transitional//EN\">");

out.println("");

out.println(" 

A Servlet");

out.println(" 

");

out.print("    This is ");

out.print(this.getClass());

out.println(", using the POST method");

out.println("  ");

out.println("");

out.flush();

out.close();

}

/**

* Initialization of the servlet.

*

* @throws ServletException if an error occurs

*/

public void init() throws ServletException {

// Put your code here

}

}

6、创建ServerClinetStartServlet.servlet,代码如下:

package com.lhq;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class ServerClinetStartServlet extends HttpServlet {

/**

* Constructor of the object.

*/

public ServerClinetStartServlet() {

super();

}

/**

* Destruction of the servlet.

*/

public void destroy() {

super.destroy(); // Just puts "destroy" string in log

// Put your code here

}

/**

* The doGet method of the servlet.

*

* This method is called when a form has its tag value method equals to get.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

ServerClientStart scsObj=new ServerClientStart();

scsObj.start();

}

/**

* The doPost method of the servlet.

*

* This method is called when a form has its tag value method equals to post.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request,response);

}

/**

* Initialization of the servlet.

*

* @throws ServletException if an error occurs

*/

public void init() throws ServletException {

// Put your code here

}

}

6、 创建类ServerClientStart.class

package com.lhq;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.MalformedURLException;

import java.net.URL;

import java.util.Timer;

public class ServerClientStart extends Thread{

public ServerClientStart()

{

super();

}

public void run()

{

int i=1;

while(true)

{

try{

this.sleep(1000);

String str="第"+(i++)+"秒";

URL url=new URL("http://10.180.84.38/dwrTest/servlet/ServerClientServlet?msg="+str);//这里的http://10.180.84.38/dwrTest/改为本机和自己建的工程

BufferedReader br=new BufferedReader(new InputStreamReader(url.openStream()));

br.close();

}catch(Exception ex){

this.stop();

break;

}

}

}

}

[1        背景“服务器推送技术”( ServerPushing)是最近 Web技术中最热门的一个流行术语。它是继“ Ajax ”之后又一个倍受追捧的 Web技术。“服务器推送技术”最近的流

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值