|
Source Code Archive
More Servlets and JavaServer Pages
Chapter 10: The Application Events Framework
To view source code for other chapters in this book, mouse over the table of contents bar on the left.
Source Code from Chapter
Note: right-click or shift-click on the links to download the associated file.
-
InitialCompanyNameListener.java.
Listener that looks up the name of the company when
the Web application is first loaded. Stores this
name in the
companyName servlet context attribute.
Various servlets and JSP pages will extract it
from that location.
Also looks up and stores the former company name and
stores it in the formerCompanyName attribute.
- web.xml.
Deployment descriptor needed by the servlets, JSP pages,
and listeners of this chapter.
- index.jsp.
Home page. Uses the predefined
application
variable (i.e., the servlet context) to access the companyName
and formerCompanyName attributes. Requires the
events-styles.css
style sheet.
- events-styles.css.
Style sheet used by most JSP pages in this chapter.
- company-info.jsp.
Company information page. Also uses the predefined
application
variable (i.e., the servlet context) to access the companyName
and formerCompanyName attributes.
-
ChangedCompanyNameListener.java. Listener that monitors changes in the company
name (which is stored in the
companyName attribute
of the servlet context).
-
change-company-name.jsp. A JSP page containing a form
that displays the current company
name, lets users enter a new name, and submits the new name to the
ChangeCompanyName servlet. Since changing the company name is a
privileged operation, access to the form and the servlet is restricted.
-
ChangeCompanyName.java. Servlet that changes the company name. The
web.xml
file specifies that only authenticated users in the
ceo role can access the servlet. A servlet context
attribute listener updates the former company name
when this servlet (or any other program) changes
the current company name.
- login.jsp.
Page used for not-yet-authenticated users.
- login-error.jsp.
Page used for failed authentication attempts.
- tomcat-users.xml.
Tomcat-specific password file.
-
CompanyNameTag.java. Custom tag to display the company name.
The
InitialCompanyNameListener class has static
methods that permit access to the current and former
company names. But, using these methods in JSP requires
explicit Java code, and creating beans that provided
the information would have yielded a cumbersome result.
So, we simply move the code into a custom tag.
-
company-name-taglib.tld. TLD file for tag library to print out the ever-changing current
and former company names (which are monitored by event
listeners).
- index2.jsp.
Company home page reworked with the new custom tags.
- company-info2.jsp.
Company information page reworked with the new custom tags.
-
SessionCounter.java. Listener that keeps track of the number of sessions
that the Web application is currently using and has
ever used in its life cycle.
- session-counts.jsp.
JSP page that displays the session counts.
- make-sessions.html.
Page that, when cookies are disabled, results in 16 new sessions
each time it is accessed. Assumes that the server uses cookies,
not URL rewriting, for session tracking. Requires
test.jsp and the
ColorUtils class.
- test.jsp. Small page
that uses the
ColorUtils class to display a random background color.
-
ColorUtils.java. Small utility to generate random HTML color names.
-
YachtWatcher.java. Listener that keeps track of yacht purchases by monitoring
the
orderedItem and purchasedItem session attributes.
-
OrderHandlingServlet.java. Servlet that handles
submissions from the order form. If the
user selects the "Reserve Order" button, the selected item
is put into the orderedItem attribute. If the user selects
the "Cancel Order" button, the orderedItem attribute is
deleted. If the user selects the "Purchase Item" button,
the selected item is put into the
purchasedItem attribute.
- orders.html.
HTML form that sends data to the
OrderHandlingServlet.
-
DailySpecialRegistrar.java. Listener that records how to detect orders
of the daily special. It reads a list of attribute
names from an init parameter: these correspond to
session attributes that are used to record orders.
It also reads a list of item names: these correspond
to the names of the daily specials. Other listeners
will watch to see if any daily special names appear
as values of attributes that are hereby designated
to refer to orders.
-
DailySpecialWatcher.java. Listener that keeps track of orders of the
current daily special.
- track-daily-specials.jsp.
JSP page that prints the current order count.
-
ChangedDailySpecialListener.java.
Listener that monitors changes to the names
of the daily specials (which are stored in
the
daily-special-item-names attribute of
the servlet context). If the names change, the
listener resets the running count of the number
of daily specials being ordered.
-
change-daily-specials.jsp. JSP page that
displays the current daily specials in a text
area. It lets the user change the values and
send them to a
servlet that
records the changes in the servlet context.
The JSP page is in the admin directory
and the servlet is assigned a URL beginning with /admin
(see the web.xml
file), so security restrictions apply.
-
ChangeDailySpecial.java.
Servlet that changes the daily specials. The
web.xml
file specifies that only authenticated users in the
ceo role can access the servlet. A servlet context
attribute listener resets the count of daily special
orders when this servlet (or any other program) changes
the daily specials.
- events.war.
The entire
events application bundled in
a WAR (Web ARchive) file.
More Information
-
J2EE training courses from the author. Available on-site at your
company or at public venues.
- Free online version of
Core Servlets & JavaServer Pages.
Complete text of book in PDF.
-
Home page for Core Servlets and JavaServer Pages 2nd Edition (Vol 1).
Includes the table of contents, index, sample chapters, source code archive, etc.
-
Home Page for More Servlets and JavaServer Pages.
Includes the table of contents, index, source code archive, etc.
-
Free online version of More Servlets and JavaServer Pages.
- Servlet and JSP Programming Resources.
Download sites, servers, IDEs, tutorials, books, job listings, and more.
-
Tutorials and lecture notes on servlets, JSP, Struts, JSF, and Java 5.
PDF version free for all. PowerPoint version free for qualified university instructors.
- Servlet and JSP
programming resources. Specs, documentation, servers, IDEs,
hosting providers, books, training, etc.
- Java Programming Resources.
General resources on Java technology. Compilers, editors, IDEs, tutorials, books,
user forums, and more.
|