O'Reilly logo

Head First Servlets and JSP, 2nd Edition by Bert Bates, Bryan Basham, Kathy Sierra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required