Get and Post Methods in Servlets

Introduction to the Get and Post Methods in Java

doGet() and the doPost() methods are the two most widely used standard Http methods as per the protocol. In the previous tutorial “understanding servlets” we have seen that doPost() is called when the post method is issued to the servlet from our Html form. By making the previous tutorial a base for this tutorial, we will elaborate more about the Get and Post methods in Java Enterprise.

Use of the Get and Post Methods

As we know that doGet()t is called when get method is issued to the servlet and that doPost() is called when post method is issued to the servlet, so the question is, when do we use the get and post methods? Get method is used when you are actually getting something from the application and post method is used when we want to submit some data to our Server and the application has to perform write operation depending upon the data you have posted, like if you have to create a new user in your application Or you want to initiate some action that causes a change in data like Update or Delete operations. So the understanding is that when you are using a post method it causes the application to make some changes to the data, this is how it is recommended how to use the get and post method.

For example, you have to create a new user in your application Or you want to initiate some action that causes a change in data like Update or Delete operations. So the understanding is that when you are using a post method it causes the application to make some changes to the data, this is how it is recommended how to use the get and post method.

Now the question is what if we use get method to write some data or use a post method to just retrieve data?

Understanding the Get and Post Methods

Let us explain by the example that we have used in the previous example.
Right click on “SimpleHTMLForm.html” -> RunAs -> RunOnServer

  1. Copy the link as shown in Figure
Simple HTML form web address in Java
Simple HTML form web address in Java
  1. Paste it in any browser as shown in Figure
Open HTML form in the browser
Open HTML form in the browser

Using the get method

  1. Open the file SimpleHTMLForm.html, in your project that we have previously created and change the method to “get” as shown in the Figure below now save and again Right click on “SimpleHTMLForm.html” -> RunAs -> RunOnServer, go to the browser and refresh the page, enter some text and click Submit query, it should call the get method.
Change method to get in simple HTML form
Change method to get in simple HTML form
  1. Notice the URL, the values that we have submitted has come here as a query parameter. As shown in Figure
Change in URL from get method
Change in URL from the get method
Query string in URL from Get method
Query string in URL from Get method

Now, what happens if we refresh in the browser? The servlet doGet() method is called on every refresh and each time it gets the value and returns it. So the assumption here is that we are using this get method to get some data from the database, which we have not done here.

Using the post method

  1. Open the file SimpleHTMLForm.html again and change the method to “post”, as shown in the Figure below. Now save and again Right click on “SimpleHTMLForm.html” -> RunAs -> RunOnServer, go to the browser close and reopen it and perform step 1 and 2 and press enter to load the pasted link, Now enter some text and click Submit query, it should call the post method now.
Change method to post in simple HTML form
Change method to post in simple HTML form
  1. Notice the URL, the values that we have submitted has NOT come here as query parameter

Now, what happens if we refresh in the browser? Before calling the post method, the browser will prompt a message as shown in figure

URL change and data change on post method
URL change and data change on post method

Difference between the get and post methods

In the above message, the browser is telling me that if I do a refresh, it’s going to call the same action again. So we can continue or we can cancel. If I continue, the doPost will be called. So why this message is not prompted in doGet()? It’s because of the implementation of the protocol. That’s how the protocol works. So it is one of the differences that these methods have. Another difference is in the URL query parameter, doGet() passes the values through the query parameter. While doPost() do not use the query parameter string as mentioned above points 4 and 6. So because of the doGet() method the URL becomes big, because the parameters are passing through URL string.

Download Complete Servlets Project Here

Summary:

In this tutorial, we have accomplished following things.

  • We got to know what the servlet class is.
  • What doGet and doPost methods are, and when to call which method.
  • We have also seen some basic differences between doGet and doPost.

In the next coming tutorials, we will learn about Scopes along with Request, Session, and Contexts. So stay tuned and stay blessed.