4. Registration with the Web application home page; definition
of initialization parameters. First, the filter element associates
the name LateAccessFilter with the class coreservlets.
filters.LateAccessFilter. The filter element also includes
two init-param subelements: one that defines the startTime
parameter and another that defines endTime. Because the people
that will be accessing the filtersRus home page are programmers, an
abnormal range is considered to be between 2:00 a.m. and 10:00 a.m.
Finally, the filter-mapping element uses a url-pattern of
/index.jsp to associate the filter with the Web application home
page. See Listing 5.11.
5. Disablement of the invoker servlet. This operation is shown in
Section Section 5.2 (Example: A Reporting Filter) and is not repeated
here.
Chapter 5 ?– Servlet and JSP Filters 224
After the Web application is deployed on an external server and the logging filter
is attached, a client request for the Web application home page results in an entry in
the log file like ???WARNING: hacker6.filtersrus.com accessed http://www.filtersrus.
com/filters/index.jsp on Oct 30, 2006 9:22:09 AM.???
Listing 5.10 LateAccessFilter.java
package coreservlets.filters;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
/** Filter that keeps track of accesses that occur
* at unusual hours.
*/
public class LateAccessFilter implements Filter {
private FilterConfig config;
private ServletContext context;
private int startTime, endTime;
private DateFormat formatter;
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws ServletException, IOException {
HttpServletRequest req = (HttpServletRequest)request;
GregorianCalendar calendar = new GregorianCalendar();
int currentTime = calendar.
Pages:
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275