Tuesday, March 1, 2011

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

In the previous chapter we saw how a Servlet handles a Http Request that comes from a web browser. We also saw that there are many doXXX() methods inside a Servlet that serve a specific purpose. In this chapter we are going look at the XXX part of the Servlet code.

So, lets get started!!!

The doXXX Methods

There are 3 main types of requests that get processed by a Servlet. They are:
• Get
• Post
• Put
Each of them have a corresponding doXXX() method in the Servlet class which would be:
• doGet
• doPost
• doPut

In the exam you can expect many questions related to this topic so pay attention!!!

These methods are called by the service method in your Servlet.

Let us now take a look at a sample Servlet that has these 3 doXXX methods.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

* A servlet that has the 3 doXXX Methods
* @author Anand

public class OurSecondServlet extends HttpServlet
// doGet()
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
requestType("GET", response);

// doPost()
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
requestType("POST", response);

// doPut()
public void doPut(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
requestType("PUT", response);

public void requestType(String requestType,
HttpServletResponse response)
throws IOException, ServletException

PrintWriter out = response.getWriter();

out.println("< html >");
out.println("< head >");
out.println("< title >Our Second Servlet" +
"< / title >");
out.println("< / head >");
out.println("< body >");
out.println("< h1 >Servlet Request< / h1 >");
out.println("The Submitted Request type is : " + requestType);
out.println("< / body >");
out.println("< / html >");

How this code works?

As you can see, we have 3 different doXXX methods in our servlet, each of them calling the same method with just one parameter that differentiates them. Ideally in most cases, our code would be like this too. The logic that gets executed is mostly the same irrespective of the type of request submission. So, the doXXX methods, just receive the request, add some identifier that can be used to identify what request is submitted and then calls another method which will eventually be called by the other doXXX methods too.

The delegate method does the actual processing and returns the results.

In this case, based on the doXXX method calling it, the output will be different. If you submit using the GET option, you will get the below screen:

As mentioned earlier, the service method would invoke the appropriate doXXX method based on the request submission type. But, if you are curious and wonder how the code for such a service method would look like, well, you don't have to search anymore. Here is how it would look:

protected void service(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, IOException
String method = req.getMethod();

if (method.equals(METHOD_GET))
// Call doGet
doGet(req, resp);
} else if (method.equals(METHOD_POST))
// call doPost
doPost(req, resp);
} else if (method.equals(METHOD_PUT))
// call doPut
doPut(req, resp);
} else
// Our Servlet doesn't currently support
// other types of request.
String errMsg = "Method Not Supported");
HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);

Previous Chapter: Chapter 6 - Servlet Handling of Http Request

Next Chapter: Chapter 8 - Servlet Request Types

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.