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 sent it, 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 is a method that sends the information appended to the user request. Following is the GET method. In this, we see the information like key1  & key2 is passed along with the request. 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 is a method that sends the information from browser to server and is more reliable as compared to GET. Following is the POST request. In this, the information passed is the same as that of getting 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.

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

Example.java

web.xml

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

Example.java

web.xml

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.