Initializing Shared Resources Using a Listener
The Project Billboard
application uses two business
logic beans that must be available to process requests from all
users; in other words, available as application scope objects. You
may remember the NewsBean from Chapter 13. This bean is the repository for all news
items relating to projects, used as the source for the personalized
message list. The other business logic bean is called
EmployeeRegistryBean. It acts as an abstraction of
the database with employee information, containing methods for user
authentication and retrieving and saving employee information. The
EmployeeRegistryBean class is described in more
detail in Chapter 20.
Beans like this
typically
need to be initialized before they can be used. For instance, they
may need a reference to a database or some other external data source
and may create an initial information cache in memory to provide fast
access even to the first request for data. You can include code for
initialization of the shared resources in the servlet and JSP pages
that need them, but a more modular approach is to place all this code
in one place and let the other parts of the application work on the
assumption that the resources are already initialized and available.
An application lifecycle listener is a perfect tool for this type of
resource initialization. Example 19-5 shows a
listener suitable for the billboard application’s
needs. This type of listener implements the
javax.servlet.ServletContextListener ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access