Cheap VPS & Xen Server

Residential Proxy Network - Hourly & Monthly Packages

Get Form Parameters


In this example, we will print form parameters like parameter name and parameter value. To do so, we are calling getElementById() method of Document class and getElementsByTag() method of Element class.

register.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset=“utf-8”>
  5. <title>Register Please</title>
  6. </head>
  7. <body>
  8. <form id=“registerform” action=“register.jsp” method=“post”>
  9. Name:<input type=“text” name=“name” value=“sonoo”/><br/>
  10. Password:<input type=“password” name=“password” value=“sj”/><br/>
  11. Email:<input type=“email” name=“email” value=“sonoojaiswal1987@gmail.com”/><br/>
  12. <input name=“submitbutton” type=“submit” value=“register”/>
  13. </form>
  14. </body>
  15. </html>

JsoupPrintFormParameters.java

  1. import java.io.File;
  2. import java.io.IOException;
  3. import org.jsoup.Jsoup;
  4. import org.jsoup.nodes.Document;
  5. import org.jsoup.nodes.Element;
  6. import org.jsoup.select.Elements;
  7. public class JsoupPrintFormParameters {
  8. public static void main(String[] args) throws IOException {
  9.     Document doc = Jsoup.parse(new File(“e:\\register.html”),”utf-8″);
  10.     Element loginform = doc.getElementById(“registerform”);
  11.     Elements inputElements = loginform.getElementsByTag(“input”);
  12.     for (Element inputElement : inputElements) {
  13.         String key = inputElement.attr(“name”);
  14.         String value = inputElement.attr(“value”);
  15.         System.out.println(“Param name: “+key+” \nParam value: “+value);
  16.     }
  17. }
  18. }

Output:

Param name: name 
Param value: sonoo
Param name: password 
Param value: sj
Param name: email 
Param value: sonoojaiswal1987@gmail.com
Param name: submitbutton 
Param value: register

Comments

comments