统计网站访问量的小例子

基于servlet的统计网站访问量的小栗子:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class Aservlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        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("<h1>"+count+"<h1>");
    }

}

猜你喜欢

转载自blog.csdn.net/system_obj/article/details/49032677