日期:2014-05-17 浏览次数:21183 次
<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>
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// 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);
}