1 package geekblog.filters;
2
3
4 import java.text.*;
5 import java.util.*;
6 import javax.servlet.*;
7 import javax.servlet.http.*;
8 import geekblog.*;
9
10
11
14 public class HitCountFilter
15 implements Filter
16 {
17 private FilterConfig config;
18 private String attributeName;
19 private Date refDate;
20
21 private static final SimpleDateFormat datef = new SimpleDateFormat("yyyyMMdd");
22
23 public void init(FilterConfig config)
24 {
25 this.config = config;
26
27 attributeName = config.getInitParameter("attributeName");
28 refDate = new Date();
29
30 config.getServletContext().setAttribute(attributeName, new Integer(0));
31 }
32 public void destroy()
33 {
34 }
35
36
37 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
38 throws ServletException, java.io.IOException
39 {
40 ServletContext context = config.getServletContext();
41
42 Date todayDate = new Date();
43 if (!datef.format(todayDate).equals(datef.format(refDate)))
44 {
45 synchronized(this)
46 {
47 refDate = todayDate;
48 }
49 synchronized(context)
50 {
51 context.setAttribute(attributeName, new Integer(1));
52 }
53 }
54 else
55 {
56 synchronized(context)
57 {
58 Integer hitCount = (Integer)context.getAttribute(attributeName);
59 context.setAttribute(attributeName, new Integer(hitCount.intValue() + 1));
60 }
61 }
62
63 chain.doFilter(request, response);
64 }
65 }
66