J2EE Web Component Developer: Servlet Container Model
- 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
Objectives
This chapter covers the following objectives listed by Sun in "Section 1The Servlet Model" and "Section 3The Servlet Container Model."
1.1 For each of the HTTP methods, GET, POST, and PUT, identify the corresponding method in the HttpServlet class.
The HTTP methods GET, POST, and PUT are how browsers and Web servers communicate the purpose of communication. A GET simply wants to retrieve a page without providing much information. A POST, however, can package lots of form or file information with its request. A PUT is for uploading a file. The HttpServlet class has a corresponding method for each HTTP method, including doGet(), doPost(), and doPut().
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.
This objective asks you to understand the events associated with each type of request. For example, clicking a hyperlink will send a GET request to a Web server, but clicking a Submit button (when the action is set to "post") will send a POST request.
1.3 For each of the following operations, identify the interface and method name that should be used to
- Retrieve HTML form parameters from the request
- Retrieve a servlet initialization parameter
- Retrieve HTTP request header information
- 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
This objective is huge. It encompasses the heart of a servlet process, especially the request and response objects. The request parameters for the servlet are the strings sent by the client to the Servlet Container. The container parses the request and puts the information in an HttpServletRequest object which is passed to the servlet. Going the other way, the container wraps the response parameters in an HttpServletResponse object which is passed back to the container. The associated chapter section later in this chapter ("Overriding HttpServlet GET, POST, and PUT methods") goes into much detail on the methods involved.
1.4 Identify the interface and method to access values and resources and to set object attributes within the following three Web scopes:
- Request
- Session
- Context
This objective addresses the idea of scope. When something has Context scope, it is application-wide and all users can share data. Session scope means one user can share data across page views, but other users can't. Request scope restricts data to only that page.
1.5 Given a life-cycle method, identify correct statements about its purpose or about how and when it is invoked. These methods are
- init
- service
- destroy
The container manages the servlet life-cycle. This part of the chapter explains, with examples, how the container initializes a servlet with a call to the init() method. Then it calls the service() method upon every request. Finally, when the servlet is about to be removed from memory, the container calls its destroy() method. This gives the servlet one last chance to clean up resources.
1.6 Use a RequestDispatcher to include or forward to a Web resource.
The RequestDispatcher object is the servlet forwarding mechanism. You will see in the section "Servlet Life-cycle" how you can transfer processing of the request from one servlet to another (which the browser will be unaware of). This is how a servlet can pass the request to some other Web component within the same Web container.
3.1 Identify the uses for and the interfaces (or classes) and methods to achieve the following features:
- Servlet context initialization parameters
- Servlet context listener
- Servlet context attribute listener
- Session attribute listeners
These elements let you get and monitor servlet attributes. Not only can you get them and change them too, but you can actually put in place behavior to occur when an attribute changes. The listeners are event-driven triggers. When an attribute changes, special targeted methods are called. In them, you can define special actions, such as adding a note to the log every time the user count changes (perhaps a context attribute called counter).
3.3 Distinguish the behavior of the following in a distributable:
- Servlet context initialization parameters
- Servlet context listener
- Servlet context attribute listener
- Session attribute listeners
As explained in the previous objective, these elements let you get and monitor Servlet attributes. There is a difference here in that Sun wants you to understand how this works in a distributable Web application.
Outline
Introduction
Overriding HttpServlet GET, POST, and PUT Methods
GET
POST
PUT
Triggering HttpServlet GET, POST, and PUT Methods
GET
POST
HEAD
Interfacing with HTML Requests
Form Parameters
Retrieving a Servlet Initialization Parameter
Retrieving HTTP Request Header Information
Acquiring a Binary Stream for the Response
Redirecting an HTTP Request to Another URL
Web Application Scope
Request
Session
Context
Servlet Life-cycle
Using a RequestDispatcher
Web Application Context
Context Within a Distributable Web Application
The key to this section of the exam is understanding how servlets implement the Servlet interface, which defines life-cycle methods. The Servlet Container (such as Apache Tomcat) is itself an application that monitors a port on a given IP address. Servlets generate responses to HTTP requests. To do so, the container loads your servlet (if it isn't in memory already) and calls the methods defined in the interface. This is the foundation of servlet and JSP architecture.
There are many methods to know. It is easier if you learn the methods in groups according to theme. For example, write a servlet that has HttpServlet methods which handle all three GET, POST, and PUT types of request.
Each JavaServer Page is transformed into a servlet that is compiled and then loaded. Therefore much of what you learn here applies to the JSP section of the exam too.