Nearby lessons
7 of 34Servlet - HttpServlet
- 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
Demo: HttpServlet with GET and POST
login.html: Sends POST request to the servlet
login.html Form
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.
HttpServlet Lifecycle
The complete request processing flow:
- Browser sends HTTP request to the web server
- Web server checks if the request is for static or dynamic information
- For static information: server provides the response (or 404 if unavailable)
- For dynamic information: server forwards to the web container
- Container identifies the servlet class based on URL pattern (using web.xml or annotations)
- Container checks if a servlet object exists; if not, loads class, instantiates, and calls init()
- Container creates ServletRequest and ServletResponse objects and calls public service(SR, SR)
- If our class has public service(SR, SR): it's executed directly
- If not: HttpServlet's public service() is called, which type-casts and calls protected service(HSR, HSR)
- If our class has protected service(HSR, HSR): it's executed directly
- If not: HttpServlet's protected service() identifies the HTTP method and invokes the corresponding doXxx() method
- If our class has doXxx(): it's executed
- 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
| Case | What 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 service | Public 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() exists | HttpServlet doGet() returns 405 GET not supported |
| 5: POST request, no doPost() but doGet() exists | HttpServlet doPost() returns 405 POST not supported |
| 6: Common response for GET and POST | Implement 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:
- HttpServlet extends GenericServlet
- Two service methods: public and protected
- Request method determines which doXxx() is called
- Exam-style questions at the end