Understanding Servlets


After completing our web application tutorial series till now, you would be able to create a Web application project, create a simple HTML file in this project, create a new servlet class in it, implement a doGet Method in the servlet class and run and test it. In the previous tutorial named  “Creating Servlet program” we have discussed the introduction of servlet plus the doGet() method and use of response object in it. Now we will see in detail about servlet functionality and different functions like doPost() etc. Before starting this tutorial, we assume that you have all the basic knowledge of Java programming language and OOP concepts, and also you have an understanding of HTML and XML. In this tutorial, we will try understanding servlets and their working.

Understanding Servlets

Servlet Class in Java

Let us start from the servlet class

In the above source, you can notice that the parent class for any servlet is the HttpServlet, we can access this HttpServlet by importing it from javax.servlet.http.HttpServlet package.

The list of functions that it implements are as follows.

Destroy Method

doHead Method


Overridden Methods doOptions, doTrace, doDelete, doPut along with doGet and doPost

In the above source notice the commonalities, that is  HttpServletRequest arg0, HttpServletResponse arg1 are coming from the parameters we have used these imports in our servlet.


Import Java Servlet Request and Response from javax.servlet.http


Working of HTTPServletRequest and HTTPServletResponse

HttpServletRequest and HttpServletResponse are the two objects created when user’s dynamic requests arrive on the server. From HttpServletRequest user can send data to the server, from HttpServletResponse object user can receive various forms of responses like HTML pages and others, we have shared a sample implementation of doGet() method in our previous tutorial, that was only publishing HTML response back to the user.

doPost in Http Servlet

Now we will illustrate an example of HttpServletRequest and HttpServletResponse in detail here. In this example, HttpServletRequest object gets the data from user HTML form and pass it to the doPost() method of the servlet that we will create and configure now.

There are simple steps you have to perform. Create a Servlet class in src folder.


And name it “XMLServlet”, implement it as mentioned below.


In our XMLServlet.java class we have implemented the doPost() method and make use of HttpServletRequest, HttpServletResponse objects named request and response. This servlet doPost() method will be called on from action as discussed bellow in the SimpleHTMLForm.html section, it will gets all the data from the HTML form through the request object and transform it into HTML and finally make it a response to response object and sends back to the user and will be displayed in the users browser.


Now create an HTML file in the “WebContent” folder and name it  as “SimpleHTMLForm” and implement it as mentioned below


In our SimpleHTMLForm.html, we have defined an HTML form and force it to use post method and on form action we have called our servlet through our Servlet URL mapping defined in web.xml.


Now Change your “web.xml” file and configure the created Servlet as mentioned below.


Now you can run the SimpleHTMLForm.html on the server and test it. To run on server Right click on “SimpleHTMLForm.html” à RunAS à Run on Server. Enter some data and click on Submit Query. It will show the response in the browser as we have created in doPost() method. Now we have successfully created our example and have a big picture of what is happening.


Let us discuss what we have done so far. We have created and edited contents in these 3 files. “XMLServlet.java” , “SimpleHTMLForm.html” and “web.xml”.

What we have done in our web.xml is another and older technique of defining “URLMapping” for the servlet, we have previously illustrated “URLMapping” through Annotations. When the request from user arrives on the server, the server will look in either the web.xml or in the defined annotations of the servlet class. If the servlet is found, the server will execute it on its JVM. Now the “web.xml” has all the servlet names, their class names with full package path and their URL mappings. If you know XML you can easily configure another servlet in here but it is not required for this tutorial.

Download Complete Servlets Project Here

Summary: In this tutorial we have accomplished following things, we got to know what the servlet class is and how to make use of request and response objects.

Up till now we got to know what the servlet class is and how to make use of request and response objects. There is more about servlets coming in our next coming tutorials, so stay tuned and  stay blessed.