zoukankan      html  css  js  c++  java
  • 使用过滤器实现网站访问计数器的功能

    创建过滤器类:

    public class CountFilter implements Filter {

        private int count;//访问数量

        //初始化

    @Override

        public void init (FilterConfig config) throws ServletException{

                      //获取初始化数量

                      String  param = filterConfig.getInitParameter(“count”);

                      count = Integer.valueOf(param);  

         }

    @Override

         public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOException ,ServletException{

                        //访问数据自增

                        count++;

                        //将ServletRequest转换为HttpServletRequest

                        HttpServletRequest req = (HttpServletRequest)request;

                        //获取ServletContext   (Application)

                         ServletContext context=req.getSession().getServletContext();

                        //将来访数量值放入到ServletContext中

                        context.setAttribute(“count”,count);

                        //向下传递过滤器

                        chain.doFilter(request, response);

          }

    @Override

         public void destory(){

                

         }

    }

    在web.xml中配置过滤器

    <filter>

         <filter-name>countfilter</filter>

          <filter-class>com.filter.CountFilter</filter-class>

    <!--设置初始化参数      -->

           <init-param>

                 <param-name>count</param-name>

                 <param-value>5000</param-value>

           </init-param>

    <filter>

    <filter-mapping>

        <filter-name>countfilter</filter>

        <url-pattern>/index.jsp</url-pattern>

    <filter-mapping>

  • 相关阅读:
    【QT】对话框打开图像并用QPixmap显示
    【QT】打开文件对话框,选择路径下文件
    狄拉克下采样
    Linux 安装JDK(jdk-8u121-linux-x64.tar.gz)
    Linux 命令安装bin文件
    Python3 tesseract加载chi_sim异常停止工作
    Python3 pip出现Fatal error in launcher: Unable to create process using '"'
    Python3 判断文件和文件夹是否存在、创建文件夹
    Python3 itchat实现微信定时发送群消息
    Python3 实现(wxpy)用微信自动定时给朋友定时推广
  • 原文地址:https://www.cnblogs.com/next-open/p/3536551.html
Copyright © 2011-2022 走看看