Java.Servlet.What are the main methods in the HttpServlet class?

🧠 Main Methods in HttpServlet

Here’s the main methods grouped by their purpose:

PurposeMethodDescription
Core dispatchingservice(HttpServletRequest req, HttpServletResponse resp)Dispatches the HTTP request to doGet(), doPost(), etc., based on HTTP method
Handle GET requestdoGet(HttpServletRequest req, HttpServletResponse resp)Handles HTTP GET requests
Handle POST requestdoPost(HttpServletRequest req, HttpServletResponse resp)Handles HTTP POST requests
Handle PUT requestdoPut(HttpServletRequest req, HttpServletResponse resp)Handles HTTP PUT requests
Handle DELETE requestdoDelete(HttpServletRequest req, HttpServletResponse resp)Handles HTTP DELETE requests
Handle HEAD requestdoHead(HttpServletRequest req, HttpServletResponse resp)Handles HTTP HEAD requests (like GET but without body)
Handle OPTIONS requestdoOptions(HttpServletRequest req, HttpServletResponse resp)Handles HTTP OPTIONS (returns allowed methods)
Handle TRACE requestdoTrace(HttpServletRequest req, HttpServletResponse resp)Handles HTTP TRACE (echoes back the request for debugging)

🎯 Quick Details on Each Important Method


1. service(HttpServletRequest req, HttpServletResponse resp)

  • Called by the servlet container (Tomcat, Jetty, etc.)
  • Decides which doXxx() method to call depending on HTTP method (GET, POST, PUT, etc.).

2. doGet(HttpServletRequest req, HttpServletResponse resp)

  • For handling normal browser requests.
  • Example: User clicks a link or types a URL.
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.getWriter().println("Hello, GET!");
}

3. doPost(HttpServletRequest req, HttpServletResponse resp)

  • For handling form submissions, data posting, etc.
  • Example: User submits a login form.
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String username = req.getParameter("username");
    resp.getWriter().println("Posted username: " + username);
}

4. doPut(HttpServletRequest req, HttpServletResponse resp)

  • For updating resources (e.g., REST APIs).

5. doDelete(HttpServletRequest req, HttpServletResponse resp)

  • For deleting resources (e.g., REST APIs).

6. doHead(HttpServletRequest req, HttpServletResponse resp)

  • Same as doGet(), but without sending response body.
  • Used to check metadata like Content-Length, headers, etc.

7. doOptions(HttpServletRequest req, HttpServletResponse resp)

  • Returns allowed HTTP methods for a resource.
  • Server automatically sets "Allow: GET, POST, PUT, DELETE, OPTIONS" header.

8. doTrace(HttpServletRequest req, HttpServletResponse resp)

  • Echoes back the received request.
  • Rarely used (mostly for network diagnostics).

⚡ Quick Visual Map

Browser → Servlet Container → HttpServlet.service()
         ├── if GET     → doGet()
         ├── if POST    → doPost()
         ├── if PUT     → doPut()
         ├── if DELETE  → doDelete()
         ├── if HEAD    → doHead()
         ├── if OPTIONS → doOptions()
         └── if TRACE   → doTrace()

🛠️ Bonus Tip:

You can override only the methods you need.
For example:

  • A login servlet usually overrides only doPost().
  • A catalog page usually overrides only doGet().

🔥 Final Mini Summary

MethodWhen to Override
service()Rare — only for very custom dispatching
doGet()Handle page loads, URL clicks
doPost()Handle form submissions, API posts
doPut()Handle updating resources (REST APIs)
doDelete()Handle deleting resources (REST APIs)
doHead()Handle HEAD requests (metadata only)
doOptions()Handle OPTIONS pre-flight (CORS, allowed methods)
doTrace()Handle TRACE requests (debugging, rare)
This entry was posted in Без рубрики. Bookmark the permalink.