HTML Form Editor – Java

HTML Form Element

The HTMLFormElement represents a section containing input controls for submitting user information. Using our library, you can easily fulfill the HTML forms and send data directly from your Java application. To make this process is much more comfortable, we designed the com.aspose.html.forms Package. The main element of this Package is FormEditor. It allows you to find an existing form in the document, create a new from scratch, iterate through the elements, fill them up with data and surely send the information to the remote server. In this article, you will learn how to work with FormEditor object.

In this article, you will learn how to work with a FormEditor object to manipulate HTML form in the document, input controls, and submit information. For the testing purpose in this article, we use the form designed by https://httpbin.org service.

How to Use FormEditor in Java

The following code snippet demonstrates how to use the Aspose.HTML for Java library to fill and submit an HTML form programmatically:

  1. Create a new instance of the HTMLDocument class using HTMLDocument(url) constructor. Specify the URL of the HTML form to be filled out and submitted.
  2. Use the create(document, index) method to create an instance of the FormEditor class, passing in it the HTMLDocument instance and the index of the form being edited (value 0 refers to the first form in the HTML document).
  3. Fill the data up using classes and methods of the com.aspose.html.forms and save the filled HTML form to a file.
  4. Create an instance of the FormSubmitter class.
  5. Call the submit() method of the FormSubmitter class to submit the form data to the remote server.
  6. Check the status of the form submission result using the isSuccess() method.
 1// @java com/aspose/html/documentation/examples/Advanced_HTMLFormEditor.java
 2// Initialize an instance of HTML document from 'https://httpbin.org/forms/post' url
 3HTMLDocument document = new HTMLDocument("https://httpbin.org/forms/post");
 4
 5// Create an instance of the FormEditor
 6FormEditor editor = FormEditor.create(document, 0);
 7
 8// You can fill the data up using direct access to the input elements, like this:
 9InputElement custname = editor.addInput("custname");
10custname.setValue("John Doe");
11
12document.save("output/out.html");
13
14// or this:
15TextAreaElement comments = editor.getElement(TextAreaElement.class, "comments");
16comments.setValue("MORE CHEESE PLEASE!");
17
18// or even by performing a bulk operation, like this one:
19java.util.Map<String, String> dictionary = new java.util.HashMap<>();
20dictionary.put("custemail", "john.doe@gmail.com");
21dictionary.put("custtel", "+1202-555-0290");
22
23// Create an instance of FormSubmitter
24FormSubmitter submitter = new FormSubmitter(editor);
25
26// Submit the form data to the remote server
27// If you need you can specify user-credentials and timeout for the request, etc.
28SubmissionResult result = submitter.submit();
29
30// Check the status of the result object
31if (result.isSuccess()) {
32    // Check whether the result is in json format
33    if (result.getResponseMessage().getHeaders().getContentType().getMediaType().equals("application/json")) {
34        // Dump result data to the console
35        System.out.println(result.getContent().readAsString());
36    }
37    else {
38        // Load the result data as an HTML document
39        Document doc = result.loadDocument();
40        // Inspect HTML document here
41    }
42}

Aspose.HTML offers free online Converters for converting HTML, XHTML, MHTML, EPUB, XML and Markdown files to a variety of popular formats. You can easily convert HTML to PDF, HTML to JPG, SVG to PDF, MHTML to PDF, or MD to HTML. Just select a file, choose the format to convert, and you’re done. It’s fast and completely free!

Text “Banner Free Online Converters”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.