The Servlet Container Model, Page 5
The HTTP methods GET, POST, and PUT are how browsers and Web servers trade data with each other. The GET retrieves a page without providing much information, while a POST can package huge amounts of information with its request. A PUT is for uploading a file. There are events associated with each type of request, such as clicking a hyperlink sending a GET request, but clicking a form button sends a POST request.
The most important objects in the servlet process are 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 a HttpServletRequest object which is passed to the servlet. Going the other way, the container wraps the response parameters with the HttpServletResponse object which is passed back to the container.
Lastly, the servlet specifies a RequestDispatcher object which performs servlet forwarding. Notice that this is different from redirection, where the servlet would return a new URL to the browser that triggers the browser to try to get that page. The RequestDispatcher doesn't redirect; rather it "dispatches" or performs forwarding.
Servlet Forwarding and Includes
Apply Your Knowledge
What methods of the Servlet interface are invoked at different points in the servlet life cycle?
What HTTP methods are supported by HttpServlet?
What objects are passed to the servlet's service() method?
What is a distributable application?
Why is it a bad idea to synchronize a servlet's service() method?
What is the relationship between an application's ServletConfig object and ServletContext object?
What mechanisms are used by a Servlet Container to maintain session information?
What are the four events that are defined in the Servlet API?
How are request dispatchers used?
Which of the following methods are defined in the Servlet interface?
Which of the following objects are passed to a servlet's service() method?
By default, how many instances of a servlet are created by a Servlet Container?
One per request
One per session
None of the above
Which of the following exceptions are defined by the Servlet API?
Which of the following are used by Servlet Containers to maintain session information?
hidden form fields
HTTPS protocol information
Which of the following event listeners are defined by the Servlet API?
Which of the following methods are defined by the RequestDispatcher interface?
Which of the following is the name of the cookie used by Servlet Containers to maintain session information?
Answers to Review Questions
The init() method is invoked during the initialization phase. The service() method is invoked during the request processing (service) phase. In other words, init() is invoked the first time the servlet runs, but service() is invoked once for every request the servlet receives. The destroy() method is invoked when the servlet is to be taken out of service. Refer to the section, "Servlet Life-cycle."
The GET, POST, HEAD, PUT, DELETE, TRACE, and OPTIONS methods are supported by HttpServlet. Refer to the section, "Interfacing with HTML Requests."
ServletRequest and ServletResponse objects are passed to the servlet's service method. Refer to the section, "Interfacing with HTML Requests."
A distributable application is an application that is distributed over multiple JVMs. Refer to the In the Field, "How Does a Servlet Work?"
When the synchronized keyword is used with a servlet's service() method, requests to that servlet are handled one at a time in a serialized manner. This means that the processing capabilities of the Servlet Container are minimized. Refer to the section, "Servlet Life-cycle."
An application's ServletConfig object contains its ServletContext object and provides access to this object via its getServletContext() method. Refer to the section, "Web Application Context."
Cookies, URL rewriting, and HTTPS protocol information are used to maintain session information. Refer to the section, "Session."
The four events that are defined by the Servlet API are HttpSessionEvent, HttpSessionBindingEvent, ServletContextEvent, and ServletContextAttributeEvent. Refer to the section, "Servlet Life-cycle."
Request dispatchers are used to forward requests to other servlets or to include the results of other servlets. Refer to the section, "Using a RequestDispatcher."
Answers to Exam Questions
C. The finalize() method is not defined by the Servlet interface. Refer to the section, "Servlet Life-cycle."
A, C. ServletRequest and ServletResponse methods are passed to the service() method. Refer to the section, "Servlet Life-cycle."
A. By default, only one instance of a servlet is created by a Servlet Container. Refer to the section, "Servlet Life-cycle."
A, C. The Servlet API defines ServletException and UnavailableException. Refer to the section, "Servlet Life-cycle."
A, C, D. Hidden form fields are not used by Servlet Containers to maintain session information. Refer to the section, "Form Parameters."
A. Only HttpSessionBindingListener is defined by the Servlet API. Refer to the section, "Servlet Life-cycle."
B, D. The RequestDispatcher interface defines the include() and forward() methods. Refer to the section, "Using a RequestDispatcher."
C. The JSESSIONID cookie is used by Servlet Containers to maintain session information. Refer to the section, "Session."
Suggested Readings and Resources
Sun's excellent J2EE Tutorialjava.sun.com/j2ee/tutorial/1_3-fcs/doc/ J2eeTutorialTOC.html.
The Java Language Specification(java.sun.com/docs/books/jls/second_edition/html/j.title.doc.html).
Exam objectives for the Sun Certified Web Component Developer for J2EE Platformhttp://suned.sun.com/US/certification/java/exam_objectives.html.
The Java Servlet 2.3 Specificationhttp://jcp.org/aboutJava/communityprocess/first/ jsr053/index.html.
Sun's official Servlet pagehttp://java.sun.com/products/servlet/.
Java Software FAQ Indexhttp://java.sun.com/docs/faqindex.html.
Tomcatan implementation of the Java Servlet 2.2 and JavaServer Pages 1.1 Specificationshttp://jakarta.apache.org/tomcat/index.html.
Java Training by the MageLang Institutehttp://www.magelang.com/.
Servlets.com, Web site companion to Java Servlet Programming by O'Reillyhttp://www.servlets.com/.
Glossary of Java Technology-Related Termshttp://java.sun.com/docs/glossary.html.
Source of this material
|This is Chapter 4: Servlet Container Model from the book Sun Certification Training Guide (310-080): Java 2 Enterprise Edition (J2EE) Web Component Developer (ISBN:0-7897-2821-4) written by Alain Trottier, published by Que. |
To access the full Table of Contents for the book