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
Loading editor…

Demo: HttpServlet with GET and POST

login.html: Sends POST request to the servlet

login.html Form

Example04
Loading editor…

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
Loading editor…

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
Loading editor…
📝 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

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.