简体   繁体   中英

How to get the value from the input tag in java?

<input type="text" name="n1" value="howru"/>

The given code above is the question and i want to know how we can extract value ie howru from value using java and the given code is not jsp i wanted solution in core java no jsp tags are used and this is normal html code

As the question isn't clear,but it seems like you want to extract value from the tag without using jsp and servlet, and let's just add a couple of more tags for better understanding. This can be done using JSOUP: https://jsoup.org/cookbook/

 String inputTag = "<input type=\"text\"   name=\"n1\" value=\"howru\"/>"
    + "<input type=\"text\"  id=\"textField2\" name=\"n2\" value=\"howru2\"/>"
    + "<input type=\"hidden\"  id=\"hiddenField\" name=\"n3\"value=\"howru3\"/>";

    Document document = Jsoup.parse(inputTag);

    Elements elementByTag = document.select("input[type=text]");
    System.out.println("Element By Tag(First Input Tag--text):" + elementByTag.get(0).attr("value"));
    System.out.println("Element By Tag(Second Input Tag--text):" + elementByTag.get(1).attr("value"));

    Element elementByID = document.getElementById("textField2");
    System.out.println("Element By ID(Second Input Tag--text):" + elementByID.attr("value"));

    elementByID = document.getElementById("hiddenField");
    System.out.println("Element By ID(Hidden Field):" + elementByID.attr("value"));

Output

Element By Tag(First Input Tag--text):howru
Element By Tag(Second Input Tag--text):howru2
Element By ID(Second Input Tag--text):howru2
Element By ID(Hidden Field):howru3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM