Retrieving values from HTML form input elements in JSP.

Byron Tymvios

To retrieve any value from an HTML Form element on a jsp page you need to use the implicit HttpServletRequest object's getParameter(String s) method. The HttpServletRequest object is available to all jsp pages and is named request. The String argument of the method getParameter(String s) is the elements name whose value you want to retrieve.

The result of this method will differ according to what type of element you are querying. For instance a checkbox element might return "on" if it has been selected, or it might return "null" if it hasn't been selected. Where a text element will return the text/value that was part of the element at the time the form was submitted. A radio button will return the value of the selected radio button in the button group. If you try and query an element that never existed in the form and therefore was never submitted to the JSP page in the HttpServletRequest object will return null.

Example HTML Form:
<form name="myForm" action="result.jsp" method="post">     <input type="checkbox" name="inputCheckbox">
    <input type="radio" name="inputRadio" value="0">
    <input type="radio" name="inputRadio" value="1">
    <input type="text" name="inputText">

    <input type="submit">
</form>

Example JSP page (This will only display the values recieved from the submitted form):
    <%= request.getParameter("inputCheckbox") %>
    <%= request.getParameter("inputRadio") %>
    <%= request.getParameter("inputText") %>

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact