- 浏览: 180434 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
liuweihug:
图说浏览器的缓存原理及缓存方式说明(1) http://www ...
浏览器缓存机制 -
fei33423:
楼主为啥不根据评论改下文章..不然会误导很多人的..
Java5 Concurrent包中的锁机制 -
superscorpio:
根据总结,点击back和forward跟是否缓存无关。这应该是 ...
浏览器缓存机制 -
hzxlb910:
引用详细内容请访问我的新博客地址:http://xiaofen ...
Storm的第一份作业 -
tudopi:
遇到F5和enter刷新效果不一样的问题,看了文章解惑了
浏览器缓存机制
GWT中默认一个service对应一个servlet,这样会使web.xml要配置很多servlet, 不是很方便。我这里通过一个Dispatch servlet来分配service, 这样每个service都成了pojo,使得配置更加方便,也容易测试。
1. Server code
Configure web.xml like
<servlet>
<servlet-name>gwt-dispatch</servlet-name>
<servlet-class>
com.xxxxx.gwt.GwtRpcServiceDispatch
</servlet-class>
<init-param>
<param-name>prefix</param-name>
<param-value>/gsvc</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>gwt-dispatch</servlet-name>
<url-pattern>/gsvc/*</url-pattern>
</servlet-mapping>
prefix参数表示service访问url的根路径
Why it works: Let's see the code of GwtRpcServiceDispatch.java
1 /**
2 * @author xwang
3 * Dispatch the gwt services
4 */
5 public class GwtRpcServiceDispatch extends RemoteServiceServlet {
6 private static final long serialVersionUID = -2797202947611240618L;
7
8 private Injector injector;
9
10 private int beginIndexOfClassName;
11
12 @Override
13 public void init(ServletConfig config) throws ServletException {
14 super.init(config);
15 // get guice injector, it init by ServletContextListener
16 injector = InjectorHolder.getInjector();
17 if (injector == null) {
18 throw new UnavailableException("Guice Injector not found (did you forget to register a "
19 + this.getClass().getSimpleName() + "?)");
20 }
21
22 // set prefix of service url
23 String prefix = config.getInitParameter("prefix");
24 if (prefix == null || prefix.length() == 0) {
25 beginIndexOfClassName = 1;
26 } else if (prefix.startsWith("/")) {
27 beginIndexOfClassName = prefix.length() + 1;
28 } else {
29 beginIndexOfClassName = prefix.length() + 2;
30 }
31 }
这里先要设置Guice Injector, 应为我们的service中用到了guice. 其次设置prefix的长度.
接着就可以根据URL来实例化RomteService,如下:
1 /**
2 * get service implement class's instance
3 * @return service implement class's instance
4 */
5 protected Object getCurrentHandler() {
6 String classPath = getThreadLocalRequest().getRequestURI();
7 // /gsvc/com/xxxxx/bus/domain/service/LandmarkService => com.xxxxx.bus.domain.service.LandmarkService
8 String className = classPath.substring(beginIndexOfClassName);
9 if (className.endsWith("/")) {
10 className = className.substring(0, className.length() - 1);
11 }
12 className = className.replaceAll("/", ".");
13
14 Class<?> clazz = null;
15 try {
16 clazz = Class.forName(className);
17 } catch (ClassNotFoundException e) {
18 e.printStackTrace();
19 throw new IllegalStateException(e.getMessage());
20 }
21
22 return injector.getInstance(clazz);
23 }
当然我们需要重新定义RemoteServiceServlet的主处理方法:
1 // this is the main method of RemoteServiceServlet to process request
2 @Override
3 public String processCall(String payload) throws SerializationException {
4 Object serviceHandler = getCurrentHandler(); //get instance of RemoteService
5
6 try {
7 RPCRequest rpcRequest = RPC.decodeRequest(payload, serviceHandler.getClass(), this);
8 return RPC.invokeAndEncodeResponse(serviceHandler, rpcRequest.getMethod(), rpcRequest.getParameters(), rpcRequest
9 .getSerializationPolicy());
10 } catch (IncompatibleRemoteServiceException ex) {
11 getServletContext().log("An IncompatibleRemoteServiceException was thrown while processing this call.", ex);
12 return RPC.encodeResponseForFailure(null, ex);
13 }
14 }
还要重新定义support方法:
1 // RemoteServiceServlet use this judge the instance
2 public boolean supports(Object handler) {
3 return handler instanceof RemoteService;
4 }
现在我们的RemoteService就成为了一个pojo如:
1 public interface LandmarkService extends RemoteService {
2 ...........
3 }
4
5 public class LandmarkServiceImpl implements LandmarkService {
6 ...........
7 }
2. Client code
gwt client这边需要用
1 ServiceHelper.registerServiceEntryPoint(instance);
获取访问URL.
why it works:
1 /**
2 * register service url
3 * @param svcObj service object
4 * com.xxxxx.bus.domain.service.LandmarkService => /gsvc/com/xxxxx/bus/domain/service/LandmarkService
5 */
6 public static void registerServiceEntryPoint(Object svcObj) {
7 ServiceDefTarget endpoint = (ServiceDefTarget) svcObj;
8 String endpointText = GWT.getTypeName(svcObj);
9 endpointText = endpointText.substring(0, endpointText.indexOf("_Proxy"));
10 endpointText = endpointText.replace('.', '/');
11 endpoint.setServiceEntryPoint("/gsvc/" + endpointText);
12 }
GWT.getTypeName can get the class name of service object
你可以使用如下代码的方式构造你的service
1 public interface LandmarkService extends RemoteService {
2 public static class Initer {
3 private static LandmarkServiceAsync instance;
4 public static LandmarkServiceAsync getInstance(){
5 if (instance == null) {
6 instance = (LandmarkServiceAsync) GWT.create(LandmarkService.class);
7 ServiceHelper.registerServiceEntryPoint(instance);
8 }
9
10 return instance;
11 }
12 }
13 ........
1. Server code
Configure web.xml like
<servlet>
<servlet-name>gwt-dispatch</servlet-name>
<servlet-class>
com.xxxxx.gwt.GwtRpcServiceDispatch
</servlet-class>
<init-param>
<param-name>prefix</param-name>
<param-value>/gsvc</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>gwt-dispatch</servlet-name>
<url-pattern>/gsvc/*</url-pattern>
</servlet-mapping>
prefix参数表示service访问url的根路径
Why it works: Let's see the code of GwtRpcServiceDispatch.java
1 /**
2 * @author xwang
3 * Dispatch the gwt services
4 */
5 public class GwtRpcServiceDispatch extends RemoteServiceServlet {
6 private static final long serialVersionUID = -2797202947611240618L;
7
8 private Injector injector;
9
10 private int beginIndexOfClassName;
11
12 @Override
13 public void init(ServletConfig config) throws ServletException {
14 super.init(config);
15 // get guice injector, it init by ServletContextListener
16 injector = InjectorHolder.getInjector();
17 if (injector == null) {
18 throw new UnavailableException("Guice Injector not found (did you forget to register a "
19 + this.getClass().getSimpleName() + "?)");
20 }
21
22 // set prefix of service url
23 String prefix = config.getInitParameter("prefix");
24 if (prefix == null || prefix.length() == 0) {
25 beginIndexOfClassName = 1;
26 } else if (prefix.startsWith("/")) {
27 beginIndexOfClassName = prefix.length() + 1;
28 } else {
29 beginIndexOfClassName = prefix.length() + 2;
30 }
31 }
这里先要设置Guice Injector, 应为我们的service中用到了guice. 其次设置prefix的长度.
接着就可以根据URL来实例化RomteService,如下:
1 /**
2 * get service implement class's instance
3 * @return service implement class's instance
4 */
5 protected Object getCurrentHandler() {
6 String classPath = getThreadLocalRequest().getRequestURI();
7 // /gsvc/com/xxxxx/bus/domain/service/LandmarkService => com.xxxxx.bus.domain.service.LandmarkService
8 String className = classPath.substring(beginIndexOfClassName);
9 if (className.endsWith("/")) {
10 className = className.substring(0, className.length() - 1);
11 }
12 className = className.replaceAll("/", ".");
13
14 Class<?> clazz = null;
15 try {
16 clazz = Class.forName(className);
17 } catch (ClassNotFoundException e) {
18 e.printStackTrace();
19 throw new IllegalStateException(e.getMessage());
20 }
21
22 return injector.getInstance(clazz);
23 }
当然我们需要重新定义RemoteServiceServlet的主处理方法:
1 // this is the main method of RemoteServiceServlet to process request
2 @Override
3 public String processCall(String payload) throws SerializationException {
4 Object serviceHandler = getCurrentHandler(); //get instance of RemoteService
5
6 try {
7 RPCRequest rpcRequest = RPC.decodeRequest(payload, serviceHandler.getClass(), this);
8 return RPC.invokeAndEncodeResponse(serviceHandler, rpcRequest.getMethod(), rpcRequest.getParameters(), rpcRequest
9 .getSerializationPolicy());
10 } catch (IncompatibleRemoteServiceException ex) {
11 getServletContext().log("An IncompatibleRemoteServiceException was thrown while processing this call.", ex);
12 return RPC.encodeResponseForFailure(null, ex);
13 }
14 }
还要重新定义support方法:
1 // RemoteServiceServlet use this judge the instance
2 public boolean supports(Object handler) {
3 return handler instanceof RemoteService;
4 }
现在我们的RemoteService就成为了一个pojo如:
1 public interface LandmarkService extends RemoteService {
2 ...........
3 }
4
5 public class LandmarkServiceImpl implements LandmarkService {
6 ...........
7 }
2. Client code
gwt client这边需要用
1 ServiceHelper.registerServiceEntryPoint(instance);
获取访问URL.
why it works:
1 /**
2 * register service url
3 * @param svcObj service object
4 * com.xxxxx.bus.domain.service.LandmarkService => /gsvc/com/xxxxx/bus/domain/service/LandmarkService
5 */
6 public static void registerServiceEntryPoint(Object svcObj) {
7 ServiceDefTarget endpoint = (ServiceDefTarget) svcObj;
8 String endpointText = GWT.getTypeName(svcObj);
9 endpointText = endpointText.substring(0, endpointText.indexOf("_Proxy"));
10 endpointText = endpointText.replace('.', '/');
11 endpoint.setServiceEntryPoint("/gsvc/" + endpointText);
12 }
GWT.getTypeName can get the class name of service object
你可以使用如下代码的方式构造你的service
1 public interface LandmarkService extends RemoteService {
2 public static class Initer {
3 private static LandmarkServiceAsync instance;
4 public static LandmarkServiceAsync getInstance(){
5 if (instance == null) {
6 instance = (LandmarkServiceAsync) GWT.create(LandmarkService.class);
7 ServiceHelper.registerServiceEntryPoint(instance);
8 }
9
10 return instance;
11 }
12 }
13 ........
发表评论
-
hibernate启动时的一个小问题
2010-01-07 21:39 1307测试时报错但测试可正常运行javax.naming.NoIni ... -
JRobin简介
2009-03-20 10:49 1309做过网络管理的人,对于MRTG应该不算陌生 ... -
Java中assertion的使用
2009-03-18 16:06 2270assertion的使用是一个复杂的问题,因为这将 ... -
监控系统实现设计
2009-03-18 14:37 0做了半年的网络监控系统,研究过一些开源的解决方案(如:Open ... -
JVM调优总结
2009-03-03 10:40 1286堆大小设置 JVM 中最大堆大小有三方面限制:相关操作系统 ... -
Java Reference的简单总结
2009-02-18 10:45 1181介绍 J ava 2 平台中引入 java.lang.re ... -
Java5 Concurrent包中的锁机制
2009-02-16 16:19 3121JDK1.5以后加入了concurrent包, ... -
该选择哪个Java Map对象?
2009-01-04 16:48 1233HashMap, Hashtable, LinkedHash ... -
Java编码规范
2008-12-08 11:21 10191 介绍(Introduction) 1.1 为什么要有编码规 ... -
在OSGi中运行Servlet
2008-06-13 10:05 2362这几天在研究OSGi这个东东,稍有成果。 什么是OSGi? 简 ... -
让Tapestry5支持中文
2007-04-17 13:18 2339相信很多使用Tapestry5的人和我一样,碰到中文编码的问题 ... -
Struts开发流程
2007-04-06 14:47 2677开发流程如下:1.收集 ... -
重构原则(Java)
2007-04-06 14:44 25391995,Auer曾在文献[AUER95]中指出: 1.应当根 ... -
使用JACOB控制Powerpoint
2007-04-03 16:52 3188什么是JACOB? JACOB是一个JAVA和COM之间的桥接 ... -
为Java ME程序添加蓝牙文本协议
2007-04-03 16:48 1877这篇文章的副标题是: ... -
使用MIDPLogger进行在机调试
2007-04-03 16:47 1046在进行手机蓝牙远程控制的开发中,需要通过蓝牙控制PC,这个过程 ... -
在测试MIDlet时需要考虑的一些重要方面
2007-04-03 16:45 1154在测试MIDlet时需要考虑的一些重要方面: 即使运行环境 ... -
MIDlet的事件模型
2007-04-03 16:44 1117javax.mircoedition.lcdui包实现了一个同 ... -
Java中实现鼠标模拟与键盘映射
2007-04-03 16:41 2404Java SDK 1.3以后实现了Robot类。此类用于为测试 ... -
学习java要经常看的网站[转]
2007-04-03 16:40 1299http://java.sun.com/developer/b ...
相关推荐
gwt-servlet.jar 最新版,由于文件太大、不可以上传、有需要可以留言、整包分享给你、
gwt-servlet-2.3.0.jar
gwt-servlet-1.4.61.jar
GWT(Google Web Toolkit) 是 Google 最近推出的一个开发 Ajax 应用的框架,它支持用 Java 开发和调试 Ajax 应用,本文主要介绍如何利用 GWT 进行 Ajax 的开发。 Ajax技术是当前开发web应用的非常热门的技术,也是Web...
gwt 练习gwt 练习gwt 练习gwt 练习
gwt 入门,是你开始学习gwt的大道 安装之后的目录结构为: C:\程序开发\Java\gwt-windows-1.4.59 doc(文档目录,开发文档和Java API文档) samples(示例代码目录,非常有名的KitchenSink示例... gwt-servlet.jar
一个简单的GWT示例,是有关于股票显示的,其中有个小小的BUG(不影响对GWT的使用,故意的),希望大家可以把它找出来并做修改!
Gwt中文手册,GWt入门Gwt中文手册,GWt入门Gwt中文手册,GWt入门
EXT-GWT2.0.1+API+DOC gwtdesigner Gwt-Ext基础-中级-进阶 GWT快速开发 GWT入门 GWT中文教程
115提供的那个不知道怎么回事,我一直下不下来,后来在论坛找到了但是书签很乱,重新整理了一个书签,顺手删除了一个广告页。 1. 准备开发环境 2. Hello GWT 3. 模块 4. JSNI 5. 在GWT中使用XML 6. GWT控件详解 7. ...
gwt 开发
fwefwefwGWT工具GWT工具GWT工具GWT工具GWT工具GWT工具
gwt 获取前台所有input 标签,gwt 获取前台所有input 标签
GoogleWebToolkit(GWT)是一个Java软件开发框架用于开发类似于GoogleMaps和Gmail的AJAX应用程序。GWT的设计参考JavaAWT包设计,类命名规则、接口设计、事件监听等。在 GWT框架中使用 Echarts 。 注意: 本项目jar包已...
gwt 实现的一个简单计算器 在eclipse下调试通过 方便开始学习gwt的人学习
gwt使用手册,详细介绍gwt使用手册和各种组件的调用配置
使用GWT的一个简单的Login示例 GWT是 Google Web Toolkit的简称。
很好的资源,在这边分享下,提供给想要寻找GWT资源的兄弟姐妹们 如果哪位兄弟有好的资源不要忘了分享啊
gwt与hibernate整合,一个简单的新增和查询,只有一张表(两个字段),大家根据hibernate的配置文件自己生成吧