Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Snehal Masne
www.TechProceed.com
Contents
Traditional GWT
GWT RPC
www.TechProceed.com
builder.sendRequest() method receives a string data to be sent with the request and RequestCallback object that handles the response from the server null is passed if there is no data to be sent to the data
www.TechProceed.com
www.TechProceed.com
GWT Equivalent for Form Data Submission Constructing HTTP Request Manually
//build the data to post StringBuffer postBuilder = new StringBuffer(); postBuilder.append("username=" ); postBuilder.append( URL.encodeComponent( username ) ); postBuilder.append("&email=" ); postBuilder.append( URL.encodeComponent( email ) ); postBuilder.append("&password=" ); postBuilder.append( URL.encodeComponent( password ) ); try{ //create and submit the request RequestBuilder requestBuilder = new RequestBuilder( RequestBuilder.POST, GWT.getModuleBaseURL()+"/register.php" ); requestBuilder.sendRequest( postBuilder.toString(), new RequestCallback(){ public void onError(Request request, Throwable exception){ // handle error } public void onResponseReceived(Request request, Response response){ // handle response } }); }catch( Exception e){ // handle exception }
www.TechProceed.com
//set the main widget for the panel to a vertical panel VerticalPanel panel = new VerticalPanel(); form.setWidget(panel);
//create the username field TextBox tb = new TextBox(); tb.setName("username"); panel.add(tb); //create the e-mail field TextBox tb = new TextBox(); tb.setName("email "); panel.add(tb);
www.TechProceed.com
www.TechProceed.com
GWT RPC
GWT extends a browsers capability to asynchronously communicate with the server by providing a remote procedure call (RPC) library. Calls to the server are simplified by providing you with an interface of methods that can be called similarly to regular method calls. GWT marshal the calls (convert to a stream of data) and send to the remote server. At the server side, the data, is un-marshalled the method on the server is invoked
www.TechProceed.com
In GWT, the RPC library is divided into two packages: com.google.gwt.user.client.rpc package used for clientside RPC support com.google.gwt.user.server.rpc package used for serverside RPC support The client side provides interfaces that you can use to tag When the client code is compiled to Javascript using the GWT compiler, the code required to do the RPC marshaling will be generated
www.TechProceed.com
To understand how GWT RPC works, we would implement the data form submission using it
www.TechProceed.com
Create the RPC interface (stub) on the client side Create a new interface named LoginService under the my.utm.kase.gwttraining.client package Edit the code to look like the one below:
import com.google.gwt.user.client.rpc.RemoteService; import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
@RemoteServiceRelativePath("login") public interface LoginService extends RemoteService { boolean login(String username, String email, String password); }
www.TechProceed.com
Add the following in /war/WEB-INF/web.xml <servlet> <servlet-name>loginServlet</servlet-name> <servlet-class> my.utm.kase.gettraining.server.LoginServiceImpl </servlet-class> </servlet>
<servlet-mapping> <servlet-name>loginServlet</servlet-name> <url-pattern>/gwttraining/login</url-pattern> </servlet-mapping>
www.TechProceed.com
Create an async counterpart interface of the LoginService. This interface defines the callback method that will be called when the server generates a response Create another interface, LoginServiceAsync under my.utm.kase.gwttraining.client package Edit the code to look like the one below:
import com.google.gwt.user.client.rpc.AsyncCallback; public interface LoginServiceAsync { boolean login(String username, String email, String password, AsyncCallback<Boolean> callback); }
The async method always returns void and takes parameters similar to its stub counterpart plus an AsyncCallback parameter which specifies the return value of the stub method which is Void in this case
www.TechProceed.com
Implement the remote method on the server side Create a class, LoginServiceImpl under my.utm.kase.gwttraining.server package Edit the code to look like the one below:
import my.utm.kase.gwttraining.client.LoginService; import com.google.gwt.user.server.rpc.RemoteServiceServlet; public class LoginServiceImpl extends RemoteServiceServlet implements LoginService {
public boolean login(String username, String email, String password)
Call the remote method Create a login widget in my.utm.kase.gwttraining.client package to get username, email and password from user. It also captures login event and makes a RPC to loign user.
public class LoginWidget extends Composite { public LoginWidget() { //login panel FlexTable loginTable = new FlexTable(); final TextBox usernameBox = new TextBox(); final TextBox emailBox = new TextBox(); final PasswordTextBox passwordBox = new PasswordTextBox(); Button loginButton = new Button("Login"); loginButton.addClickHandler( new ClickHandler() { public void onClick(ClickEvent event) { login(usernameBox.getText(), emailBox.getText(), passwordBox.getText(); } }); //username widget loginTable.setWidget(0, 0, new Label("Username")); loginTable.setWidget(0, 1, usernameBox); //email widget loginTable.setWidget(1, 0, new Label("Email")); loginTable.setWidget(1, 1, emailBox);
www.TechProceed.com
www.TechProceed.com
Instantiate the widget in the GWTTrainingWidgets entry point class: //login widget LoginWidget loginWidget = new LoginWidget(); tabPanel.add(loginWidget, "Login"); Run or refresh to test
www.TechProceed.com
Thank you
www.TechProceed.com