- Objectives
- Introduction
- Overriding HttpServlet GET, POST, and PUT Methods
- Triggering HttpServlet GET, POST, and PUT Methods
- Interfacing with HTML Requests
- Web Application Scope
- Servlet Life-cycle
- Using a RequestDispatcher
- Web Application Context
- Context Within a Distributable Web Application
- Chapter Summary
- Apply Your Knowledge
Triggering HttpServlet GET, POST, and PUT Methods
1.2 For each of the HTTP methods, GET, POST, and HEAD, identify triggers that might cause a browser to use the method, and identify benefits or functionality of the method.
GET
POST
HEAD
This exam objective focuses on what triggers the events or methods in your servlets. For example, what action can a client take that results in the doGet() method being called in your servlet?
GET
As noted previously, the GET type request is normally used for simple HTML page requests. The types of events that generate this type of request are clicking on a hyperlink, changing the address directly by typing in the address textbox on a browser or application that has HTML functionality, and submitting an HTML form where the method header is set to get as in method=get. Also, a GET request is triggered when selecting a favorite from the Favorites list and using JavaScript to change location.href. Usually the browser is configured to send a GET request even if no method is set explicitly by the HTML.
The benefits of the GET method are
It retrieves information such as a simple HTML page or the results of a database query.
It supports query strings (name-value pairs appended to URL). Servers usually limit query strings to about 1000 characters.
It allows bookmarks.
POST
This occurs when a browser or application submits an HTML form with the method attribute set to post as in method=post.
The benefits of the POST method are
It sends information to the server such as form fields, large text bodies, and key-value pairs.
It hides form data because it isn't passed as a query string, but in the message body.
It sends unlimited length data as part of its HTTP request body.
It disallows bookmarks.
HEAD
A browser or application will sometimes send a request to a server just to check the status or get information (for example, "can you handle file upload?") from the server.
The HEAD method returns the same header lines that a GET method would return; however, no body or content is returned. This is often accomplished by calling doGet(), setting the headers but not setting any output, and then returning the response (without any body) to the requester.
The primary benefit of this method is message size. The HEAD method receives and returns very small messages. Therefore it is fast and lightweight on both ends.