Share on Google+Share on Google+

Java Servlet : Reading Form Data Example

In this tutorial, we are discussing about reading form data of a servlet.

Java Servlet : Reading Form Data Example

In this tutorial, we are discussing about reading form data of a servlet.

Reading Form Data :

There are three methods of servlet to handle the form data. These are listed below -

  • getParameter(String name) - It returns the value of a request parameter as a String. If there is no parameter, return null.
    This method is used when parameter has single value.
  • getParameterNames() - it returns an enumeration of String objects containing complete list of all parameters
    in the given request.
  • getParameterValues() - It returns array of String objects containing all the values of the requested parameter.
    If there is no parameter, return null.

Example : Here we are taking example of getParameter(). form.jsp is our jsp page in which we are designing form having two fields name and location. In servlet we are getting name and location by using getParameter() method.

form.jsp -

<form method="get" action="getParameterExample">
<table align="center">
<td>Name : </td>
<td><input type="text" name="name"/></td>
<td>Location : </td>
<td><input type="text" name="location"/></td>

<td><input type="submit" name="Submit" value="Submit"/></td>
</html> -

package net.roseindia;

import javax.servlet.*;
import javax.servlet.http.*;

public class GetParameterExample extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
String name = request.getParameter("name");
String location = request.getParameter("location");

out.println("<b><font color='purple'>Your Name :</font>" + name + "</b></br>");
out.println("<b><font color='purple'>Your Location :</font>" + location+"</b>");


web.xml -

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">

<!--GetParameter servlet mapping-->



Output :

When you click on submit you will get following result page.


Posted on: October 4, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null