Servlet chaining can be performed two ways, using the include() and forward() methods of the RequestDispatcher interface.
A2:
To write a filter for a servlet, you must override the doFilter(), getFilterConfig(), and setFilterConfig() methods of the Filter interface. The actual functionality of the filter is written in the doFilter() method. After you write and compile the filter, it must be associated with a servlet. You do so in the web.xml deployment descriptor file.
A3:
ServletContext events such as creation, termination, adding an attribute, removing an attribute, and replacing an attribute can be intercepted by a listener of a ServletContext.
A4:
Creation, termination, adding an attribute, removing an attribute, and replacing an attribute are events of the HttpSession object that can be intercepted using a listener.
A5:
Listeners must always be defined before servlets in the deployment descriptor file.
Exercises
A1:
Add the following method to the filter DemonstrateFilters.java to perform the logging functionality:
Note that static HTML cannot show any information retrieved from this request object. When might you require a servlet to forward a request to a static HTML file? For example, an online shopping Web site for white papers might require first-time users to register. After the user has registered and the servlet has saved the user information in a database, the user would be shown a static HTML page containing a list of links to all the existing white papers.