码迷,mamicode.com
首页 > 其他好文 > 详细

过滤器统计访问次数

时间:2015-12-13 21:44:16      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

index.jsp

<h1>欢迎来到我主页,此页面已被访问<%=application.getAttribute("count")%>次!</h1>

web.xml

    <filter>
        <filter-name>countFilter</filter-name>
        <filter-class>com.lyq.CountFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>countFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

CountFilter.java

@Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req=(HttpServletRequest) request;
        HttpServletResponse res=(HttpServletResponse) response;
        ServletContext application=req.getSession().getServletContext();
        if (application.getAttribute("count")!=null) {
            Long count=(Long) application.getAttribute("count");
            application.setAttribute("count", ++count);
        } else {
            application.setAttribute("count", new Long(1));
        }
        chain.doFilter(req, res);
    }

P84页

 

过滤器统计访问次数

标签:

原文地址:http://www.cnblogs.com/ganjun/p/5043468.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!