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