Gwt Rpc Using Http Request
Hi Every One ,
Today I will be discussing on how to make a call to server using different approach than Gwt Rpc and RequestBuilder.
HttpRequest is a class which allows you to make asynchronous HTTP requests to the originating server. But currently this class is Deprecated as this is replaced by Requestbuilder.
So HttpRequest has 4 methods to work with
2 types of asyncGet , first one which takes a url and ResponseTextHandler
Second one which takes a username,password ,url and a ResponseTextHandler
2 types of asyncPost , first one which takes a url ,data as a part of request and ResponseTextHandler
Second one which takes a username,password ,url ,data as a part of request and a ResponseTextHandler
now we will work on asyncPost ,
so now we are going to call a servlet on the server side which return the reverse of the String that is sent from client.The servlet is same as previous one.
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);
String s=new String(buffer.reverse());
resp.getWriter().print(s);
}
}
Now on the client side , in onModuleLoad()
We will create a HttpRequest object like
HttpRequest request=new HttpRequest();
From this request object we will be making a call like
httpRequest.asyncPost(url_host+"?string="+toServer, null, handler);
where first one is the url,null is the data to server [we can send our own data, and handler belongs to a class which implements the ResponseTextHandler interface .these class is used to receive HTTP completion events. This event is fired by HTTPRequest.
I have written a class which implements the ResponseTextHandler where this interface contains only method
public void onCompletion(String responseText) {
}
Which get executed on completation of call.
So the complete code in OnModuleLoad() is
private String StringToServer;
private HorizontalPanel hPanel;
private String url_host="";
private static final int STATUS_CODE_OK = 200;
HTTPRequest httpRequest;
public myResponse handler;
public void onModuleLoad() {
url_host=GWT.getModuleBaseURL()+"/server/ServletCallback";
httpRequest=new HTTPRequest();
hPanel=new HorizontalPanel();
hPanel.add(new TextBox());
handler=new myResponse();
hPanel.add(new Button("Get String Reverse",new ClickListener() {
public void onClick(Widget sender) {
try {
String toServer=((TextBox)hPanel.getWidget(hPanel.getWidgetCount()-2)).getText();
httpRequest.asyncPost(url_host+"?string="+toServer, null,handler );
}catch(Exception e) {
e.printStackTrace();
}
}//onClick
}));
RootPanel.get().add(hPanel);
}
Now lets see the class that implements the ResponseTextHandler
class myResponse implements ResponseTextHandler {
public void onCompletion(String responseText) {
((TextBox)hPanel.getWidget(hPanel.getWidgetCount()-2)).setText(responseText);
}
}
Here is the complete code
public class Application implements EntryPoint {
private String StringToServer;
private HorizontalPanel hPanel;
private String url_host="";
private static final int STATUS_CODE_OK = 200;
HTTPRequest httpRequest;
public myResponse handler;
//OnModuleLoad()
public void onModuleLoad() {
url_host=GWT.getModuleBaseURL()+"/server/ServletCallback";
httpRequest=new HTTPRequest();
hPanel=new HorizontalPanel();
hPanel.add(new TextBox());
handler=new myResponse();
hPanel.add(new Button("Get String Reverse",new ClickListener() {
public void onClick(Widget sender) {
try {
String toServer=((TextBox)hPanel.getWidget(hPanel.getWidgetCount()-2)).getText();
httpRequest.asyncPost(url_host+"?string="+toServer, null,handler );
}catch(Exception e) {
e.printStackTrace();
}
}//onClick
}));
RootPanel.get().add(hPanel);
}
class myResponse implements ResponseTextHandler {
public void onCompletion(String responseText) {
((TextBox)hPanel.getWidget(hPanel.getWidgetCount()-2)).setText(responseText);
}
}
}
Donot forget to keep the lines in your gwt.xml files
The path of servlet and the Http package.
<inherits name="com.google.gwt.http.HTTP"/>
<servlet path="/server/ServletCallback" class="com.example.server.MyServlet"/>
No comments :
Post a Comment