Sunday,Apr18,

How to Call a Servlet in GWT

Sometimes you may need a HttpServlet other than GWT Rpc Service in a real life application. If needed then how to call the Servlet from your GWT client? Here you will find the answer.

Here is a simple HelloServlet

public class HelloServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{ 
String name = request.getParameter("name");
response.getWriter().write("Hello "+name+", Welcome in My Servlet");
}
}
The Servlet uses a request a parameter 'name' and writes it in the response writer.

To call the Servlet from the client first configure the web.xml (war/WEB-INF/web.xml) file to let your GWT client know about the HelloServlet.

 HelloServlet
 org.ratul.servlettest.server.HelloServlet


 HelloServlet
 /hello

Value of the url-pattern node will be used later to call the Servlet.

Now make a simple UI with a TextBox and a Button.
final Button sendButton = new Button("Send");
final TextBox nameField = new TextBox();
RootPanel.get().add(nameField);
RootPanel.get().add(sendButton);

In the Click Handler of the sendButton call the Servlet with the 'name' parameter in the following way
sendButton.addClickHandler(new ClickHandler() {
  public void onClick(ClickEvent event) {
    Window.Location.replace("/hello?name="+nameField.getText());
    //the url-pattern value is used here
  }
});

Thats' all for today. Happy coding with GWT :-)

12 comments:

Mark October 10, 2010 at 11:55 PM  

Good one. This helps.

Bastien August 10, 2011 at 3:37 AM  

Hi. Thanks for the Help !

Do you know a way to open the link in a new tab ?

Unknown August 10, 2011 at 8:00 AM  

@Bastien: use target property of the a tag.
<a href="http://zawoad.blogspot.com" target = "_blank">Link</a>

Radu August 13, 2011 at 1:18 PM  

Hi. I gave it a try but I keep getting this error and I checked it over and over didn't find where the problem might be.

link: http://localhost:8084/hello?name=John

HTTP Status 404 -
Type: Status Report
Message:
Description: The requested resource () is not available.


Many thanks and best regards

Unknown August 13, 2011 at 7:12 PM  

@radu: I think the problem lies in the url. Before /hello there should be the project name.

Radu August 15, 2011 at 12:42 PM  

Many thanks it worked! Although I did try that before but maybe it didn't work because I didn't restart browser before retries.

MJ August 28, 2011 at 2:26 AM  

This is good, but I have a problem. Above mentioned callign works flawlessly in all browsers BUT IE. In IE, I get exception. The servlet request string is messed up. If I put a "/hello", the message is: unknown url: http://localhost:8080/hello?..

But if I call it with "hello" ony, the IE changes that to:
http://localhost:8080/Application/si.myPackage.servlets.HelloServlet/hello?...

I just can't figure out why IE is doing this. Like mentioned, in other browsers it is fine.

Bah :)

L.F. November 28, 2012 at 6:06 PM  

Thanks!
This was very helpful in adding DataSourceServlet to a gwt project.
Larisa

Sascha December 18, 2012 at 9:06 AM  

Hey, thank you so much, this one made it quite easy :)

Glyndwr Bartlett January 14, 2013 at 6:35 PM  

This is very helpful. I am getting the following error:
HTTP ERROR: 503
SERVICE_UNAVAILABLE
RequestURI=/AwardTracker.html

I am very new to Java and GWT.

Thanks,

Glyn

Unknown March 17, 2013 at 2:58 PM  

Hmm... I've got several years experience with GWT and Java, thaought this would be easy - but I can't get this work. I've tried every variant I can think of in the url-pattern: with and without project name, etc -- but still I get a 404 no matter what I do. Working under tomcat 7. Any hints?

Unknown March 17, 2013 at 3:02 PM  
This comment has been removed by the author.

Total Pageviews

447,005

Tags

Abstract Entities ActiveMessaging Amazon SQS Asynchronous mail sending Context Menu Contiuous Integration Deploy Rails Application Deployment by Capistrano Design pattern Ext GWT Ext GWT DnD Framework Filter Grid GWT GXT Chart GXT Editable Grid GXT Example GXT Grid Grouping GXT Tree Filter GlassFish Google App Engine Grid GroupLayout JDO JPA JPQL JTable JavaMail API ListView Local Paging Mapped Superclass Open flash chart Reflection Remote pagination Session Management Strategy pattern XTemplate cruisecontrol.rb filter JTable Row flash chart gxt java reCAPTCHA
Twitter Updates
    follow me on Twitter

    Followers