[java]代码库
package pm_cn.itcast.context;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CountServlet extends HttpServlet {
@Override
public void init() throws ServletException {
// 将初始化的 值 放进到 servletContext 对象中
getServletContext().setAttribute("times", 0);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int times = (Integer) getServletContext().getAttribute("times");
// 取出来 +1
times++;
// 然后 再 存进去
getServletContext().setAttribute("times", times);
System.out.println(" 本网站 本 访问了 一次 .............");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}