Chapter 4. Request and Response: Being a Servlet

image with no caption

Servlets live to service clients. A servlet’s job is to take a client’s request and send back a response. The request might be simple: “get me the Welcome page.” Or it might be complex: “Complete my shopping cart check-out.” The request carries crucial data, and your servlet code has to know how to find it and how to use it. The response carries the info the browser needs to render a page (or download bytes), and your servlet code has to know how to send it. Or not... your servlet can decide to pass the request to something else (another page, servlet, or JSP) instead.


The Servlet Technology Model


For each of the HTTP Methods (such as GET, POST, HEAD, and so on), describe the purpose of the method and the technical characteristics of the HTTP Method protocol, list triggers that might cause a client (usually a Web browser) to use the Method, and identify the HttpServlet method that corresponds to the HTTP Method.


Using the HttpServletRequest interface, write code to retrieve HTML form parameters from the request, retrieve HTTP request header information, or retrieve cookies from the request.


Using the HttpServletResponse interface, write code to set an HTTP response header, set the content type of the response, acquire a text stream for the response, acquire a binary stream for the response, redirect an HTTP request to another URL, or add cookies to the response.[a]


Describe the purpose and event sequence of the servlet lifecycle: (1) servlet class loading, (2) servlet instantiation, (3) call the init() method, (4) call the service() method, and (5) call the destroy() method.

[a] We won’t say much about the objectives related to cookies until the Sessions chapter.

Coverage Notes:

All of the objectives in this section are covered completely in this chapter, with the exception of the cookies part of objective 1.3. A lot of the content in this chapter was touched on in chapter two, but in chapter two we said, “Don’t worry about memorizing it.”

In this chapter, you DO have to slow down, really study, and memorize the content. No other chapter will cover these objectives in detail, so this is it.

Do the exercises in the chapter, review the material, then take your first mock exam at the end of the chapter. If you don’t get at least 80% correct, go back through the chapter to figure out what you missed, BEFORE you move on to chapter five.

Some of the mock exam questions that belong with these objectives have been moved into Chapter 5 and Chapter 6, because the questions require additional knowledge of some of the topics we don’t cover until those chapters. That means there are fewer mock exam questions in this chapter, and more in later chapters, to avoid testing you on topics you haven’t covered.

Important note: while the first three chapters covered background information, from this page forward in the book, virtually everything you’re going to see is directly related to or explicitly part of the exam.

Get Head First Servlets and JSP, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.