Home > Articles

  • Print
  • + Share This
This chapter is from the book

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?

    1. SESSIONID

    2. SERVLETID

    3. JSESSIONID

    4. CONTAINERID

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.

  • + Share This
  • 🔖 Save To Your Account