Form Data in Servlets

As of now, we have discussed all the basics of servlet like interfaces in a servlet, the request, and the response object in the servlet. Let us move on to the next article which is Form Data in Servlets. As we know When we have sent information from the browser to the server. In order to send it, the browser uses two important methods which are GET and POST. In this article, we will discuss the same.

What is Form Data?

When we sent information from the browser to servlet the browser uses methods that are provided in the <form> tag element. It uses the following two methods.

  • GET
  • POST

What is GET in Servlet?

The GET method is the method used by the client to ask the server to send back a given resource.

  • The GET is a method that sends the information appended to the user request.
  • In the following example, we see the information like key1  & key2 is passed along with the request.
  • The data is appended to the URL as a series of name/value pairs.
  • The GET method is a default method.
  • The GET method is used to retrieve data from the browser. The servlet handles this type of request in the doGet() method.
http://www.abc.com/welcome?key1=value1&key2=value2

What is Post in Servlet?

The POST method is the method the browser uses to talk to the server when asking for a response.

  • The POST is a method that sends the information from browser to server and is more reliable as compared to GET.
  •  In the following example, the information passed is the same as that of getting the method only difference here is that instead of sending string after? it sends as a separate message.
  • The servlet handles this type of request in the doPOST() method.
  • When we submit the form with the POST method, we get no data appended to the URL.
http://wwww.abc.com/

Methods to Read Form data using Servlet

  • getParameter(): This method returns the value of the form.
  • getParameterValues():This method returns multiple values.
  • getParameterNames():This method return complete list of parameter name in the request.

Let’s see the example of the GET method

In this example, we will use the GET method. To fetch the value we will use the getParameter() method.

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
  <form action="GetDemo" method="get">
    Enter Name<input type="text" name="name"> <input type="submit"
      value="GET" id="button-1" />
  </form>
</body>
</html>

Example.java

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Example
 */
@WebServlet("/Example")
public class Example extends HttpServlet {
  private static final long serialVersionUID = 1L;

  public Example() {
    super();
    // TODO Auto-generated constructor stub
  }
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    // TODO Auto-generated method stub
    PrintWriter out = response.getWriter();
    String name = request.getParameter("name");
    out.println("BY GET Method");
    out.println("Welcome " + name);
  }
}

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"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  id="WebApp_ID" version="3.0">
  <display-name>GETExample</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>GetDemo</servlet-name>
    <servlet-class>Example</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>GetDemo</servlet-name>
    <url-pattern>/GetDemo</url-pattern>
  </servlet-mapping>
</web-app>

Output

Let’s see the example of the POST method

In this example, we will use the POST method. To fetch the value we will use the getParameter() method.

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
  <form action="PostDemo" method="post">
    Name<input type="text" name="name"> <br> RollNo <input
      type="number" name="roll"> <br> <input type="submit"
      value="POST" id="button-1" />
  </form>
</body>
</html>

Example.java

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Example
 */
@WebServlet("/Example")
public class Example extends HttpServlet {
  private static final long serialVersionUID = 1L;

  public Example() {
    super();
    // TODO Auto-generated constructor stub
  }

  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    // TODO Auto-generated method stub
    PrintWriter out = response.getWriter();
    String name = request.getParameter("name");
    int roll = Integer.parseInt(request.getParameter("roll"));
    out.println("BY POST Method");
    out.println("Name: " + name);
    out.println("Roll No: " + roll);
  }

}

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"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  id="WebApp_ID" version="3.0">
  <display-name>Postmethoddemo</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>PostDemo</servlet-name>
    <servlet-class>Example</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>PostDemo</servlet-name>
    <url-pattern>/PostDemo</url-pattern>
  </servlet-mapping>
</web-app>

Output

Difference between GET & POST method

 

GET

POST

Only a limited amount of data can be sent. A large amount of data can be sent.
It is not secured. It is secured.
It can be bookmarked It cannot be bookmarked.
It is more efficient than POST. It is less efficient than GET.
It is less reliable than POST. It is more reliable than GET.