448 CHAPTER 11 (Web hosting account) ADVANCED FILTERING TECHNIQUES out.println(”");

448 CHAPTER 11 ADVANCED FILTERING TECHNIQUES out.println(”“); out.println(”

Sorry, game playing is not ” + “allowed at this time!

“); out.println(”“); out.flush(); filterConfig.getServletContext() .log(”Access to game page denied”); return; } filterConfig.getServletContext() .log(”Access to game page granted”); chain.doFilter(request, response); filterConfig.getServletContext() .log(”Getting out of StopGamesFilter”); } public void init(FilterConfig arg0) throws ServletException { this.filterConfig = arg0; } public void destroy() {} } The class has two instance variables to hold the default hours of operation, which allows game playing all the time. These parameters are used if the filter is configured without initial parameters, or if the access to the initial parameters failed. It s always a good idea to set some usable default value for the filter. In the doFilter()method, the class obtains the current hour, which depends on a 24-hour clock to make things simple. The class also assumes that the start and stop hours don t cross the 12 a.m. boundary to keep the logic simple. Next, the class makes a log entry to show the various values, which is useful for auditing or debugging purposes. If the incoming request arrives outside of the allowed time range, doFilter() simply generates the content of the response. The generated content lets the user know that access to the game resource page is disallowed. If the request arrives within the allowed hours, the filter lets the request through to access the game resource by passing the request and response to chain.doFilter(). Thread-Safety Considerations One question that you may have is, Where do I access the initialization parameters? The answer is, generally not in the doFilter() method. This goes back to our discussion in the previous chapter about thread-safe programming. The starthour and stophour variables are instance-scoped variables. Modifying the value of instance-scoped variables in doFilter() requires careful synchronization, because doFilter() will be accessed in multiple threads at
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.

Leave a Reply