Class Form


  • public class Form
    extends HtmlElement
    Represents an HTML form. Each element in the form is represented by an HtmlElement object. The elements in the form are accessed by using the various "get" methods (e.g. getHtmlElement(String), getInputElement(String, String)). The HtmlElement objects can be modified to change the data that will be sent back to the server. More elements can be added to the form using addElement(HtmlElement), and removed using removeElement(String, String).

    Usually, a Form object is obtained by extracting it from a Response, using the Response.extractForm(int) method.

    e.g.

    
     Response response = request.send();
     Form form = response.extractForm(0);
     
    The form is then usually modified by changing the HtmlElement objects it contains, and sent back to the server on a later request. This could be either as query data on a GET request:
    
     // Create a Url with the form data as a query string
     Url url1 = new Url("http://localhost/");
     url1 = url1.withQuery(form);
    
     // Create a GET request based on the url and send it to the web server.
     Request request1 = getWebBrowser().createRequest(HttpMethod.GET, url1, 1);
    
     // Send the request to the web server
     Response response1 = request1.send();
     
    Or, as the message body on a POST request:
    
     // Create a Url
     Url url2 = new Url("http://localhost/");
    
     // Create a POST request and set the form data as the message body
     Request request2 = getWebBrowser().createRequest(HttpMethod.POST, url2, 2);
     request2.setMessageBody(form);
    
     // Send the the request to the web server
     Response response2 = request2.send();