网站访问量统计小案例

1.创建javaweb项目
2.在src目录下创建servlet文件,其继承HttpServlet
3.重写doGet()方法
4.使用浏览器访问

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	/*
	 *1.获取ServletContext
	 *2.通过ServletContext获取属性count
	 *3.判断count是否存在,如果不存在,则设置count属性且设置值为1,存在则+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("<h1>"+count+"</h1>");
	}
发布了25 篇原创文章 · 获赞 5 · 访问量 1886

猜你喜欢

转载自blog.csdn.net/DoMyBestintheworld/article/details/104085206