Let’s learn completely about the HttpServlet class in servlet and know what are the most important methods used by HttpServlet Class in this tutorial. You may also discover the purpose of all the methods with an example from here. So, make use of these links available & understand the concept thoroughly with ease.
- What is HttpServlet class?
- Hierarchy of Http Servlet in Servlet
- How Http Servlet class works?
- Methods of HttpServlet Class
- HttpServlet class Example
What is HttpServlet class?
The HttpServlet class extends the GenericServlet. It is commonly used when developing servlets that receive and process HttpRequest. Basically, HttpServlet class totally depends on HTTP Request Methods. HttpServlet class implements Http specific methods like doGet(), doPost(), doHead(), doDelete(), doPut() etc.
In HTTP Servlet there is no requirement to override the service() method as this method dispatches the Http Requests to the correct method handler. Let’s check how it works and what is the hierarchy of the HTTP servlet class in the below sections.
Hierarchy of Http Servlet in Servlet
Here is the hierarchy of the httpservlet class in servlet:
java.lang.Object |_extended byjavax.servlet.GenericServlet |_extended byjavax.servlet.http.HttpServlet
How Http Servlet class works?
As you can see the working of the HTTP servlet class from the below-illustrated image:
Do Refer:
- GenericServlet class in Servlet with Example
- Session Management Using Hidden Form Field in Servlet with Example
Methods of HttpServlet Class
We have compiled the most important methods of the HttpServlet class below. Check out them thoroughly and make use of these methods in your further practice examples:
1. public void doGet(HttpServletRequest req, HttpServletResponse res): throws ServletException, IOException This method is called the servlet service() method. It is used to handle HTTP Get requests from the client browser.
2. public void doPost(HttpServletRequest req, HttpServletResponse res): throws ServletException, IOException. This method is called the Servlet service() method. It is used to handle HTTP Post requests from the client browser.
3. public void doDelete(HttpServletRequest req, HttpServletResponse res) throws ServletException IOException. This method is called the Servlet service() method. It is used to handle HTTP delete requests from the client browser.
4. public void doHead(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException. This method is called the Servlet service() method. It is used to handle HTTP head requests from the client browser.
5. public void doPut(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException. This method is called the Servlet service() method. It is used to handle HTTP Put requests 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 the Servlet service() method. It is used to handle HTTP trace requests 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 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, January 1, 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.
HttpServlet class Example:
In this instance, we are creating a HttpServlet by extending the HttpServlet Class.
HttpServletExample.java file:
Web.xml file
Output: