Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Google Web Toolkit: AJAX Buzz Meets Real World Development : Page 4

With its Swing-like development framework and its awe-inspiring compiler/debugger, the Google Web Toolkit eases AJAX development.


advertisement

Spotlight on GWT RPC

Figure 3 shows how the AJAX With Ease application uses GWT RPCs.

Click to enlarge

Figure 3. 'AJAX With Ease' Application Uses GWT RPCs

Let's go over each of the RPCs:

  • Remote Service interface: HelloService.java in the client folder extends GWT's RemoteService and represents the interface GWT's RPC plumbing needs to enable.


  • Remote Service implementation class: HelloServiceImpl.java in the server folder implements the remote interface defined in HelloService and extends GWT's RemoteServiceServlet. RemoteServiceServlet in turn extends HttpServlet. In effect, HelloServiceImpl is a servlet responding to client requests (browser + HTML/JavaScript). The HelloServiceImpl is also referred to in the module configuration file hello.gwt.xml, wherein it is mapped to the '/hello' Servlet path.
  • Asynchronous callback interface: HelloServiceAsync in the client folder is an asynchronous callback interface. The name of the class must be the service interface class's name followed by 'Async'. You do not need to implement this interface, as it is implemented by GWT in the background.

    One of the peculiar aspects of GWT's RPC communication is that it is asynchronous, meaning when the client makes a call to the server it does not have to wait for the server to return the response immediately. The server later connects back to the client when it has data ready and supplies the client with the response data.

  • Serializable Type object: Person.java is a value object that is GWT serializable. It is received as a parameter by the hello() function. You do not need to do anything special to use the value objects that are passed back and forth between client and server. GWT handles serialization and de-serialization of the parameter and return values of the primitive and custom Java objects given. These custom objects implement GWT's IsSerialiazable interface, and you just follow some rules at Google's GWT documentation when defining these objects. The Person object is peculiar because it is used in JavaScript on the client side and in Java byte code on the server side.
  • HelloConstants: This class is not required by GWT, but it is a necessary evil if you intend to both debug your applications in GWT Hosted Mode within Eclipse and deploy your applications in Web servers such as Apache Tomcat. The class defines a constant HELLO_SERVICE_URL that points to the Servlet path. When debugging applications, you need to use (uncomment) the line that sets HELLO_SERVICE_URL to /hello as expected by the GWT Hosted environment. However, when you are ready to build a WAR file to deploy to a Web server, you will need to use (uncomment) the line that sets HELLO_SERVICE_URL at runtime to a value equal to http://localhost:8080/ajaxwithease-0.1/hello when deployed in a Web server.

    In addition to deploying your WAR file in a Web server, you will need to add Servlet mappings to web.xml as follows:

    ... <servlet> <servlet-name>hello</servlet-name> <servlet-class> com.techyatra.awe.server.HelloServiceImpl </servlet-class> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> ...

  • HelloRPC: A composite widget that consumes the HelloService service. It first retrieves a service endpoint for HelloService and then the endpoint entry point is set to the HelloServiceImpl Servlet path URL. An instance of AsyncCallback is created and passed as a parameter to the helloService.hello() function call. The onSuccess and onFailure of the AsyncCallback instance are overridden to perform AJAX With Ease application-specific processing:

    package com.techyatra.awe.client; ... public class HelloRPC extends Composite { ... HelloServiceAsync helloService = (HelloServiceAsync) GWT.create(HelloService.class); ServiceDefTarget endpoint = (ServiceDefTarget) helloService; public HelloRPC() { endpoint.setServiceEntryPoint (HelloConstants.HELLO_SERVICE_URL); ... AsyncCallback callback = new AsyncCallback() { public void onSuccess(Object result) { labelRPC.setText((String) result); } public void onFailure(Throwable caught) { errorMessage.setHTML(caught.getMessage()); } }; helloService.hello( new Person(textTitle.getText(), textName.getText()), callback); } ... } }

    When the server finishes processing and is ready to return data, it relies on AsyncCallback to callback (who else?) the client and invoke the onSuccess() method. Exceptions throw by the server are routed to the onFailure() method.

Your applications now have spiffy interfaces and fast data retrieval, but do they have any respect for history? With GWT, they will.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap