Sei sulla pagina 1di 11

Remote Procedure Calls (RPC)

What to understand:

==> Client vs. Server


==> The Client Files ==> The Server Files ==> How they talk

Client vs. Server


Client
<=======>

Server

(Browser)

(Tomcat)

RPC is a method for allowing the client to talk to server

The Client Files


1. Create the interface to define the service StringReverserService
public interface StringReverserService extends RemoteService{ public String reverseString(String stringToReverse); }

The Client Files


2. Create the asynchronous interface for the StringReverserService StringReverserServiceAsync
public interface StringReverserServiceAsync { void reverseString(String stringToReverse, AsyncCallback async); }

The Client Files


3. Instantiate and call the Service on an event EntryPoint class: StringReverser
declare StringReverserServiceAsync
instantiate Async object set the entry point back to server define the AsyncCallBack onSuccess and onFailure

StringReverser Code
1. Declare the Async object
private StringReverserServiceAsync reverserService = null;

2. Instantiate the Async object


if (reverserService == null) { reverserService = (StringReverserServiceAsync) GWT.create(StringReverserService.class);

3. Set the Async Entry Point to the server class via URL
//this just gets the Url (eg. http://ahs510.org) String module = GWT.getHostPageBaseURL(); int indexMod = module.indexOf(GWT.getModuleName()); String host = module.substring(0, indexMod); //this sets the entry point for the service back to server ((ServiceDefTarget)reverserService).setServiceEntryPoint( host + "/servlet/StringReverserService");

StringReverser Code
4. Call reverseString and define the AsynCallBack onSuccess and OnFailure
reverserService.reverseString(stringToReverse, new AsyncCallback() { public void onFailure(Throwable caught) { // Reset the button text and remove the loading animation resetWiget(); Window.alert("Fail to get response from server: "); } public void onSuccess(Object result) { // Reset the button text and remove the loading animation resetWiget(); // Get the service call response and display it String reverse = (String) result; ReversedStringDisplayDialog dlg = new ReversedStringDisplayDialog(stringToReverse, reverse); dlg.show(); } });

The Server File


1. Implement the reverseString method
public class StringReverserServiceImpl extends RemoteServiceServlet implements StringReverserService {

public String reverseString(String stringToReverse) { StringBuffer reverse = new StringBuffer(stringToReverse.length()); for (int i = (stringToReverse.length() - 1); i >= 0;i--) reverse.append(stringToReverse.charAt(i)); return reverse.toString(); } }

The Servlet mapping


1. path attribute in <servlet> tag must match setServiceEntryPoint

==> Remember in StringReverser:


((ServiceDefTarget) reverserService).setServiceEntryPoint( host + "/servlet/StringReverserService");

==>Configure server to make path available: StringReverser.gwt.xml


<servlet path="/servlet/StringReverserService" class="server.StringReverserServiceImpl" />

In review
1. Configure Client side files:
a. StringReverserService
b. StringReverserServiceAsyc c. EntryPoint class: StringReverser

2. Configure Server File:


a. StringReverserServiceImpl

3. Map servlet in xml file

Lets give it a shot


Together: IsPalindrome

Instructions: A simple text box and button to check is entry a palindrome. Display results to user

Potrebbero piacerti anche