Wednesday, March 2, 2011

Chapter 9: Triggering HttpServlet GET, POST, and HEAD Methods

In the previous chapter, we saw how the GET and POST methods are different how to handle them. In this chapter, we are going to see how we can explicitly trigger these methods.

So, lets get started!!!

GET and POST Methods

As you might already know, these are the 3 important ways a request could get submitted to a servlet. Now, we are going to identify the triggers that might cause a browser to use any of these methods and also identify the advantages of using each of these methods.


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:

a. Clicking on a hyperlink
b. Changing the address directly by typing in the address textbox on a browser
c. An application that has HTML functionality, and submitting an HTML form where the method header is set to get as in method=get.
d. When the user selects a favorite from the Favorites list in his/her browser
e. When JavaScript is used to change location.href.

Usually the browser is configured to send a GET request even if no method is set explicitly by the HTML. Which means, that the GET is the default way in which a request would get submitted if the programmer does not explicitly set how they want the request to be submitted

The advantages 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 1,000 characters.
• It allows bookmarks.

The GET Method has a few disadvantages too:

• The amount of data that can be sent is limited (approx 1 kb)
• The data sent to the servlet is visible on the address bar (which is not so secure)


The POST method is used when a browser or application submits an HTML form with the method attribute set to post as in method=post. (Remember the form declaration example I showed in the previous chapter?)

The advantages 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.


This is something we havent seen so far. As you might have already guessed, the HEAD method has a corresponding doHead() method in a servlet that handles a request that gets submitted this way.

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 this 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 Advantage of using this method are:

a. It receives and returns very small messages
b. It is fast and lightweight on both ends

Previous Chapter: Chapter 8 - Servlet Request Types

Next chapter: Chapter 10 - Form Parameters

No comments:

Post a Comment

© 2013 by All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.