Just to warm up, let’s try a simple filter that merely prints a message to standard output whenever the associated servlet or JSP page is invoked. To accomplish this task, the filter has the following capabilities.
A class that implements the Filter interface. This class is called ReportFilter and is shown in Listing 9.2. The class provides empty bodies for the init and destroy methods.
Filtering behavior in the doFilter method. Each time a servlet or JSP page associated with this filter is invoked, the doFilter method generates a printout that lists the requesting host and the URL that was invoked. Since the getRequestURL method is in HttpServletRequest, not ServletRequest, I cast the ServletRequest object to HttpServletRequest.
A call to the doFilter method of the FilterChain. After printing the report, the filter calls the doFilter method of the FilterChain to invoke the servlet or JSP page (or the next filter in the chain if there was one).
Registration with the Web application home page and the servlet that displays the daily special. First, the filter element associates the name Reporter with the class moreservlets. filters.ReportFilter. Then, the filter-mapping element uses a url-pattern of /index.jsp to associate the filter with the home page. Finally, the filter-mapping element uses a servlet-name of TodaysSpecial to associate the filter with the daily special servlet (the name TodaysSpecial is declared in the servlet element). See Listing 9.3.
Disablement of the invoker servlet. First, I created a RedirectorServlet (Listing 9.6) that redirects all requests that it receives to the Web application home page. Next, I used the servlet and servlet-mapping elements (Listing 9.3) to specify that all URLs that begin with http://host/webAppPrefix/servlet/ should invoke the RedirectorServlet.
Given these settings, the filter is invoked each time a client requests the Web application home page (Listing 9.4, Figure 9-1) or the daily special servlet (Listing 9.5, Figure 9-2).
package moreservlets.filters; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; // For Date class /** Simple filter that prints a report on the standard output * each time an associated servlet or JSP page is accessed. */ public class ReportFilter implements Filter { public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { HttpServletRequest req = (HttpServletRequest)request; System.out.println(req.getRemoteHost() + " tried to access " + req.getRequestURL() + " on " + new Date() + "."); chain.doFilter(request,response); } public void init(FilterConfig config) throws ServletException { } public void destroy() {} } |
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <!-- Register the name "Reporter" for ReportFilter. --> <filter> <filter-name>Reporter</filter-name> <filter-class> moreservlets.filters.ReportFilter </filter-class> </filter> <!-- ... --> <!-- Apply the Reporter filter to home page. --> <filter-mapping> <filter-name>Reporter</filter-name> <url-pattern>/index.jsp</url-pattern> </filter-mapping> <!-- Also apply the Reporter filter to the servlet named "TodaysSpecial". --> <filter-mapping> <filter-name>Reporter</filter-name> <servlet-name>TodaysSpecial</servlet-name> </filter-mapping> <!-- ... --> <!-- Give a name to the Today's Special servlet so that filters can be applied to it. --> <servlet> <servlet-name>TodaysSpecial</servlet-name> <servlet-class> moreservlets.TodaysSpecialServlet </servlet-class> </servlet> <!-- ... --> <!-- Make /TodaysSpecial invoke the servlet named TodaysSpecial (i.e., moreservlets.TodaysSpecial). --> <servlet-mapping> <servlet-name>TodaysSpecial</servlet-name> <url-pattern>/TodaysSpecial</url-pattern> </servlet-mapping> <!-- Turn off invoker. Send requests to index.jsp. --> <servlet-mapping> <servlet-name>Redirector</servlet-name> <url-pattern>/servlet/*</url-pattern> </servlet-mapping> <!-- ... --> </web-app> |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>Filters 'R' Us</TITLE> <LINK REL=STYLESHEET HREF="filter-styles.css" TYPE="text/css"> </HEAD> <BODY> <CENTER> <TABLE BORDER=5> <TR><TH CLASS="TITLE">Filters 'R' Us</TABLE> <P> <TABLE> <TR> <TH><IMG SRC="images/air-filter.jpg" ALT="Air Filter"> <TH><IMG SRC="images/coffee-filter.gif" ALT="Coffee Filter"> <TH><IMG SRC="images/pump-filter.jpg" ALT="Pump Filter"> </TABLE> <H3>We specialize in the following:</H3> <UL> <LI>Air filters <LI>Coffee filters <LI>Pump filters <LI>Camera lens filters <LI>Image filters for Adobe Photoshop <LI>Web content filters <LI>Kalman filters <LI>Servlet and JSP filters </UL> Check out <A HREF="TodaysSpecial">Today's Special</A>. </CENTER> </BODY> </HTML> |
package moreservlets; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; /** Sample servlet used to test the simple filters. */ public class TodaysSpecialServlet extends HttpServlet { private String title, picture; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { updateSpecials(); response.setContentType("text/html"); PrintWriter out = response.getWriter(); String docType = "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 " + "Transitional//EN"> "; out.println (docType + "<HTML> " + "<HEAD><TITLE>Today's Special</TITLE></HEAD> " + "<BODY BGCOLOR="WHITE"> " + "<CENTER> " + "<H1>Today's Special: " + title + "s!</H1> " + "<IMG SRC="images/" + picture + "" " + " ALT="" + title + ""> " + "<BR CLEAR="ALL"> " + "Special deal: for only twice the price, you can " + "<I>buy one, get one free!</I>. " + "</BODY></HTML>"); } // Rotate among the three available filter images. private void updateSpecials() { double num = Math.random(); if (num < 0.333) { title = "Air Filter"; picture = "air-filter.jpg"; } else if (num < 0.666) { title = "Coffee Filter"; picture = "coffee-filter.gif"; } else { title = "Pump Filter"; picture = "pump-filter.jpg"; } } } |
package moreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/** 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.
*/
public class RedirectorServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.sendRedirect(request.getContextPath());
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
} |
18.191.234.150