1. 程式人生 > >統計網站訪問量的小例子

統計網站訪問量的小例子

基於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>"); } }