- 浏览: 64579 次
- 性别:
- 来自: 长沙
最新评论
-
hyj1254:
说得好啊..
什么是个人核心竞争力 -
hehaibo_job:
楼主,数据库脚本怎么都没啊
Jfreechat实例 -
dazui521:
感动,我找了好久才找到这么好的例子
Jfreechat实例 -
dazui521:
...
Jfreechat实例
DWR一个外国人实现的很有前途的AJAX框架。
多余的话就不说了,请看DWR的例子程序:
web.xml
1<?xml version="1.0" encoding="ISO-8859-1"?>
2<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
3
4<web-app id="dwr">
5
6 <display-name>DWR (Direct Web Remoting)</display-name>
7 <description>A demo of how to call Java on the server directly from Javascript on the client</description>
8
9 <servlet>
10 <servlet-name>dwr-invoker</servlet-name>
11 <display-name>DWR Servlet</display-name>
12 <description>Direct Web Remoter Servlet</description>
13 <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
14 <!--
15 <init-param>
16 <param-name>config</param-name>
17 <param-value>WEB-INF/dwr.xml</param-value>
18 </init-param>
19 -->
20 <init-param>
21 <param-name>debug</param-name>
22 <param-value>true</param-value>
23 </init-param>
24 <init-param>
25 <param-name>scriptCompressed</param-name>
26 <param-value>false</param-value>
27 </init-param>
28 <load-on-startup>1</load-on-startup>
29 </servlet>
30
31 <servlet-mapping>
32 <servlet-name>dwr-invoker</servlet-name>
33 <url-pattern>/dwr/*</url-pattern>
34 </servlet-mapping>
35
36</web-app>servlet(uk.ltd.getahead.dwr.DWRServlet)里:
1 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
2 {
3 try
4 {
5 builder.set(req, resp, getServletConfig(), getServletContext(), container);
6 ServletLoggingOutput.setExecutionContext(this);
7
8 processor.handle(req, resp);//该方法对所有request路径/dwr/*有效,在引用JS的时候,使用这个路径执行dwr生成的javascript代码
finally
11 {
12 builder.unset();
13 ServletLoggingOutput.unsetExecutionContext();
14 }
15 }
index.html
1 <?xml version="1.0" encoding="ISO-8859-1" ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <title>DWR - Test Home</title>
6 <script type='text/javascript' src='dwr/interface/Test.js'></script>
7 <script type='text/javascript' src='dwr/engine.js'></script>
8 <script type='text/javascript' src='dwr/util.js'></script>
9 <script>
10 function init() {
11 // This turns off the no-javascript message
12 document.getElementById("start").style.display = "none";
13 // This checks for file: URLs and loading problems
14 if (window.DWREngine == null || window.DWRUtil == null) {
15 document.getElementById("file").style.display = "block";
16 return;
17 }
18 // DWR setup
19 DWREngine.setErrorHandler(function(message) { alert(message); });
20 DWREngine.setWarningHandler(function(message) { alert(message); });
21 DWRUtil.useLoadingMessage();
22 // Turn on the generic error div
23 $("error").style.display = "block";
24 // Load a message from the server
25 Test.getInsert(load);
26 }
27
28 function load(data) {
29 $("error").style.display = "none";
30 DWRUtil.setValue("reply", data);
31 $("reply").style.display = "block";
32 }
33 </script>
34 </head>
35
36 <body onload="init()">
37 .这一部分经过了SERVLET处理:
<script type='text/javascript' src='dwr/interface/Test.js'></script>
<script type='text/javascript' src='dwr/engine.js'></script>
<script type='text/javascript' src='dwr/util.js'></script>dwrservlet.doPost方法内processor.handle(req, resp)这个方法如下:
1 public void handle(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
2 {
3 String pathInfo = req.getPathInfo();
4 String servletPath = req.getServletPath();
5
6 if (nullPathInfoWorkaround && pathInfo == null)
7 {
8 pathInfo = req.getServletPath();
9 servletPath = HtmlConstants.PATH_ROOT;
10 log.debug("Default servlet suspected. pathInfo=" + pathInfo + "; contextPath=" + req.getContextPath() + "; servletPath=" + servletPath); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
11 }
12
13 if (pathInfo == null ||
14 pathInfo.length() == 0 ||
15 pathInfo.equals(HtmlConstants.PATH_ROOT))
16 {
17 resp.sendRedirect(req.getContextPath() + servletPath + HtmlConstants.FILE_INDEX);
18 }
19 else if (pathInfo.startsWith(HtmlConstants.FILE_INDEX))
20 {
21 index.handle(req, resp);
22 }
23 else if (pathInfo.startsWith(HtmlConstants.PATH_TEST))
24 {
25 test.handle(req, resp);
26 }
27 else if (pathInfo.startsWith(HtmlConstants.PATH_INTERFACE))
28 {
29 iface.handle(req, resp);//这个方法是我们要关注的
}
。。。。。。。
} iface.handle(req, resp);//这个方法是我们要关注的,来自DefaultInterfaceProcessor
1 public void handle(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
2 {
3 String pathinfo = req.getPathInfo();
4 String servletpath = req.getServletPath();
5 if (pathinfo == null)
6 {
7 pathinfo = req.getServletPath();
8 servletpath = HtmlConstants.PATH_ROOT;
9 }
10 String scriptname = pathinfo;
11 scriptname = LocalUtil.replace(scriptname, HtmlConstants.PATH_INTERFACE, HtmlConstants.BLANK);
12 scriptname = LocalUtil.replace(scriptname, HtmlConstants.EXTENSION_JS, HtmlConstants.BLANK);
13 Creator creator = creatorManager.getCreator(scriptname);
14
15 //resp.setContentType("text/javascript");
16 PrintWriter out = resp.getWriter();
17 out.println();
18
19 out.println("function " + scriptname + "() { }"); //从这里开始DWR自动生成javascript
String path = overridePath;
22 if (path == null)
23 {
24 path = req.getContextPath() + servletpath;
25 }
26 out.println(scriptname + "._path = '" + path + "';"); //$NON-NLS-1$ //$NON-NLS-2$
27
28 Method[] methods = creator.getType().getMethods();
29 for (int i = 0; i < methods.length; i++)
30 {
31 Method method = methods[i];
32 String methodName = method.getName();
33
34 // We don't need to check accessControl.getReasonToNotExecute()
35 // because the checks are made by the doExec method, but we do check
36 // if we can display it
37 String reason = accessControl.getReasonToNotDisplay(req, creator, scriptname, method);
38 if (reason != null && !allowImpossibleTests)
39 {
40 continue;
41 }
42
43 // Is it on the list of banned names
44 if (jsutil.isReservedWord(methodName))
45 {
46 continue;
47 }
48
49 out.print('\n');
50 out.print(scriptname + '.' + methodName + " = function("); //$NON-NLS-1$
51 Class[] paramTypes = method.getParameterTypes();
52 for (int j = 0; j < paramTypes.length; j++)
53 {
54 if (!LocalUtil.isServletClass(paramTypes[j]))
55 {
56 out.print("p" + j + ", "); //$NON-NLS-1$ //$NON-NLS-2$
57 }
58 }
59 out.println("callback) {"); //$NON-NLS-1$
60
61 out.print(" DWREngine._execute(" + scriptname + "._path, '" + scriptname + "', '" + methodName + "\', "); //实现javascript调用java内的方法
for (int j = 0; j < paramTypes.length; j++)
63 {
64 if (LocalUtil.isServletClass(paramTypes[j]))
65 {
66 out.print("false, "); //$NON-NLS-1$
67 }
68 else
69 {
70 out.print("p" + j + ", "); //$NON-NLS-1$ //$NON-NLS-2$
71 }
72 }
73 out.println("callback);"); //$NON-NLS-1$
74
75 out.println('}');
76 }
77
78 out.flush();
79 }
多余的话就不说了,请看DWR的例子程序:
web.xml
1<?xml version="1.0" encoding="ISO-8859-1"?>
2<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
3
4<web-app id="dwr">
5
6 <display-name>DWR (Direct Web Remoting)</display-name>
7 <description>A demo of how to call Java on the server directly from Javascript on the client</description>
8
9 <servlet>
10 <servlet-name>dwr-invoker</servlet-name>
11 <display-name>DWR Servlet</display-name>
12 <description>Direct Web Remoter Servlet</description>
13 <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
14 <!--
15 <init-param>
16 <param-name>config</param-name>
17 <param-value>WEB-INF/dwr.xml</param-value>
18 </init-param>
19 -->
20 <init-param>
21 <param-name>debug</param-name>
22 <param-value>true</param-value>
23 </init-param>
24 <init-param>
25 <param-name>scriptCompressed</param-name>
26 <param-value>false</param-value>
27 </init-param>
28 <load-on-startup>1</load-on-startup>
29 </servlet>
30
31 <servlet-mapping>
32 <servlet-name>dwr-invoker</servlet-name>
33 <url-pattern>/dwr/*</url-pattern>
34 </servlet-mapping>
35
36</web-app>servlet(uk.ltd.getahead.dwr.DWRServlet)里:
1 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
2 {
3 try
4 {
5 builder.set(req, resp, getServletConfig(), getServletContext(), container);
6 ServletLoggingOutput.setExecutionContext(this);
7
8 processor.handle(req, resp);//该方法对所有request路径/dwr/*有效,在引用JS的时候,使用这个路径执行dwr生成的javascript代码
finally
11 {
12 builder.unset();
13 ServletLoggingOutput.unsetExecutionContext();
14 }
15 }
index.html
1 <?xml version="1.0" encoding="ISO-8859-1" ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <title>DWR - Test Home</title>
6 <script type='text/javascript' src='dwr/interface/Test.js'></script>
7 <script type='text/javascript' src='dwr/engine.js'></script>
8 <script type='text/javascript' src='dwr/util.js'></script>
9 <script>
10 function init() {
11 // This turns off the no-javascript message
12 document.getElementById("start").style.display = "none";
13 // This checks for file: URLs and loading problems
14 if (window.DWREngine == null || window.DWRUtil == null) {
15 document.getElementById("file").style.display = "block";
16 return;
17 }
18 // DWR setup
19 DWREngine.setErrorHandler(function(message) { alert(message); });
20 DWREngine.setWarningHandler(function(message) { alert(message); });
21 DWRUtil.useLoadingMessage();
22 // Turn on the generic error div
23 $("error").style.display = "block";
24 // Load a message from the server
25 Test.getInsert(load);
26 }
27
28 function load(data) {
29 $("error").style.display = "none";
30 DWRUtil.setValue("reply", data);
31 $("reply").style.display = "block";
32 }
33 </script>
34 </head>
35
36 <body onload="init()">
37 .这一部分经过了SERVLET处理:
<script type='text/javascript' src='dwr/interface/Test.js'></script>
<script type='text/javascript' src='dwr/engine.js'></script>
<script type='text/javascript' src='dwr/util.js'></script>dwrservlet.doPost方法内processor.handle(req, resp)这个方法如下:
1 public void handle(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
2 {
3 String pathInfo = req.getPathInfo();
4 String servletPath = req.getServletPath();
5
6 if (nullPathInfoWorkaround && pathInfo == null)
7 {
8 pathInfo = req.getServletPath();
9 servletPath = HtmlConstants.PATH_ROOT;
10 log.debug("Default servlet suspected. pathInfo=" + pathInfo + "; contextPath=" + req.getContextPath() + "; servletPath=" + servletPath); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
11 }
12
13 if (pathInfo == null ||
14 pathInfo.length() == 0 ||
15 pathInfo.equals(HtmlConstants.PATH_ROOT))
16 {
17 resp.sendRedirect(req.getContextPath() + servletPath + HtmlConstants.FILE_INDEX);
18 }
19 else if (pathInfo.startsWith(HtmlConstants.FILE_INDEX))
20 {
21 index.handle(req, resp);
22 }
23 else if (pathInfo.startsWith(HtmlConstants.PATH_TEST))
24 {
25 test.handle(req, resp);
26 }
27 else if (pathInfo.startsWith(HtmlConstants.PATH_INTERFACE))
28 {
29 iface.handle(req, resp);//这个方法是我们要关注的
}
。。。。。。。
} iface.handle(req, resp);//这个方法是我们要关注的,来自DefaultInterfaceProcessor
1 public void handle(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
2 {
3 String pathinfo = req.getPathInfo();
4 String servletpath = req.getServletPath();
5 if (pathinfo == null)
6 {
7 pathinfo = req.getServletPath();
8 servletpath = HtmlConstants.PATH_ROOT;
9 }
10 String scriptname = pathinfo;
11 scriptname = LocalUtil.replace(scriptname, HtmlConstants.PATH_INTERFACE, HtmlConstants.BLANK);
12 scriptname = LocalUtil.replace(scriptname, HtmlConstants.EXTENSION_JS, HtmlConstants.BLANK);
13 Creator creator = creatorManager.getCreator(scriptname);
14
15 //resp.setContentType("text/javascript");
16 PrintWriter out = resp.getWriter();
17 out.println();
18
19 out.println("function " + scriptname + "() { }"); //从这里开始DWR自动生成javascript
String path = overridePath;
22 if (path == null)
23 {
24 path = req.getContextPath() + servletpath;
25 }
26 out.println(scriptname + "._path = '" + path + "';"); //$NON-NLS-1$ //$NON-NLS-2$
27
28 Method[] methods = creator.getType().getMethods();
29 for (int i = 0; i < methods.length; i++)
30 {
31 Method method = methods[i];
32 String methodName = method.getName();
33
34 // We don't need to check accessControl.getReasonToNotExecute()
35 // because the checks are made by the doExec method, but we do check
36 // if we can display it
37 String reason = accessControl.getReasonToNotDisplay(req, creator, scriptname, method);
38 if (reason != null && !allowImpossibleTests)
39 {
40 continue;
41 }
42
43 // Is it on the list of banned names
44 if (jsutil.isReservedWord(methodName))
45 {
46 continue;
47 }
48
49 out.print('\n');
50 out.print(scriptname + '.' + methodName + " = function("); //$NON-NLS-1$
51 Class[] paramTypes = method.getParameterTypes();
52 for (int j = 0; j < paramTypes.length; j++)
53 {
54 if (!LocalUtil.isServletClass(paramTypes[j]))
55 {
56 out.print("p" + j + ", "); //$NON-NLS-1$ //$NON-NLS-2$
57 }
58 }
59 out.println("callback) {"); //$NON-NLS-1$
60
61 out.print(" DWREngine._execute(" + scriptname + "._path, '" + scriptname + "', '" + methodName + "\', "); //实现javascript调用java内的方法
for (int j = 0; j < paramTypes.length; j++)
63 {
64 if (LocalUtil.isServletClass(paramTypes[j]))
65 {
66 out.print("false, "); //$NON-NLS-1$
67 }
68 else
69 {
70 out.print("p" + j + ", "); //$NON-NLS-1$ //$NON-NLS-2$
71 }
72 }
73 out.println("callback);"); //$NON-NLS-1$
74
75 out.println('}');
76 }
77
78 out.flush();
79 }
发表评论
-
Struts 源码学习之ActionServlet ( 二)
2008-01-15 16:48 1128Struts 源码学习之ActionServlet ( 二) ... -
Struts 源码学习之ActionServlet ( 一)
2008-01-15 16:46 1122权所有:(xiaodaoxiaodao)蓝小刀 xiao ... -
Jakarta的公共连接池实现 - BasicDataSource
2008-01-10 14:52 6959| Jakarta的公共连接池实现 - BasicDa ... -
用dwr封装表单项提交表单
2008-01-02 16:26 4006首先,配置dwr环境,网上很多资料都说得很详细,这里就不写了。 ... -
Java Reflection (JAVA反射)详解
2008-01-02 15:06 975Reflection是Java 程序开发语言的特征之一,它允许 ... -
DOM 解析
2008-01-02 09:30 1059To read and update, create and ... -
hql0
2007-12-29 14:07 845HQL语句。(已更新)2007年06月04日 星期一 18:2 ... -
Hql
2007-12-29 13:54 1096Hib的检索方式 1'导航对象图检索方式。通过已经加载的对象, ... -
翻页例子
2007-12-29 09:38 856个MS SQLServer7数据库 DNS ... -
在JSP中访问数据库大全
2007-12-29 09:33 801这种把数据库逻辑全部放在jsp里未必是好的做法,但是有利于初学 ... -
jsp 调用sql server数据源
2007-12-29 09:31 1273import java.sql.*; import java. ... -
转:j2ee中DAO设计模式
2007-12-26 17:11 940最近参与WEB编程项目,采用STRUTS框架,在处理到数据持久 ... -
getAttribute和getParameter的区别
2007-12-26 14:42 2405getAttribute是取得jsp中 用setAttribu ... -
xml dom 教程
2007-12-26 14:31 804http://61.139.52.111:8090/kj/Ma ... -
在网页中引入其它html页面的几种方法
2007-12-26 11:30 47251.IFrame引入,看看下面的代码 <IFRAME N ... -
Tomcat 的数据库连接池设置与应用(Mysql篇)
2007-12-17 17:24 15031.将数据库驱动程序的JAR文件放在Tomcat的 commo ... -
Tomcat+Mysql连接池
2007-12-17 15:25 2491Tomcat+Mysql连接池2006年10月06日 星期五 ... -
tomcat+mysql
2007-12-17 15:23 965安装tomcat+mysql2007-12-02 19:461 ... -
如何在 JavaScript 中实现拖放(上)
2007-08-18 17:48 871JavaScript擅长于修改页面中的DOM元素,但是我们使用 ... -
如何在 JavaScript 中实现拖放(下)
2007-08-18 17:46 869终于完成了全文的翻译,由于时间比较参促,文章没有过多的校正与润 ...
相关推荐
1、 导入dwr.jar包 2、 在web.xml中配置dwr,如下: <!-- 配置DWR --> <servlet-name>dwr-invoker org.directwebremoting.servlet.DwrServlet <init-param> <param-name>debug</param-name> ...
DWR (Direct Web Remoting) 是一个开源Java库,它允许Web应用程序在浏览器和服务器之间进行实时通信,无需使用插件或复杂的JavaScript框架。DWR简化了AJAX(Asynchronous JavaScript and XML)开发,使开发者可以像...
Direct Web Remoting (DWR) 是一种开源的Java库,它允许JavaScript在浏览器端与服务器端的Java对象进行交互,从而实现动态Web应用程序。DWR的主要功能是通过AJAX技术在客户端和服务器之间进行实时通信,使得用户界面...
DWR(Direct Web Remoting)是一种Java库,它允许JavaScript在Web浏览器中直接调用Java方法,从而实现实时的、双向的通信。在“DWR3实现服务器端向客户端精确推送消息”这一主题中,我们将深入探讨如何利用DWR3进行...
DWR.xml配置文件说明书 1、 建立dwr.xml 配置文件 任何一个dwr.xml的文件都需要包含DWR DOCTYPE的声明行,格式如下: <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" ...
DWR(Direct Web Remoting)是一种Java库,它允许JavaScript在客户端与服务器端进行直接的交互,从而实现在Web应用程序中的Ajax功能。DWR的主要目标是简化前后端的数据交换,提高用户体验,使得Web应用能够像桌面...
DWR(Direct Web Remoting)是一种JavaScript库,它允许JavaScript代码在客户端与服务器端进行直接交互,从而实现实时Web应用程序。在这个特定的场景中,我们利用DWR来实现一个新消息的定时提醒功能,这在许多Web...
这个压缩包包含了DWR的核心组件`engine.js`和辅助库`util.js`,它们是实现DWR功能的关键。 `engine.js`是DWR的核心脚本,它实现了JavaScript和Java之间的远程调用(Remote Procedure Calls, RPC)。这个文件处理了...
DWR (Direct Web Remoting) 是一个开源的Java库,它允许JavaScript在浏览器端与服务器端的Java对象进行交互,实现动态的Web应用。在这个场景中,我们将讨论如何使用DWR来实现简单的文件上传功能。 文件上传是Web...
DWR(Direct Web Remoting)是一种JavaScript到Java的远程调用技术,允许Web应用程序在客户端与服务器之间进行实时通信,而无需刷新整个页面。DWR的出现极大地提升了Web应用的用户体验,因为它允许开发者在前端与...
Direct Web Remoting (DWR) 是一个开源Java库,它允许在浏览器和服务器之间进行安全、高效的异步通信,即所谓的“反向AJAX”或“Comet”技术。DWR使得JavaScript能够调用服务器端的Java方法,就像它们是本地函数一样...
DWR (Direct Web Remoting) 是一种开源Java技术,它允许Web应用程序在浏览器和服务器之间进行实时、双向通信,使得JavaScript可以直接调用服务器端的Java方法,极大地简化了客户端和服务器端的数据交换。本笔记将...
**DWR(Direct Web Remoting)**是一种Java技术,它允许Web应用程序在客户端与服务器之间进行实时通信,而无需刷新整个页面。通过DWR,我们可以使用JavaScript直接调用服务器端的Java方法,实现Ajax(Asynchronous ...
DWR (Direct Web Remoting) 是一个开源的Java库,它允许JavaScript在浏览器端与服务器端的Java对象进行交互,实现动态的Web应用。DWR简化了AJAX(Asynchronous JavaScript and XML)的开发,使得开发者可以像调用...
SpringBoot整合Direct Web Remoting (DWR)是一个常见的技术实践,它允许JavaScript在浏览器端直接调用服务器端的Java方法,极大地增强了Web应用的交互性。在这个过程中,我们通常会结合使用FreeMarker或JSP作为视...
SpringMVC 和 DWR(Direct Web Remoting)是两种在Web开发中用于增强交互性的技术。SpringMVC作为Spring框架的一部分,主要用于构建后端服务,而DWR则是一种JavaScript库,允许JavaScript与Java服务器进行实时通信,...
这个压缩包包含了DWR的三个主要版本:DWR1.0、DWR2.0和DWR3.0的jar包,这些jar包是运行DWR应用的核心组件。 DWR1.0: DWR1.0是DWR项目的早期版本,主要目标是简化Web应用中的异步通信。在这个版本中,DWR提供了一个...
这个压缩包包含了DWR的三个关键组件:DWR3.0.jar,DWR的实例war文件,以及DWR2.0的中文说明文档。以下是关于这些组件的详细知识: 1. **DWR3.0.jar**: DWR3.0.jar是DWR框架的核心库,包含所有必需的类和接口,...
在本教程中,我们将探讨如何利用Direct Web Remoting (DWR) 和Spring框架来实现这样的功能。 DWR是一个开源Java库,它允许Web应用程序在浏览器和服务器之间进行实时的、异步的通信,类似于Ajax的功能,但更加强大。...