GWT RPC
RPC (Remote Procedure Call) helps in client communication with server. RPC is a process of calling a method from a class however, in this process only difference is that the class is located on a server but not actually the part of client program.
There is a problem with RPC, as JavaScript runs in web browser and the RPC call from browser it hangs browser until the response is received.
To avoid the browser hanging, GWT RPC call is made “Asynchronous” and the browser does not hang while waiting for the response.
GWT RPC Mechanism
The implementation of GWT RPC is based on the Java Servlet technology. The server-side code that gets called from the client is usually referred to as a service and the remote procedure call is referred as calling a service.
The RPC implementation in a GWT application is given below.
Creating Service
Service is known as an interface at client side that defines all service methods. It is placed under client-package as service is available at client side.
1) Define Service Interface: We will be creating a client-side Java interface that extends RemoteService interface to develop a new service interface.
SampleService.java
2) Define Async Service Interface: The above service would produce error until unless it is defined inside Async interface with void as return type. This interface should have call back object for the Async service. The name of the Async interface should be same as service interface followed by Async.
SampleServiceAsync.java
3) Async Callback and Error Handling: This interface implements two methods OnSuccess and OnFailure. In this the class receives a callback from server and provides its functionality.
SampleCallback.java
Implementing Service
Services are implemented to perform some processing to respond to client requests. Service implementation is based on Servlet architecture.
1) Define Service Interface Implementation: GWT Service must extend RemoteServiceServlet class and should implement the associated service interface. It extends RemoteServiceServlet instead of HttpServlet. Serialization of the data is automatically handled by RemoteServiceServlet.
SampleServiceImpl.java
2) Update Entry of Service: In this we update the entry inside web.xml where we define the servlet and map the URL.
web.xml
Compilation file: Sample WebApplicaion.java
Output: