The HTTP request Method determines whether doGet() or doPost() runs

The client’s request, remember, always includes a specific HTTP Method. If the HTTP Method is a GET, the service() method calls doGet(). If the HTTP request Method is a POST, the service() method calls doPost().

image with no caption

HTTP requests

image with no caption

You probably won’t care about any HTTP Methods except GET and POST

Yes, there are other HTTP 1.1 Methods besides GET and POST. There’s also HEAD, TRACE, OPTIONS, PUT, DELETE, and CONNECT.

All but one of the eight has a matching doXXX() method in the HttpServlet class, so besides doGet() and doPost(), you’ve got doOptions(), doHead(), doTrace(), doPut(), and doDelete(). There’s no mechanism in the servlet API for handling doConnect(), so it’s not part of HttpServlet.

But while the other HTTP Methods might matter to, say, a web server developer, a servlet developer rarely uses anything but GET and POST.

For most (or probably all) servlet development, you’ll use either doGet() (for simple requests) or doPost() (to accept and process form data), and you won’t have to think about the others.

Get Head First Servlets and JSP, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.