日期:2014-05-17  浏览次数:21138 次

通过filter 统计用户访问次数
用filter来统计用户访问网页次数 遇到瓶 各位大神求指导

web.xml这么写的

 <filter>
   <filter-name>ViewCount</filter-name>
   <filter-class>filters.ViewCount</filter-class>
  </filter>
  <filter-mapping>
   <filter-name>ViewCount</filter-name>
   <url-pattern>*</url-pattern>
  </filter-mapping>

filter这么写的

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOExceptionServletException {
// TODO Auto-generated method stub
HttpServletRequest req1 = (HttpServletRequest) request;
HttpServletResponse resp1 = (HttpServletResponse) response;

Cookie cookies[] = req1.getCookies();
Cookie myCookie = null;

if(cookies != null){
for (int i = 0; i < cookies.length; i++) {
if(cookies[i].getName().equals("count")){
myCookie = cookies[i];
break;
}
}

}

if(myCookie != null){
int count = 0;
count = Integer.parseInt(myCookie.getValue());
count++;
myCookie.setValue(String.valueOf(count));
myCookie.setMaxAge(60*60*24*30);
resp1.addCookie(myCookie);

System.out.println(count);
}else{

Cookie cookie2  = new Cookie("count", "1");
cookie2.setMaxAge(60*60*24*30);
resp1.addCookie(cookie2);
}

req1.getRequestDispatcher("./index.jsp").forward(request, response);
}

然后到JSP页面就调不出myCookies了
求各位大神帮助 
filter.java.jsp

------解决方案--------------------
用 cookie 记录访问次数?