Pages

Thursday, April 2, 2009

Gwt Rpc using Request Builder

Share it Please


Gwt Rpc using Request Builder

Hi everyone . Today we will be working on implementing a different approach of making a call to server [java servlet] using Request Builder. Request Builder is a class available in Http package of gwt. This is used to create request objects. Currently Request Builder is capable of making only Get and Post calls only .according to the Gwt Api ,this limitation is due to the bug in safari web browser.

So lets start creating the files , this requires only 2 files . we need a class on client side containing onModuleLoad() and a servlet on the servder side.

Lets create a sample servlet on the server side .
//Sample Servlet
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/xml");
String StringToServer=req.getParameter("string");
StringBuffer buffer=new StringBuffer(StringToServer);
resp.getWriter().print(buffer.reverse().toString());
}
}
The simple servlet which returns the reverse of the String sent from client.

Now on the client side in onModuleLoad() , we can use the RequestBuilder as follows,
First we need to create a RequestBuilder object .i have used RequestBuilder which takes 2 arguments .Request Builder method and url to connect .

Here the RequestBuilder
builder=new RequestBuilder(RequestBuilder.POST, url_host+"?string="+toServer);

after creating the request builder , we need to send the request
builder.sendRequest(null, requestCallback);
in the place of null we can send data as a part of request. requestCallback belongs to RequestCallback interface which is nothing but a callback interface which handles the response .it has 2 methods .
public void onError(Request request, Throwable exception) { }
public void onResponseReceived(Request request,Response response){}
the appropriate method is executed according to the response.

Here is the complete code in onModuleLoad().
private String StringToServer;
private HorizontalPanel hPanel;
private String url_host="";
private RequestCallback requestCallback;
private RequestBuilder builder;
private static final int STATUS_CODE_OK = 200;
public void onModuleLoad() {
url_host=GWT.getModuleBaseURL()+"/server/ServletCallback";
requestCallback=new RequestCallback() {
public void onError(Request request, Throwable exception) {
exception.printStackTrace();
}
public void onResponseReceived(Request request,Response response) {
if(response.getStatusCode()==STATUS_CODE_OK) {
Window.alert("Successfull ");
}
}
};
hPanel=new HorizontalPanel();
hPanel.add(new TextBox());
hPanel.add(new Button("Get String Reverse",new ClickListener() {
public void onClick(Widget sender) {
try {
String toServer=((TextBox)hPanel.getWidget(hPanel.getWidgetCount()-2)).getText();
builder=new RequestBuilder(RequestBuilder.POST, url_host+"?string="+toServer);
builder.sendRequest(null, requestCallback);
}catch(Exception e) {
e.printStackTrace();
}
}
}));

RootPanel.get().add(hPanel);
}//OnModuleLoad () Close
}








Donot forget to add the path of the servlet in gwt.xml file like
<servlet path="/server/ServletCallback" class="com.example.server.MyServlet"/>



in order to work with Request Builder classes , we need to inherit the Http package.



so place this in gwt.xml file : <inherits name="com.google.gwt.http.HTTP"/>

Thank u,
jagadesh














1 comment :

  1. I to consume REST service(xml/json) and i require post and delete request too, can i use the same approach.

    Thanks
    Santhosh

    ReplyDelete