You are on page 1of 2

Document Information

Preface

Part I Introduction Your choices regarding cookies on this site


1. Overview

The Java EE 5 Tutorial


2. Using the Tutorial Examples

Part II The Web Tier


3. Getting Started with Web Applications
By using our sites, Oracle and our third party partners may use cookies and similar technologies to collect user data for the following purposes:
4. Java Servlet Technology
Home | Download
required cookies are necessary for the site| to
PDF | FAQ for
function, | Feedback
example, to remember your log-in details and provide secure log-in
What Is a Servlet?
The Example Servlets functional cookies help us to optimize the site, for example, to collect statistics, to save your preferences for social interactions or to enable you
Troubleshooting Duke's Bookstore Database Problemsto post comments on our site
advertising cookies allow Oracle and our third party partners to provide you with interest-based advertising and tailored content across your
Servlet Life Cycle
browsers and devices Servlet Life Cycle
Handling Servlet Life-Cycle Events

Please visit our privacy policy forThe lifeinformation.


cycle of a servlet is controlled by the container in which the servlet has been deployed. When a request is mapped to a servlet, the container performs the
Defining the Listener Class
more
Specifying Event Listener Classes following steps.
Handling Servlet Errors
1. If an instance of the servlet does not exist, the web container
Accept all Decline all View and change cookie preferences
Sharing Information
a. Loads the servlet class.
Using Scope Objects
Controlling Concurrent Access to Shared Resources b. Creates an instance of the servlet class.
Accessing Databases
c. Initializes the servlet instance by calling the init method. Initialization is covered in Initializing a Servlet.
Initializing a Servlet
Writing Service Methods Privacy
2. Invokes the service method, passing request and response objects. Policymethods
Service | Powered bydiscussed in Writing Service Methods.
are
Getting Information from Requests
Constructing Responses If the container needs to remove the servlet, it finalizes the servlet by calling the servlet’s destroy method. Finalization is discussed in Finalizing a Servlet.
Filtering Requests and Responses
Programming Filters
Handling Servlet Life-Cycle Events
Programming Customized Requests and Responses
You can monitor and react to events in a servlet’s life cycle by defining listener objects whose methods get invoked when life-cycle events occur. To use these listener
objects you must define and specify the listener class.
Specifying Filter Mappings

Invoking Other Web Resources Defining the Listener Class


Including Other Resources in the Response You define a listener class as an implementation of a listener interface. Table 4-2 lists the events that can be monitored and the corresponding interface that must be
Transferring Control to Another Web Component implemented. When a listener method is invoked, it is passed an event that contains information appropriate to the event. For example, the methods in the
HttpSessionListener interface are passed an HttpSessionEvent, which contains an HttpSession.
Accessing the Web Context
Maintaining Client State Table 4-2 Servlet Life-Cycle Events
Accessing a Session
Associating Objects with a Session
Object Event Listener Interface and Event Class
Notifying Objects That Are Associated with a Session

Session Management Web context (see Accessing the Initialization and destruction javax.servlet.ServletContextListener and
Web Context)
Session Tracking ServletContextEvent
Finalizing a Servlet
Attribute added, removed, or javax.servlet.ServletContextAttributeListener and
Tracking Service Requests
replaced
Notifying Methods to Shut Down ServletContextAttributeEvent
Creating Polite Long-Running Methods
Session (See Maintaining Client Creation, invalidation, activation, javax.servlet.http.HttpSessionListener,
Further Information about Java Servlet Technology State) passivation, and timeout javax.servlet.http.HttpSessionActivationListener, and
5. JavaServer Pages Technology HttpSessionEvent
6. JavaServer Pages Documents
7. JavaServer Pages Standard Tag Library Attribute added, removed, or javax.servlet.http.HttpSessionAttributeListener and
replaced
8. Custom Tags in JSP Pages HttpSessionBindingEvent
9. Scripting in JSP Pages
10. JavaServer Faces Technology Request A servlet request has started javax.servlet.ServletRequestListener and
11. Using JavaServer Faces Technology in JSP Pages being processed by web ServletRequestEvent
12. Developing with JavaServer Faces Technology components
13. Creating Custom UI Components
Attribute added, removed, or javax.servlet.ServletRequestAttributeListener and
14. Configuring JavaServer Faces Applications
replaced ServletRequestAttributeEvent
15. Internationalizing and Localizing Web Applications

Part III Web Services


16. Building Web Services with JAX-WS The tut-install/javaeetutorial5/examples/web/bookstore1/src/java/com/sun/bookstore1/listeners/ContextListener class creates and removes
17. Binding between XML Schema and Java Classes the database access and counter objects used in the Duke’s Bookstore application. The methods retrieve the web context object from ServletContextEvent and then
18. Streaming API for XML store (and remove) the objects as servlet context attributes.
19. SOAP with Attachments API for Java
import database.BookDBAO;
Part IV Enterprise Beans import javax.servlet.*;
20. Enterprise Beans import util.Counter;
21. Getting Started with Enterprise Beans
22. Session Bean Examples import javax.ejb.*;
23. A Message-Driven Bean Example import javax.persistence.*;

Part V Persistence public final class ContextListener


24. Introduction to the Java Persistence API implements ServletContextListener {
25. Persistence in the Web Tier private ServletContext context = null;
26. Persistence in the EJB Tier
27. The Java Persistence Query Language @PersistenceUnit
EntityManagerFactory emf;
Part VI Services
28. Introduction to Security in the Java EE Platform
public void contextInitialized(ServletContextEvent event) {
29. Securing Java EE Applications context = event.getServletContext();
30. Securing Web Applications try {
31. The Java Message Service API BookDBAO bookDB = new BookDBAO(emf);
32. Java EE Examples Using the JMS API context.setAttribute("bookDB", bookDB);
33. Transactions } catch (Exception ex) {
34. Resource Connections System.out.println(
35. Connector Architecture "Couldn’t create database: " + ex.getMessage());
}
Part VII Case Studies
Counter counter = new Counter();
36. The Coffee Break Application context.setAttribute("hitCounter", counter);
37. The Duke's Bank Application counter = new Counter();
Part VIII Appendixes context.setAttribute("orderCounter", counter);
A. Java Encoding Schemes
}
B. About the Authors
public void contextDestroyed(ServletContextEvent event) {
Index context = event.getServletContext();
BookDBAO bookDB = context.getAttribute("bookDB");
bookDB.remove();
context.removeAttribute("bookDB");
context.removeAttribute("hitCounter");
context.removeAttribute("orderCounter");
}
}

Specifying Event Listener Classes


You specify an event listener class using the listener element of the deployment descriptor. Review The Example Servlets for information on how to specify the
ContextListener listener class.

You can specify an event listener using the deployment descriptor editor of NetBeans IDE by doing the following:

1. Expand your application’s project node.

2. Expand the project’s Web Pages and WEB-INF nodes.

3. Double-click web.xml.

4. Click General at the top of the web.xml editor.

5. Expand the Web Application Listeners node.

6. Click Add.

7. In the Add Listener dialog, click Browse to locate the listener class.

8. Click OK.

Handling Servlet Errors


Any number of exceptions can occur when a servlet executes. When an exception occurs, the web container generates a default page containing the message

A Servlet Exception Has Occurred

But you can also specify that the container should return a specific error page for a given exception. Review the deployment descriptor file included with the example to
learn how to map the exceptions exception.BookNotFound, exception.BooksNotFound, and exception.OrderException returned by the Duke’s Bookstore
application to errorpage.html.

See Mapping Errors to Error Screens for instructions on how to specify error pages using NetBeans IDE.

Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Legal Notices

Cookie Preferences | Ad Choices

You might also like