Java Servlet : Reading Form Data Example


 

Java Servlet : Reading Form Data Example

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

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 GetParameterExample.java we are getting name and location by using getParameter() method.

form.jsp -

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

<td><input type="submit" name="Submit" value="Submit"/></td>
</tr>
</table>
</form>
</body>
</html>

GetParameterExample.java -

package net.roseindia;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class GetParameterExample extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
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="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ServletExample</display-name>

<!--GetParameter servlet mapping-->
<servlet>
<servlet-name>GetParameterExample</servlet-name>
<servlet-class>net.roseindia.GetParameterExample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GetParameterExample</servlet-name>
<url-pattern>/getParameterExample</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>form.jsp</welcome-file>

</welcome-file-list>
</web-app>

Output :


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

Ads