- 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
Apply Your Knowledge
Review Questions
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?
Exam Questions
-
Which of the following methods are defined in the Servlet interface?
-
init()
-
service()
-
finalize()
-
destroy()
-
Which of the following objects are passed to a servlet's service() method?
-
ServletRequest
-
HttpServletRequest
-
ServletResponse
-
HttpServletResponse
-
By default, how many instances of a servlet are created by a Servlet Container?
-
One
-
One per request
-
One per session
-
None of the above
-
Which of the following exceptions are defined by the Servlet API?
-
ServletException
-
InitializationException
-
UnavailableException
-
ServletContextException
-
Which of the following are used by Servlet Containers to maintain session information?
-
cookies
-
hidden form fields
-
HTTPS protocol information
-
URL rewriting
-
Which of the following event listeners are defined by the Servlet API?
-
HttpSessionBindingListener
-
HttpSessionEventListener
-
HttpSessionParameterListener
-
HttpSessionAttributeListener
-
Which of the following methods are defined by the RequestDispatcher interface?
-
dispatch()
-
include()
-
redirect()
-
forward()
-
Which of the following is the name of the cookie used by Servlet Containers to maintain session information?
-
SESSIONID
-
SERVLETID
-
JSESSIONID
-
CONTAINERID
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.