Tuesday, March 1, 2011

Chapter 8: Servlet Request Types

As we saw in the previous chapter, we have many different types of requests that can be handled by the Servlet. For simplicity sake (more importantly the exam has these only) we are going to look at the 3 main types which are Get, Post and Put.

So, lets get started!!!


The GET type request is normally used for simple HTML page requests. The Servlets method that will handle this type of request would look like:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
// code to handle Get Request here

This is probably the first of the servlet methods any programmer begins his j2ee journey with. Even if this is the only method in your servlet, you’ll be able to handle a majority of the requests that get submitted to it. When a request gets submitted using the Get method, the whole URL in the browsers address bar gets passed on to the servlet and will be parsed & understood by the servlet when the request is processed.

Note: The init() and service() methods involved in a request are already provided by the HttpServlet class, so they don't need to be overridden. Overriding the init() method is always a good idea and overriding the service method can be done if you want (remember the example in the previous chapter???)

On the Job: The GET is the most common type of browser request.


The POST type request is frequently used by pages that use HTML forms. A typical doXXX method that handles these requests would look as below:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
//Code to handle Post request

The POST method is more sophisticated than a GET request. Normally, a Web form has fields whose names and values are sent to the server in key-value pairs. The POST is designed for posting large messages or providing a block of data, such as the result of submitting a form; and submitting long data fields to a database etc.

The amount of data you can send from the browser to the Servlet using POST is much larger than what we can send using GET.

Comparison Between Get and Post:

Query string or form data is simply appended to the URL as name-value pairs. Form name-value pairs are sent in the body of the request, not in the URL itself.
Query length is limited (~1KB). Query length is unlimited.
Users can see data in address bar. Data hidden from users.
Not Safe – A person standing over your shoulder can view your userid/pwd if submitted via Get Safe – No one will be able to view what data is getting submitted
invokes doGet() invokes doPost()
Supports ASCII. Supports ASCII + Binary.
Easy to bookmark Hard to bookmark.


The PUT type request is used for uploading files to the server. While uploading is its original intent, it isnt used much. Instead, people prefer the POST to upload files over PUT. The doXXX method that handles PUT requests would look like below:

public void doPut(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
//code to handle PUT request goes here

The doPut() method is called by the server (via the service method) to handle a PUT request. Uploading files from a browser has always been tricky. The idea behind the PUT operation is to make uploading easy. It is supposed to allow a client to place a file on the server, just like sending a file by FTP. The javadoc for this method warns that when overriding this method, you should leave intact any content headers sent with the request (including Content-Length, Content-Type, Content-Transfer-Encoding, Content-Encoding, Content-Base, Content-Language, Content-Location, Content-MD5, and Content-Range). This method is rarely used, but it is powerful if you decide on using it.

Setting Request Submission Type:

We have seen how the GET/POST request submissions work and what their features are. But, we havent yet seen how to set the request submission type. Well, its pretty straightforward. Lets look at an example:

< form method= "GET" action="/myservlet/process" >

The above is a form declaration where the method is set as “GET”. You can replace this with “POST” and your JSP would work without any issues (Remember that you need to have the corresponding doXXX method in your servlet to handle the request that gets submitted)

Previous Chapter: Chapter 7 - Overriding HttpServlet GET, POST, and PUT Methods

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

No comments:

Post a Comment

© 2013 by www.inheritingjava.blogspot.com. 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.


Google+ Followers