- 浏览: 195459 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
se34218:
我想问一下,为什么插入CLOB字段的时候要先插入空CLOB,e ...
利用JDBC操作Oracle CLOB和BLOB类型数据 -
tinger1:
狂赞楼主!
GWT通信机制初探 -
skygege20:
2015-7-24 16:09:14 org.apache.c ...
Java中禁止的包名(Prohibited package name) -
skygege20:
那怎么解决这种问题呢?
Java中禁止的包名(Prohibited package name) -
javashiting:
看看 看看 看看 看看
Struts+Hibernate/JDBC+ExtJS实现人力资源管理系统(一)
GWT RPC:GWT提供类似Swing的界面开发模式(基于UI组件重用,事件注册及监听等机制),遵循此开发模式的客户端Java代码将被编译为Javascript代码(并优化,压缩),以运行于客户端浏览器中。然而,客户端Java代码不仅仅包含即将呈现在HTML页面中的UI元素,还包含提供服务的接口和相对应的代理接口(实现异步调用服务),服务接口声明即将被
客户端通过RPC调用的方法。服务器端实现该方法来提供具体服务。服务架构参考下图:
服务接口必须继承一个空接口RemoteService:
服务接口的注释@RemoteServiceRelativePath("address")指定了代理接口调用的服务的相对路径(通常为servlet访问路径);
服务实现类
实现类所继承的RemoteServiceServlet是GWT提供的核心Servlet处理类,该类接收客户请求,反序列化RPC请求数据包(GWT提供了序列化及反序列化类库),将反序列化之后的数据交由开发者做业务处理,处理结果将被序列化并组织为响应对象返回客户端。以下为该Servlet处理类的对请求的处理过程:
onBeforeRequestDeserialized()和onAfterResponseSerialized()由实现类继承,以做定制化处理。
以下是从客户端获取的请求数据包及请求属性:
[INFO] Received Serialized Request Diagram: 5|0|4|http://localhost:1947/gmap/|A16261BA4B0DD6867308FEA211E4BEC2|com.wipro.gmap.client.AddressService|getAddresses|1|2|3|4|0|
[INFO] ContentType: text/x-gwt-rpc; charset=utf-8 ServletPath: /gmap/address ContextPath: Protocol: HTTP/1.1 Remote Address: 127.0.0.1 Remote Host: 127.0.0.1:1949 Remote User: null
[Header Information]:
[Host]: localhost:1947 [Connection]: keep-alive [User-Agent]: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/532.2 (KHTML, like Gecko) Chrome/4.0.222.5 Safari/532.2 [Referer]: http://localhost:1947/gmap/hosted.html?gmap [Accept]: */* [Accept-Encoding]: gzip,deflate [Accept-Language]: en-US,en;q=0.8 [Accept-Charset]: UTF-8,*;q=0.5 [Content-Length]: 127 [Origin]: http://localhost:1947 [X-GWT-Module-Base]: http://localhost:1947/gmap/ [Content-Type]: text/x-gwt-rpc; charset=utf-8 [X-GWT-Permutation]: HostedMode
以下为处理后即将返回客户端的数据包(JSON数据格式):
[INFO] Serialized Response Diagrm to be sent: //OK[7,13,0.0,3.0,5,12,11,2,7,10,0.0,2.0,5,9,8,2,7,6,0.0,1.0,5,4,3,2,3,1,["java.util.ArrayList/3821976829","com.wipro.gmap.client.Address/1028263900","Golden Gate Bridge","San Francisco","","california","611756","Main St","Oxford","New York","Zhonghe","Chengdu","Sichuan"],0,5]
该数据返回客户端后将触发回调方法,回调方法定义在入口类(EntryPoint)中:
服务接口及相应的异步接口由GWT编译至客户端Javascript中,由GWT Plugin根据服务器地址及端口,模块(Module),服务调用的页面来对服务器进行RPC调用。参考编译后的客户端JS代码(GWT Plugin负责连接服务器,调用服务端RPC服务):
客户端通过RPC调用的方法。服务器端实现该方法来提供具体服务。服务架构参考下图:
服务接口必须继承一个空接口RemoteService:
//提供getAddresses()服务的接口类 import java.util.List; import com.google.gwt.user.client.rpc.RemoteService; import com.google.gwt.user.client.rpc.RemoteServiceRelativePath; @RemoteServiceRelativePath("address") public interface AddressService extends RemoteService{ public List<Address> getAddresses(); } /*--------------------------------------------------------*/ //代理接口,提供异步调用服务 import java.util.List; import com.google.gwt.user.client.rpc.AsyncCallback; public interface AddressServiceAsync { public void getAddresses(AsyncCallback<List<Address>> callback); }
服务接口的注释@RemoteServiceRelativePath("address")指定了代理接口调用的服务的相对路径(通常为servlet访问路径);
服务实现类
public class AddressServiceImpl extends RemoteServiceServlet implements AddressService{ private static final long serialVersionUID = 7819604306802209305L; //实现从数据库查询地理信息的数据集 @Override public List<Address> getAddresses(){ List<Address> addr_list = new ArrayList<Address>(); DBConnectionPool pool = new DBConnectionPool("pool01", "com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/db?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8", "admin", "admin123", 5); System.out.println("[INFO] Connection Pool created!"); ResultSet rs = null; String sqlStmt = "SELECT addr_id,state,city,address,zip,description FROM address"; try { Connection conn = pool.getConnection(); System.out.println("[INFO] Connection to DB established!"); PreparedStatement ps = conn.prepareStatement(sqlStmt); rs = ps.executeQuery(); if(rs == null){ System.out.println("[ERROR] Failed to query data from database!"); return null; } System.out.println("[INFO] Execution of query for address list finished!"); Address addr = null; int cnt = 0; while(rs.next()){ addr = new Address(); ++cnt; System.out.println("[INFO] No. " + cnt + " of the result records."); addr.setId(rs.getLong("addr_id")); addr.setState(rs.getString("state") == null ? "N/A" : rs.getString("state")); addr.setCity(rs.getString("city")); addr.setAddress(rs.getString("address")); addr.setZip(rs.getString("zip")); addr.setDescription(rs.getString("description") == null ? "N/A" : rs.getString("description") ); addr_list.add(addr); } } catch (SQLException e) { System.out.println("[ERROR] Failed to get field value from resultSet!"); e.printStackTrace(); } return addr_list; } @Override public void onBeforeRequestDeserialized(String serializedRequest){ System.out.println("[INFO] Received Serialized Request Diagram: " + serializedRequest); } @Override public void onAfterResponseSerialized(String serializedResponse) { System.out.println("[INFO] Serialized Response Diagram to be sent: " + serializedResponse); } }
实现类所继承的RemoteServiceServlet是GWT提供的核心Servlet处理类,该类接收客户请求,反序列化RPC请求数据包(GWT提供了序列化及反序列化类库),将反序列化之后的数据交由开发者做业务处理,处理结果将被序列化并组织为响应对象返回客户端。以下为该Servlet处理类的对请求的处理过程:
/** * Standard HttpServlet method: handle the POST. * * This doPost method swallows ALL exceptions, logs them in the * ServletContext, and returns a GENERIC_FAILURE_MSG response with status code * 500. * * @throws ServletException * @throws SerializationException */ @Override public final void processPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException, SerializationException { // Read the request fully. String requestPayload = readContent(request); // Let subclasses see the serialized request. onBeforeRequestDeserialized(requestPayload); // Invoke the core dispatching logic, which returns the serialized // result. String responsePayload = processCall(requestPayload); // Let subclasses see the serialized response. onAfterResponseSerialized(responsePayload); // Write the response. writeResponse(request, response, responsePayload); } //本方法处理反序列化后的请求数据 public String processCall(String payload) throws SerializationException { try { RPCRequest rpcRequest = RPC.decodeRequest(payload, this.getClass(), this); onAfterRequestDeserialized(rpcRequest); return RPC.invokeAndEncodeResponse(this, rpcRequest.getMethod(), rpcRequest.getParameters(), rpcRequest.getSerializationPolicy(), rpcRequest.getFlags()); } catch (IncompatibleRemoteServiceException ex) { log( "An IncompatibleRemoteServiceException was thrown while processing this call.", ex); return RPC.encodeResponseForFailure(null, ex); } } /*---------------------- RPC.java -------------------------*/ //RPC类中定义的方法invokeAndEncodeResponse,采用反射机制来调用具体的服务方法。 public static String invokeAndEncodeResponse(Object target, Method serviceMethod, Object[] args, SerializationPolicy serializationPolicy, int flags) throws SerializationException { if (serviceMethod == null) { throw new NullPointerException("serviceMethod"); } if (serializationPolicy == null) { throw new NullPointerException("serializationPolicy"); } String responsePayload; try { Object result = serviceMethod.invoke(target, args); responsePayload = encodeResponseForSuccess(serviceMethod, result, serializationPolicy, flags); } catch (IllegalAccessException e) { SecurityException securityException = new SecurityException( formatIllegalAccessErrorMessage(target, serviceMethod)); securityException.initCause(e); throw securityException; } catch (IllegalArgumentException e) { SecurityException securityException = new SecurityException( formatIllegalArgumentErrorMessage(target, serviceMethod, args)); securityException.initCause(e); throw securityException; } catch (InvocationTargetException e) { // Try to encode the caught exception // Throwable cause = e.getCause(); responsePayload = encodeResponseForFailure(serviceMethod, cause, serializationPolicy, flags); } return responsePayload; }
onBeforeRequestDeserialized()和onAfterResponseSerialized()由实现类继承,以做定制化处理。
以下是从客户端获取的请求数据包及请求属性:
引用
[INFO] Received Serialized Request Diagram: 5|0|4|http://localhost:1947/gmap/|A16261BA4B0DD6867308FEA211E4BEC2|com.wipro.gmap.client.AddressService|getAddresses|1|2|3|4|0|
[INFO] ContentType: text/x-gwt-rpc; charset=utf-8 ServletPath: /gmap/address ContextPath: Protocol: HTTP/1.1 Remote Address: 127.0.0.1 Remote Host: 127.0.0.1:1949 Remote User: null
[Header Information]:
[Host]: localhost:1947 [Connection]: keep-alive [User-Agent]: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/532.2 (KHTML, like Gecko) Chrome/4.0.222.5 Safari/532.2 [Referer]: http://localhost:1947/gmap/hosted.html?gmap [Accept]: */* [Accept-Encoding]: gzip,deflate [Accept-Language]: en-US,en;q=0.8 [Accept-Charset]: UTF-8,*;q=0.5 [Content-Length]: 127 [Origin]: http://localhost:1947 [X-GWT-Module-Base]: http://localhost:1947/gmap/ [Content-Type]: text/x-gwt-rpc; charset=utf-8 [X-GWT-Permutation]: HostedMode
以下为处理后即将返回客户端的数据包(JSON数据格式):
引用
[INFO] Serialized Response Diagrm to be sent: //OK[7,13,0.0,3.0,5,12,11,2,7,10,0.0,2.0,5,9,8,2,7,6,0.0,1.0,5,4,3,2,3,1,["java.util.ArrayList/3821976829","com.wipro.gmap.client.Address/1028263900","Golden Gate Bridge","San Francisco","","california","611756","Main St","Oxford","New York","Zhonghe","Chengdu","Sichuan"],0,5]
该数据返回客户端后将触发回调方法,回调方法定义在入口类(EntryPoint)中:
//final ArrayList<Address> addressList = new ArrayList<Address>(); //final ListBox addresses = new ListBox(); AddressServiceAsync as = (AddressServiceAsync) GWT .create(AddressService.class); as.getAddresses(new AsyncCallback<List<Address>>() { @Override public void onSuccess(List<Address> result) { Window.alert("[INFO] Succeed to retrieve data from db!"); Iterator<Address> it = result.iterator(); Address address = null; while(it.hasNext()){ address = new Address(); address = it.next(); System.out.println("[INFO] Address: " + address.getAddress()); addresses.addItem(address.getAddress()); addressList.add(address); } System.out.println("[INFO] Data in addresses ListBox:"); addresses.setVisibleItemCount(result.size()); addressGrid.setAddress(addressList.get(0)); System.out.println("[INFO] [GMap.getAddresse()] Retrieved addresses already be populated into the ListBox!"); } @Override public void onFailure(Throwable caught) { String msg = "[ERROR] Failed to retrieve data from database!"; Window.alert(msg); System.out.println(msg); GWT.log(msg, caught); } });
服务接口及相应的异步接口由GWT编译至客户端Javascript中,由GWT Plugin根据服务器地址及端口,模块(Module),服务调用的页面来对服务器进行RPC调用。参考编译后的客户端JS代码(GWT Plugin负责连接服务器,调用服务端RPC服务):
if (!plugin) { // try searching for a v1 plugin for backwards compatibility var found = false; for (var i = 0; i < pluginFinders.length; ++i) { try { plugin = pluginFinders[i](); if (plugin != null && plugin.connect($hosted, $moduleName, window)) { return; } } catch (e) { } } loadIframe("http://gwt.google.com/missing-plugin"); } else { if (plugin.connect(url, topWin.__gwt_SessionID, $hosted, $moduleName, $hostedHtmlVersion)) { window.onUnload = function() { try { // wrap in try/catch since plugins are not required to supply this plugin.disconnect(); } catch (e) { } }; } else { if (errFn) { errFn(modName); } else { alert("Plugin failed to connect to hosted mode server at " + $hosted); loadIframe("http://code.google.com/p/google-web-toolkit/wiki/TroubleshootingOOPHM"); } } }
发表评论
-
uniVocity-parsers:一款强大的CSV/TSV/定宽文本文件解析库(Java)
2015-04-27 00:21 5853uniVocity-parsers 是一个开源的Java项目。 ... -
Java压缩和解压缩
2010-06-17 18:40 2151压缩需求:压缩文件夹下所有具有统一文件名规范的普通文件,同时添 ... -
Java中禁止的包名(Prohibited package name)
2009-08-19 00:18 36338由于定义了以java开始的包(java.mypackage), ... -
Log4j for Tomcat5.5
2009-05-31 13:41 2341在Tomcat5.5之前,可以通过server.xml下< ... -
利用JDBC操作Oracle CLOB和BLOB类型数据
2009-05-11 09:41 10217对LOB数据(包括CLOB BLOB NCLOB BFILE类 ... -
解决eclipse3.4下错误:Exception starting filter struts2
2009-05-04 12:26 1650项目中用到struts2,跟以前用到过的struts有很大区别 ... -
JDBC高级编程:操作结果集
2009-04-23 10:26 4806自JDBC2.0之后,可滚动(Scrollabl ... -
Java动态绑定探讨
2009-04-20 09:26 1386本文转 “子 孑” 博客:http://zhangjunhd. ... -
区别类方法、实例方法和字段的调用
2009-04-16 15:41 2204本例根据一个实例来说明类方法和实例方法调用的区别,以 ... -
Tomcat类加载器体系结构
2009-04-03 22:09 5638与Java语言相似,Tomcat提供了分级类加载机制,当 ... -
多线程的弹球游戏实现
2009-04-02 00:16 2123大二开始学习Java的时候 ... -
Java类加载器
2009-03-31 21:06 4213Java是介于编译型和解释型之间的编程语言,编译器将jav ... -
JSON
2008-12-29 22:28 1679JSON建构于两种结构: ...
相关推荐
GWT学习经验及实例指导,详细说明了GWT的安装以及使用步骤,结合实例,简单易懂
gwt 练习gwt 练习gwt 练习gwt 练习
EXT-GWT2.0.1+API+DOC gwtdesigner Gwt-Ext基础-中级-进阶 GWT快速开发 GWT入门 GWT中文教程
fwefwefwGWT工具GWT工具GWT工具GWT工具GWT工具GWT工具
gwt 入门,是你开始学习gwt的大道 安装之后的目录结构为: C:\程序开发\Java\gwt-windows-1.4.59 doc(文档目录,开发文档和Java API文档) samples(示例代码目录,非常有名的KitchenSink示例代码即在此目录中)...
Gwt中文手册,GWt入门Gwt中文手册,GWt入门Gwt中文手册,GWt入门
GWT(Google Web Toolkit) 是 Google 最近推出的一个开发 Ajax 应用的框架,它支持用 Java 开发和调试 Ajax 应用,本文主要介绍如何利用 GWT 进行 Ajax 的开发。 Ajax技术是当前开发web应用的非常热门的技术,也是Web...
很好的资源,在这边分享下,提供给想要寻找GWT资源的兄弟姐妹们 如果哪位兄弟有好的资源不要忘了分享啊
2. Hello GWT 3. 模块 4. JSNI 5. 在GWT中使用XML 6. GWT控件详解 7. 使用GWT控件 8. GWT-RPC 9. Ext GWT 10. (实战)俄罗斯方块游戏 11. (实战)费用申请审批流程 12. GWT与Flex整合 13. 图片缓存 CSS Sprite 14. ...
smartgwt最新版本GWT的DEMO 谷歌web工具箱实例
用Maven创建GWT_SmartGWT项目,从而可利用Maven进行管理,以及持续集成。
GWT的eclipse4.4离线开发插件,国内现在没有
GWT学习时的一些小例子,和大家分享,GWT学习时的一些小例子,和大家分享
基本概念及GWT结构及命令,开发等的介绍
这个文件是关于GWT开发的,属于入门级别的,GWT新手可以下载了看看。
gwt学习资料 java gwt相关学习网站 gwt图解 gwt教学 gwt安装部署 步骤流程