Reading Form Data from Servlets

Reading Form Data from Servlets
  • The browser uses two methods to pass this information to web server. 
  • These methods are GET Method and POST Method.
  • GET:
    • The GET method sends the encoded user information appended to the page request. 
    • The page and the encoded information are separated by the ? (question mark) symbol as follows −
    • Example: https://profcdpatel.blogspot.com/search?q=java
    • The GET method is the default method to pass information from browser to web server
    • Never use the GET method if you have a password or other sensitive information to pass to the server. 
    • The GET method has size limitation: only 1024 characters can be used in a request string.
    • Servlet handles this type of requests using doGet() method.
  • POST:
    • A generally more reliable method of passing information to a backend program is the POST method. 
    • This packages the information in exactly the same way as the GET method, but instead of sending it as a text string after a ? (question mark) in the URL it sends it as a separate message. 
    • This message comes to the backend program in the form of the standard input which you can parse and use for your processing. 
    • Servlet handles this type of requests using doPost() method.
Reading Form Data using Servlet
  • Servlets handles form data parsing automatically using the following methods depending on the situation.

  • getParameter():You call req.getParameter() method to get the value of a form parameter.
    • A method of HttpServletRequest.
    • Case-sensitive parameter name as an argument.
    • URL-decoding is done automatically.
    • An empty string is returned if the parameter exists but has no value.
      • null is returned if there was no such parameter.
    • Parameter names are case sensitive. 
    • Syntax: public String getParameter(String name)
    • Description: is used to obtain the value of a parameter by name.
    • Example:
  • getParameterValues(): Call this method if the parameter appears more than once and returns multiple values, for example checkbox.
    • If the same parameter name might appear in the form data more than once, you should call getParameterValues.
    • It returns an array of strings.
    • The return value of getParameterValues is null for nonexistent parameter names.
    • It is a one-element array when the parameter has only a single value.
    • Syntax: public String[] getParameterValues(String name)
    • Description: returns an array of String containing all values of given parameter name. It is mainly used to obtain values of a Multi select list box.
    • Example (in HTML):
    • Example (in JAVA):
  • getParameterNames() − Call this method if you want a complete list of all parameters in the current request.
    • Syntax: java.util.Enumeration getParameterNames()
    • Description : returns an enumeration of all of the request parameter names.
    • Example(in HTML)
    • Example (in JAVA)

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form