HttpServlet in Java

HttpServlet in Java: doGet, doPost, and Hello World Example (2026)

HttpServlet in Java: doGet, doPost, and Hello World Example

What is HttpServlet?

  • HttpServlet is an abstract class that exists in the javax.servlet.http package and extends GenericServlet.
  • When the servlet container uses the HTTP protocol to send a request, it creates HttpServletRequest and HttpServletResponse objects.
  • In HttpServlet, you do not need to override service(-, -). HttpServlet already does that and dispatches to the correct HTTP handler.

Methods provided by HttpServlet

  • protected void doGet(HttpServletRequest req, HttpServletResponse resp)

Called by the servlet service() method to handle HTTP GET requests from the client. Used for fetching data, because this is an idempotent method.

  • protected void doPost(HttpServletRequest req, HttpServletResponse resp)

Called by the servlet service() method to handle HTTP POST requests from the client. Used to insert data, because this method is non-idempotent.

  • protected void doDelete(HttpServletRequest req, HttpServletResponse resp)

Called by the servlet service() method to handle HTTP DELETE requests. Used to delete a resource.

  • protected void doPut(HttpServletRequest req, HttpServletResponse resp)

Called by the servlet service() method to handle HTTP PUT requests. Used to update a resource.

  • protected void doOptions(HttpServletRequest req, HttpServletResponse resp)

Called by the servlet service() method to handle HTTP OPTIONS requests. Used to determine which HTTP methods the server supports and return an appropriate header.

  • protected void doTrace(HttpServletRequest req, HttpServletResponse resp)

Called by the servlet service() method to handle HTTP TRACE requests. Used to trace a resource (for debugging).

  • protected void doHead(HttpServletRequest req, HttpServletResponse resp)

Called by the servlet service() method to handle HTTP HEAD requests. Used to see only the headers of a response, such as Content-Type or Content-Length.

Note: In web applications, you mostly use doGet() and doPost(). The other methods are used more often in web services.

Hello World example using HttpServlet

package com.deepsingh44.blogspot.cont;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class First extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        resp.setContentType("text/html");

        PrintWriter pw = resp.getWriter();

        pw.print("<h1>Hello Http Servlet Example</h1>");

    }

}

Map HttpServlet in web.xml

To access the servlet from the browser, create a URL mapping in web.xml:

<web-app>

    <servlet>
        <servlet-name>first</servlet-name>
        <servlet-class>com.deepsingh44.blogspot.cont.First</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>first</servlet-name>
        <url-pattern>/first</url-pattern>
    </servlet-mapping>

</web-app>

Run the code and check the output

Deploy the application to Tomcat and open the URL that matches your url-pattern. You should see the heading "Hello Http Servlet Example" in the browser.

What is happening here

  • extends HttpServlet — Your class inherits all the HTTP handler methods and the dispatching logic in service().
  • doGet() — Handles HTTP GET. It delegates to doPost(), so both GET and POST work the same way.
  • doPost() — Handles HTTP POST. It sets the content type, gets the writer, and prints the response.
  • setContentType("text/html") — Tells the browser that the response is HTML.
  • getWriter().print(...) — Sends the actual response body to the browser.

Where to go next

You now know what HttpServlet is, what it extends, the methods it provides, and how to write a Hello World servlet with it. You also know how to map it in web.xml and run it on Tomcat.

If you remember one thing, remember this: HttpServlet does the dispatching for you. It looks at the HTTP method (GET, POST, PUT, DELETE) and calls the matching doXxx method. You just override the ones you need.

If you want to go deeper, try these next steps:

  • Send a form with POST and read the parameters with request.getParameter("name"). Echo the value back in the response.
  • Try calling your servlet with PUT. You will get a 405 Method Not Allowed because you did not override doPut(). That is HttpServlet's default behavior.
  • Replace web.xml with the @WebServlet("/first") annotation. Compare both approaches.
  • Move on to JSP and MVC. HttpServlet is the controller in the MVC pattern. Learning how it interacts with JSP completes the picture.

No comments: