Another aspect of data interaction in JavaFX is the ability to post data to a web server. The HttpRequest object supports both GET and POST method. The code below shows how to capture data from a form and post it to web server over HTTP using JavaFX's HttpRequest.
The first portion of the code builds the GUI for the form:
The second part of the code defines function postDate() to collect the data from the form's TextBox instances and send it to server. Each data element to be sent to the server must be grouped in key/value pairs where key is the name of a field in an HTTP form. To accomplish the same in JavaFX, you place your data in a sequence of Pair instances. The Pair is a simple key/value structure (hence the name) where you can store associative data. In the code sample below, we declare variable postData as a sequence of Pair instances to store values with key firstName, lastName, title, and address.
Before data can be posted to the server though, the sequence of Pair instances must be converted into a format supported by web server. The utility class URLConverter makes available the encodeParameters(Pair):String function which returns a string of encoded values ready to be sent to the server.
The final portion of the code places the GUI controls on the stage:
When the code is executed, it will send the data in the TextBox components to the server addressed in the code as shown below: