table of contents

Source Code Archive

More Servlets and JavaServer Pages

Chapter 9: Servlet and JSP Filters

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.

  • Simple filter that prints a report on the standard output each time an associated servlet or JSP page is accessed.
  • web.xml. Deployment descriptor needed by all filters and filter-related examples in this Chapter.
  • index.jsp. Top-level page for Requires the filter-styles.css style sheet and images in the images/ directory.
  • filter-styles.css. Style sheet used by most JSP pages in this Chapter.
  • Sample servlet used to test the simple filters.
  • Servlet that simply redirects users to the Web application home page. Registered with the default servlet URL to prevent clients from using http://host/webAppPrefix/servlet/ServletName to bypass filters or security settings that are associated with custom URLs.
  • Filter that prints a report in the log file whenever the associated servlets or JSP pages are accessed.
  • Filter that keeps track of accesses that occur at unusual hours.
  • Filter that refuses access to anyone connecting directly from or following a link from a banned site.
  • linker.html. A very simple page that contains little but a link to the daily special servlet. When that page is hosted on a normal site, the link results in the expected output. But, when the page that contains the link is hosted on a banned site, the link results only in a warning page--access to the real servlet is blocked.
  • A response wrapper that takes everything the client would normally output and saves it in one big character array.
  • Filter that replaces all occurrences of a given string with a replacement. This is an abstract class: you must override the getTargetString and getReplacementString methods in a subclass. The first of these methods specifies the string in the response that should be replaced. The second of these specifies the string that should replace each occurrence of the target string.
  • Small utility to assist with response wrappers that return strings.
  • A filter that replaces all occurrences of with Subclass of the ReplaceFilter class.
  • page1.jsp. A page that promotes the site name. Target of the ReplaceSiteNameFilter.
  • page2.jsp. A page that promotes the site name. Identical to page1.jsp, but this page is the target of the ReplaceSiteNameFilter.
  • Filter that compresses output with gzip (assuming that browser supports gzip).
  • Servlet with long output. Used to test the effect of the compression filter.
  • filters.war. The entire filters application bundled in a WAR (Web ARchive) file.

URLs Referenced in Chapter

More Information


JSF (JavaServer Faces)

Servlets & JSP
Ajax, GWT, & JavaScript

Spring, Hibernate, & JPA