Gwt RPC

2,175 views
2,013 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,175
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
23
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Gwt RPC

  1. 1. GWT - RPC
  2. 2. The Objects com.google.gwt.user.client.rpc.RemoteService com.google.gwt.user.client.rpc.AsyncCallback com.google.gwt.user.client.rpc.ServiceDefTarget com.google.gwt.user.server.rpc.RemoteServiceServlet
  3. 3. RPC plumbing diagram
  4. 4. RPC plumbing diagram package com.mycompany.client; import com.google.gwt.user.client.rpc.RemoteService; public interface MyAddService extends RemoteService { public int add(int a, int b); }
  5. 5. RPC plumbing diagram package com.mycompany.server; import com.google.gwt.user.server.rpc.RemoteServiceServlet; import com.mycompany.client.MyAddService; public class MyAddServiceImpl extends RemoteServiceServlet implements MyAddService{ public int add(int a, int b) { int z = a + b; return z; } }
  6. 6. RPC plumbing diagram package com.mycompany.client; import com.google.gwt.user.client.rpc.AsyncCallback; public interface MyAddServiceAsync { public void add(int a, int b, AsyncCallback callback); }
  7. 7. Deploy service <ul><li>Tomcat -> add Servlet </li></ul><ul><li>Hosted mode -> modify module XML </li></ul><module> <inherits name='com.google.gwt.user.User'/> <entry-point class='com.mycompany.client.MyApplication'/> <servlet path=&quot;/addService&quot; class=&quot;com.mycompany.server.MyAddServiceImpl“/> </module>
  8. 8. Actually making a call button.addClickListener( new ClickListener() { public void onClick(Widget sender) { AddServiceAsync addService = (AddServiceAsync) GWT.create(AddService.class); ServiceDefTarget endpoint = (ServiceDefTarget) addService; String moduleRelativeURL = GWT.getModuleBaseURL() + &quot;myAddService“; endpoint.setServiceEntryPoint(moduleRelativeURL); instantiate service interface set service implementation url
  9. 9. Actually making a call button.addClickListener( new ClickListener() { public void onClick(Widget sender) { AddServiceAsync addService = (AddServiceAsync) GWT.create(AddService.class); ServiceDefTarget endpoint = (ServiceDefTarget) addService; String moduleRelativeURL = GWT.getModuleBaseURL() + &quot;myAddService“; endpoint.setServiceEntryPoint(moduleRelativeURL); AsyncCallback callback = new AsyncCallback(){ public void onSuccess(Object result){ resultaatLabel.setText(result.toString()); } public void onFailure(Throwable caught) { resultaatLabel.setText(caught.getMessage()); } create an asynchronous callback to handle the result
  10. 10. Actually making a call button.addClickListener( new ClickListener() { public void onClick(Widget sender) { AddServiceAsync addService = (AddServiceAsync) GWT.create(AddService.class); ServiceDefTarget endpoint = (ServiceDefTarget) addService; String moduleRelativeURL = GWT.getModuleBaseURL() + &quot;myAddService“; endpoint.setServiceEntryPoint(moduleRelativeURL); AsyncCallback callback = new AsyncCallback(){ public void onSuccess(Object result){ resultaatLabel.setText(result.toString()); } public void onFailure(Throwable caught) { resultaatLabel.setText(caught.getMessage()); } }; addService.add(Integer.parseInt(aBox.getText()), Integer.parseInt(bBox.getText()), callback); } } ); actually make the call
  11. 11. Actually making a call button.addClickListener( new ClickListener() { public void onClick(Widget sender) { AddServiceAsync addService = (AddServiceAsync) GWT.create(AddService.class); ServiceDefTarget endpoint = (ServiceDefTarget) addService; String moduleRelativeURL = GWT.getModuleBaseURL() + &quot;myAddService“; endpoint.setServiceEntryPoint(moduleRelativeURL); AsyncCallback callback = new AsyncCallback(){ public void onSuccess(Object result){ resultaatLabel.setText(result.toString()); } public void onFailure(Throwable caught) { resultaatLabel.setText(caught.getMessage()); } }; addService.add(Integer.parseInt(aBox.getText()), Integer.parseInt(bBox.getText()), callback); } } );
  12. 12. code executed on client side code executed on server side
  13. 13. Service Implementation Path <ul><li>This can be done in two ways – </li></ul><ul><li>1. Using RemoteServiceRelativePath annotation </li></ul><ul><ul><li>@RemoteServiceRelativePath(&quot;greet&quot;) </li></ul></ul><ul><ul><li>public interface GreetingService extends RemoteService { </li></ul></ul><ul><ul><li>String greetServer(String name) throws IllegalArgumentException; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>2. Using ServiceDefTarget </li></ul><ul><ul><li>ServiceDefTarget endPoint = (ServiceDefTarget) greetingService; </li></ul></ul><ul><ul><li>endPoint.setServiceEntryPoint(GWT.getModuleBaseURL() + “greet” ); </li></ul></ul><ul><ul><li>Reference: Common Pitfalls section of h ttp://code.google.com/webtoolkit/doc/1.6/DevGuideServerCommunication.html </li></ul></ul>
  14. 14. Servlet Mapping <ul><li>GWT modules may declare one or more <servlet> tags. These define Java Servlets that implement the server-side component of a GWT-enabled web application. </li></ul><ul><li>Prior to GWT 1.6, these GWT module servlet tags controlled the set of servlets were actually instantiated during hosted mode. But as of GWT 1.6, this is no longer true. Instead, the web application's WEB-INF/web.xml configuration file controls what servlets are instantiated. A GWT module specifies only what servlets are expected . </li></ul><ul><li>During hosted mode startup, the set of expected servlets (from GWT module <servlet> tags) is validated against the set of actual servlets (from the WEB-INF/web.xml) and a warning is issued for each expected servlet which does not match an actual servlet. </li></ul><ul><li>Reference: gwt-2.0.3/doc/helpInfo/servletMappings.html </li></ul>
  15. 15. More info <ul><li>http://code.google.com/webtoolkit/doc/latest/DevGuideServerCommunication.html </li></ul>
  16. 16. <ul><li>… and explore more by practice </li></ul><ul><li>Thanking You </li></ul><ul><li>Mahesh Babu M </li></ul>

×