November 28, 2020
Hot Topics:

The Servlet Container Model

  • By Que Publishing
  • Send Email »
  • More Articles »

Chapter Summary

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.

Containers have 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. The container also manages the servlet life-cycle by initializing a servlet with a call to the init() method, a call to the service() method upon every request, and by calling a servlet's destroy() method just prior to removing it from memory. The container also allows you to monitor context and session events with listeners that are event-driven triggers. When an attribute changes, special targeted methods are called. In them, you can define special actions such as "add a note to the log every time the user count changes."

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.


  • Redirection

  • Servlet Life-Cycle

  • Servlet Forwarding and Includes

  • Servlet attribute

  • Context parameters

  • Application session

  • listeners

Apply Your Knowledge

Review Questions

  1. What methods of the Servlet interface are invoked at different points in the servlet life cycle?

  2. What HTTP methods are supported by HttpServlet?

  3. What objects are passed to the servlet's service() method?

  4. What is a distributable application?

  5. Why is it a bad idea to synchronize a servlet's service() method?

  6. What is the relationship between an application's ServletConfig object and ServletContext object?

  7. What mechanisms are used by a Servlet Container to maintain session information?

  8. What are the four events that are defined in the Servlet API?

  9. How are request dispatchers used?

Exam Questions

  1. Which of the following methods are defined in the Servlet interface?

    1. init()

    2. service()

    3. finalize()

    4. destroy()

  2. Which of the following objects are passed to a servlet's service() method?

    1. ServletRequest

    2. HttpServletRequest

    3. ServletResponse

    4. HttpServletResponse

  3. By default, how many instances of a servlet are created by a Servlet Container?

    1. One

    2. One per request

    3. One per session

    4. None of the above

  4. Which of the following exceptions are defined by the Servlet API?

    1. ServletException

    2. InitializationException

    3. UnavailableException

    4. ServletContextException

  5. Which of the following are used by Servlet Containers to maintain session information?

    1. cookies

    2. hidden form fields

    3. HTTPS protocol information

    4. URL rewriting

  6. Which of the following event listeners are defined by the Servlet API?

    1. HttpSessionBindingListener

    2. HttpSessionEventListener

    3. HttpSessionParameterListener

    4. HttpSessionAttributeListener

  7. Which of the following methods are defined by the RequestDispatcher interface?

    1. dispatch()

    2. include()

    3. redirect()

    4. forward()

  8. Which of the following is the name of the cookie used by Servlet Containers to maintain session information?





Answers to Review Questions

  1. 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."

  2. The GET, POST, HEAD, PUT, DELETE, TRACE, and OPTIONS methods are supported by HttpServlet. Refer to the section, "Interfacing with HTML Requests."

  3. ServletRequest and ServletResponse objects are passed to the servlet's service method. Refer to the section, "Interfacing with HTML Requests."

  4. A distributable application is an application that is distributed over multiple JVMs. Refer to the In the Field, "How Does a Servlet Work?"

  5. 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."

  6. 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."

  7. Cookies, URL rewriting, and HTTPS protocol information are used to maintain session information. Refer to the section, "Session."

  8. The four events that are defined by the Servlet API are HttpSessionEvent, HttpSessionBindingEvent, ServletContextEvent, and ServletContextAttributeEvent. Refer to the section, "Servlet Life-cycle."

  9. 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

  1. C. The finalize() method is not defined by the Servlet interface. Refer to the section, "Servlet Life-cycle."

  2. A, C. ServletRequest and ServletResponse methods are passed to the service() method. Refer to the section, "Servlet Life-cycle."

  3. A. By default, only one instance of a servlet is created by a Servlet Container. Refer to the section, "Servlet Life-cycle."

  4. A, C. The Servlet API defines ServletException and UnavailableException. Refer to the section, "Servlet Life-cycle."

  5. A, C, D. Hidden form fields are not used by Servlet Containers to maintain session information. Refer to the section, "Form Parameters."

  6. A. Only HttpSessionBindingListener is defined by the Servlet API. Refer to the section, "Servlet Life-cycle."

  7. B, D. The RequestDispatcher interface defines the include() and forward() methods. Refer to the section, "Using a RequestDispatcher."

  8. C. The JSESSIONID cookie is used by Servlet Containers to maintain session information. Refer to the section, "Session."

Suggested Readings and Resources

  1. Sun's excellent J2EE Tutorial—java.sun.com/j2ee/tutorial/1_3-fcs/doc/ J2eeTutorialTOC.html.

  2. The Java Language Specification—(java.sun.com/docs/books/jls/second_edition/html/j.title.doc.html).

  3. Exam objectives for the Sun Certified Web Component Developer for J2EE Platform—http://suned.sun.com/US/certification/java/exam_objectives.html.

  4. The Java Servlet 2.3 Specification—http://jcp.org/aboutJava/communityprocess/first/ jsr053/index.html.

  5. Sun's official Servlet page—http://java.sun.com/products/servlet/.

  6. Java Software FAQ Index—http://java.sun.com/docs/faqindex.html.

  7. Tomcat—an implementation of the Java Servlet 2.2 and JavaServer Pages 1.1 Specifications—http://jakarta.apache.org/tomcat/index.html.

  8. Java Training by the MageLang Institute—http://www.magelang.com/.

  9. Servlets.com, Web site companion to Java Servlet Programming by O'Reilly—http://www.servlets.com/.

  10. Glossary of Java Technology-Related Terms—http://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

Page 5 of 5

This article was originally published on October 24, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date