POSTing data via GWT
Posted by Manolis Platakis on 10-08-2010 at 0:52
This is my second GWT tip blog post. You can read the first one here, if you are interested in finding out how to call an iFrame's javascript function via GWT. In this post we are going to deal with sending POST data via GWT code. Recently, I had to send some POST data from a custom widget that I had developed to a totally different (non GWT) web page. To do so, I employed a GWT FormPanel.
FormPanel form = new FormPanel("_blank");
//form = new FormPanel("SomeNamedFrame");
form.setAction(Constants.FORMURL);
form.setMethod(FormPanel.METHOD_POST);
form.setEncoding(FormPanel.ENCODING_URLENCODED);
The above method calls are pretty self explanatory. The constructor argument is the "target" attribute in the POST HTML code. The setAction method sets the "action" attribute i.e. the web page that will receive and process the sent data.

I didn't want any actual form to show up but I was obliged to use one of the widgets that are associated with the FormPanel. So this is what I did.
TextArea tarea = new TextArea();
tarea.setName("tarea");
tarea.setVisible(false);
form.add(tarea);
form.setVisible(false);
add(form);
As you can see both the textarea and the form as a whole are not visible to the user. The final step was to send my data.
// listener to invoke the form submission
tarea.setText(dataToSendAsString());
form.submit();
All you have to do in the ACTION page is to read the POST parameter "tarea" (or whatever the textarea's setName argument was) and handle the received data. You can easily use a delimiter to split your received string.

I hope the above mini tutorial was helpful. Let me know what you think in the comments.
Posted by Matina on 11-08-2010 at 16:02
As I've already told you, this way (with the help of GWT :) ) is much better, cleaner and faster than trying modifying the classing method of passing variables via ajax's xmlhttprequest or remembering the hidden fields in html forms. In general, GWT rocks :P

A n00bie

//p.s. Δεν έχει φάση να μιλάμε στα αγγλικά? :P
Comment on This Post

Image password:
The Captcha image
Phonetic spelling (mp3)