1 package boom.servlet; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletContext; 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 /** 12 * 案例 :统计访问量 13 * @author Administrator 14 * 15 */ 16 public class AServlet extends HttpServlet { 17 18 public void doGet(HttpServletRequest request, HttpServletResponse response) 19 throws ServletException, IOException { 20 /** 21 * 1.获取ServletContext对象 22 * 2.从ServletContext对象中获取count属性 23 * 3.如果存在:访问量+1,在保存; 24 * 4.如果不存在,就说明是第一次访问,就向ServletContext中报存count,值为1 25 */ 26 ServletContext app =this.getServletContext(); 27 Integer count = (Integer) app.getAttribute("count"); 28 if(count == null){ 29 app.setAttribute("count", 1); 30 }else{ 31 app.setAttribute("count", count+1); 32 } 33 /** 34 * 向浏览器输出:需要使用响应对象 35 */ 36 PrintWriter pw = response.getWriter(); 37 pw.print("<h1>" + count + "<h1>"); 38 } 39 40 }
day09[案例 访问量累加]
猜你喜欢
转载自www.cnblogs.com/cao-yin/p/9362486.html
今日推荐
周排行