POSTing data via GWT
Posted 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.
Comment on This Post

Warning: Theme_RCBlog::require(CaptchasDotNet.php) [function.Theme-RCBlog-require]: failed to open stream: Too many open files in ./themes/rcblog.php on line 315

Fatal error: Theme_RCBlog::require() [function.require]: Failed opening required 'CaptchasDotNet.php' (include_path='.:/opt/php/lib/php') in ./themes/rcblog.php on line 315