This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes GWT and the fly likes GWT and HTTP post requests Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » GWT
Bookmark "GWT and HTTP post requests" Watch "GWT and HTTP post requests" New topic
Author

GWT and HTTP post requests

Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3476

Hi.
I have been reading that GWT plays fine with HTML forms post requests.
Usually, we sent the post request to some kind of command, we run a little of Voodoo at the command and then forward the request to another resource (JSP for example).
How GWT fits in these cases?
What I don't understand is if I'm posting from GWT page, what to do in the command once handling the request is finished? forwards to another resource?
Would you mind shedding some lights here?
Thanks for your time.
Adam Tacy
author
Ranch Hand

Joined: Jun 02, 2007
Posts: 37
Hi John,

In the background, posting a form in GWT works in the same way as normal. You hit the submit button, the data in the form is submitted to some resource on your server (JSP page, PHP, .Net Perl etc etc) that does some action on the data and then returns a result to the web browser.

In the GWT view, you create your form in a FormPanel adding widgets (e.g. text boxes, radio buttons etc) to the panel, and then add a FormHandler. When you submit the form, the onSubmit() method of the handler is executed - here you can check the form is filled in correct etc. Once the submission is complete, then the onSubmitComplete() method of the handler is called, so you can handle any submission complete (e.g. result) from the server.

In execution, by default GWT creates a hidden frame on your page where the actual submission of the form and return of result occurs. GWT handles the moving of data around, e.g. the result returned to this hidden frame is the value that is passed to the form handler's onSubmitComplete() method.

You can see some example code here: http://code.google.com/webtoolkit/documentation/com.google.gwt.user.client.ui.FormPanel.html

So, it's all done with smoke and mirrors

Hope that answers the question.

//Adam
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3476

So, a typical flow is:
1. GWT page posts to /action.jspa command object.
2. action.jspa command do the Voodoo on behalf of us.
3. action.jspa command forwards the request to nicePage.jsp
4. The user is now see nicePage.jsp
Right?
Or the generated markup of nicePage.jsp will be shown on the GWT page (in the same way we use Prototype Ajax.Updater)?
Adam Tacy
author
Ranch Hand

Joined: Jun 02, 2007
Posts: 37
almost, apart from step 4 generally gets posted to a hidden frame in your GWT application.

In the onSubmitComplete() method of the FormHandler you can programmatically see the contents of that hidden frame and act appropriately - which might include parsing the result, or placing the contents directly into the DOM.

(If you provde the FormPanel a your own named frame, then the standard functionality of using hidden frames is replaced with all the action taking place in the visible frame)

//Adam
Robert Hanson
author
Ranch Hand

Joined: May 11, 2007
Posts: 34
With FormPanel you have the option of not using the hidden frame. You could have the results go to a named frame, like "_self", which would replace the contents of the page.


Robert Hanson
Co-Author GWT in Action
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3476

almost, apart from step 4 generally gets posted to a hidden frame in your GWT application.

In the onSubmitComplete() method of the FormHandler you can programmatically see the contents of that hidden frame and act appropriately - which might include parsing the result, or placing the contents directly into the DOM.

So, I have to create a hidden frame and assign an ID to it in my GWT host page?
In case GWT will create it on the fly, how I'm supposed to access it with DOM since I don't its ID or name?
Thanks in advance.
Adam Tacy
author
Ranch Hand

Joined: Jun 02, 2007
Posts: 37
Originally posted by John Todd:

So, I have to create a hidden frame and assign an ID to it in my GWT host page?


You don't have to worry about it; GWT will create the frame for you if you just use the constructor FormPanel().

However, GWT gives you the option of creating a frame yourself if you want, and then you can pass that (or a reference to _self/ _top etc) to the constructor of the FormPanel - this can actually be useful for debugging if you want to see the actual raw data being returned by your service.

Originally posted by John Todd:

In case GWT will create it on the fly, how I'm supposed to access it with DOM since I don't its ID or name?


Again, you don't have to worry. In the the actual FormHandler onSubmitComplete() method you get access to the results through a FormSubmitCompleteEvent object that is passed in as a parameter - I was being a bit lazy with the method definition, in reality you would have:

form.addFormHandler(new FormHandler() {
public void onSubmitComplete(FormSubmitCompleteEvent event) {
String result = event.getResults());
}

public void onSubmit(FormSubmitEvent event) {
}
});

The onSubmitComplete() method is called by GWT code when the form submission is completed and GWT passes in a FormSubmitCompleteEvent object from which you can access the contents of the hidden frame - through that event's getResults() method.

//Adam
Mahantesh Hongal
Greenhorn

Joined: Apr 06, 2010
Posts: 1
getResults() method is returning the proper value in FireFox but returning 'null' in Internet Explorer
any idea, what could be the reason? why get Results() method always returns null in IE
 
Consider Paul's rocket mass heater.
 
subject: GWT and HTTP post requests
 
Similar Threads
Apache/IHS MaxServers
doGet and doPost ?
PUT method doubt??
basic question based on request & response ...
Get vs Post