HttpServlet Class in Servlet explained with example

In this tutorial, we will learn about the HttpServlet class and its important methods. We will explain the purpose of all the methods with an example. Before discussing all its methods let's first understand what is HttpServlet class? Before discussing HttpServlet class in detail. I will recommend you to first understand the basics of HTTP Request Methods. Because the HttpServlet class is totally based on HTTP Request Methods.

What is HttpServlet class?

The HttpServlet class extends the GenericServlet. It is commonly used when developing servlets that recieve and process HttpRequest.HttpServlet class provides Http specific methods such as doGet(), doPost(), doHead(), doDelete(),
doPut() etc. The HttpServlet class doesn't override the service() method,instead this its override doGet() or doPost() method or both.

Methods of HttpServlet Class:

The methods of the HttpServlet class are summarized below. Before discussing all its methods in details. I will be recommended you to first learn what is an Exception because all the method of HttpServlet class throws an Exception.

1) public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException.
This method is called by the servlet service() method. It is used to handle HTTP Get request from the client browser.
2) public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException.
This method is called by the Servlet service() method. It is used to handle HTTP Post request from the client browser.

3) public void doDelete(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException.
This method is called by the Servlet service() method. It is used to handle HTTP delete request from the client browser.

4) public void doHead(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException.
This method is called by the Servlet service() method. It is used to handle HTTP head request from the client browser.


5) public void doPut(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException.
This method is called by the Servlet service() method. It is used to handle HTTP Put request from the client browser. This method is used to send the file to the server.


6) public void doTrace(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException.
This method is called by the Servlet service() method. It is used to handle HTTP trace request from the client browser. It is used for debugging purposes.

7) public void doOption(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException.
This method is called by the Servlet service() method. It is used to handle the HTTP option request from the client browser.


8) public long getLastModified(HttpServlerRequest req):
 It returns the time(in milliseconds since midnight,january1,1970, GMT)when the requested resource was last modified.

9) public void Service(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException:
It is called by the server when an HTTP request arrives for this servlet. The arguments provide access to the HTTP request and response, respectively.



Example of HttpServlet class:

In this Example, we are creating a HttpServlet by extending the HttpServlet Class.

HttpServletExample.java file:
HttpServlet Class in Servlet

Web.xml file
HttpServlet Class in Servlet

Output:
HttpServlet Class in Servlet
You may also like:

Session Management using URL Rewriting in Servlet
Session Management using URL Rewriting in Servlet
What are Session tracking and its techniques?

HttpServlet Class in Servlet explained with example HttpServlet Class in Servlet explained with example Reviewed by Prashant Srivastava on October 14, 2018 Rating: 5

No comments:

Powered by Blogger.