CHAPTER 11 (Web hosting script) ADVANCED FILTERING TECHNIQUES 449 the

CHAPTER 11 ADVANCED FILTERING TECHNIQUES 449 the same time. Following the filter life cycle, however, there s a natural place to set the value of your initialization parameters. After an instance of a filter has been created, and before the very first doFilter() is called, the container calls the init() method to set the FilterConfig object for the filter. This is a natural point at which to perform any initialization required, and it s conceptually equivalent to the init() method of a servlet. In this case, you ll take advantage of it by setting your instance variables with values from the filter definition. Listing 11-10 shows the init() method for the StopGamesFilter. The container always calls init() in a single thread when it sets up the filter instance. In the init() method, the filterConfig.getInitParameter()method reads the web.xml file for initialization parameters. The init() method in Listing 11-10 replaces the init() method in Listing 11-9. Listing 11-10. init() (To Be Added to StopGamesFilter.java) public void init(FilterConfig arg0) throws ServletException { String tpString; filterConfig = arg0; if ((tpString = filterConfig.getInitParameter(”starthour”)) != null) starthour = Integer.parseInt(tpString, 10); if ((tpString = filterConfig.getInitParameter(”stophour”)) != null) stophour = Integer.parseInt(tpString, 10); } Installing and Configuring the StopGamesFilter To install and configure the filter, first make sure all other and elements are removed or commented out of the web.xml file. Then add the following entries Stop Games Filter com.apress.projsp.StopGamesFilter
starthour 8

stophour 9
Stop Games Filter /games/*
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.

Leave a Reply