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.
Value of the url-pattern node will be used later to call the Servlet.HelloServlet org.ratul.servlettest.server.HelloServlet HelloServlet /hello
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:
Good one. This helps.
Hi. Thanks for the Help !
Do you know a way to open the link in a new tab ?
@Bastien: use target property of the a tag.
<a href="http://zawoad.blogspot.com" target = "_blank">Link</a>
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
@radu: I think the problem lies in the url. Before /hello there should be the project name.
Many thanks it worked! Although I did try that before but maybe it didn't work because I didn't restart browser before retries.
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 :)
Thanks!
This was very helpful in adding DataSourceServlet to a gwt project.
Larisa
Hey, thank you so much, this one made it quite easy :)
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
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?
Post a Comment