访问量统计

时间:2019-06-11
本文章向大家介绍访问量统计,主要包括访问量统计使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
public class Aservlet extends HttpServlet {
     
     public void doGet(HttpServletRequest request,  HttpServletResponse response)
                throws ServletException, IOException {
           //获取ServletContext对象
           //从对象中获取名为count的属性
           //如果存在,给访问量+1,然后再保存回去
           //如果不存在,说明时第一次访问,向ServletContext中保存count的属性,值为1
           ServletContext app=this.getServletContext();
           Integer count=(Integer) app.getAttribute("count");
           if(count==null){
                app.setAttribute("count", 1);
           }else{
                app.setAttribute("count", count+1);
           }
           /**
            * 向浏览器输出,需要使用响应对象
            */
           PrintWriter pw=response.getWriter();
           pw.print(count);
     }
}
然后在浏览器访问该servlet,每访问一次会加一。

原文地址:https://www.cnblogs.com/fantongxue/p/11002081.html