Nearby lessons

7 of 34

Servlet - HttpServlet

📌 What You Will Learn
  • Understand HttpServlet class hierarchy
  • Learn the service() → doXxx() dispatch mechanism
  • See complete working code examples with GET and POST

HttpServlet is the base class for developing HTTP-based servlets. For every HTTP method (GET, POST, etc.), HttpServlet provides a corresponding doXxx() method. This lesson covers HttpServlet lifecycle and complete code examples.

HttpServlet Class Hierarchy

HttpServlet is the child class of GenericServlet and acts as the base class for developing HTTP-based servlets.

Class hierarchy:

ServletConfig (I) → Servlet (I) → Serializable (I)
GenericServlet (AC) → HttpServlet (AC)

For every HTTP method XXX, HttpServlet contains the corresponding doXxx() method:

protected void doXxx(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException

HttpServlet contains 2 service methods:

  • public void service(ServletRequest req, ServletResponse resp)
  • protected void service(HttpServletRequest req, HttpServletResponse resp)

HttpServlet Service Methods

Example02
JCode Cell
1 
2public void service(ServletRequest req,ServletResponse resp)throws SE,IOE
3protected void service(HttpServletRequest req,HttpServletResponse resp)throws SE,IOE
4

Demo: HttpServlet with GET and POST

login.html: Sends POST request to the servlet

login.html Form

Example04
JCode Cell
1 
2<html>
3<body><h1> This is HttpServletDemo to send Post request</h1>
4<form action = "/advapps1C/test" method="POST">
5Enter Name :<input type="text" name="uname"><br/>
6<input type="submit" value="Login">
7</form>
8</body>
9</html>
10

FirstServlet.java extending HttpServlet

Folder structure: advapps1C/ → login.html, WEB-INF/ → classes/ → FirstServlet.class

If we send a GET request, doGet() method is executed. If we send a POST request, doPost() method is executed.

Example05
JCode Cell
1 
2import javax.servlet.*;
3import javax.servlet.http.*;
4import java.io.*;
5import javax.servlet.annotation.*;
6@WebServlet("/test")
7public class FirstSevlet extends HttpServlet
8{
9public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException
10{
11PrintWriter out = resp.getWriter();
12out.println("<h1>This is from doGET()method...</h1>");
13}
14public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException
15{
16PrintWriter out = resp.getWriter();
17out.println("<h1>This is from doPOST()method...</h1>");
18}
19}
20
Output

<h1>This is from doGET()method...</h1>
<h1>This is from doPOST()method...</h1>
      

HttpServlet Lifecycle

The complete request processing flow:

  1. Browser sends HTTP request to the web server
  2. Web server checks if the request is for static or dynamic information
  3. For static information: server provides the response (or 404 if unavailable)
  4. For dynamic information: server forwards to the web container
  5. Container identifies the servlet class based on URL pattern (using web.xml or annotations)
  6. Container checks if a servlet object exists; if not, loads class, instantiates, and calls init()
  7. Container creates ServletRequest and ServletResponse objects and calls public service(SR, SR)
  8. If our class has public service(SR, SR): it's executed directly
  9. If not: HttpServlet's public service() is called, which type-casts and calls protected service(HSR, HSR)
  10. If our class has protected service(HSR, HSR): it's executed directly
  11. If not: HttpServlet's protected service() identifies the HTTP method and invokes the corresponding doXxx() method
  12. If our class has doXxx(): it's executed
  13. If not: HttpServlet's doXxx() returns 405 Method Not Allowed

Method call order:
public service(SR, SR)protected service(HSR, HSR)doXxx(HSR, HSR)

Finally, the web container calls destroy() when the servlet is taken out of service.

Servlet Method Override Cases

CaseWhat Happens
1: Override public service(SR,SR)This method handles ALL request types (GET, POST, etc.) — doXxx() is never called
2: Override both public and protected servicePublic service(SR,SR) handles all requests — protected service is bypassed
3: Override protected service() + doGet()Protected service() handles all requests including GET — doGet() is never called
4: GET request, no doGet() but doPost() existsHttpServlet doGet() returns 405 GET not supported
5: POST request, no doPost() but doGet() existsHttpServlet doPost() returns 405 POST not supported
6: Common response for GET and POSTImplement doGet() and have doPost() call doGet(req, resp)

Case 6: Common Response for GET and POST

To provide the same response for both GET and POST requests:

Example08
JCode Cell
1 
2public class FirstSevlet extends HttpServlet
3{
4public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException
5{
6PrintWriter out = resp.getWriter();
7out.println("<h1>This is common response for both GET and Post methods...</h1>");
8}
9public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException
10{
11doGet(req,resp);
12}
13}
14
Output

<h1>This is common response for both GET and Post methods...</h1>
      
📝 Key Takeaways
  • HttpServlet extends GenericServlet
  • Two service methods: public and protected
  • Request method determines which doXxx() is called
  • Exam-style questions at the end

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3