Java: 网站访问统计代码

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        /*
         * 1. 获取ServletContext对象
         * 2. 从ServletContext对象中获取名为count的属性
         *   3. 如果存在:给访问量加1,然后再保存回去;
         *   4. 如果不存在:说明是第一次访问,向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 + "

"); } }

你可能感兴趣的:(Java: 网站访问统计代码)