小程式之統計網頁訪問人數
阿新 • • 發佈:2019-01-10
public class AServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1 獲得Application域中存放的統計數字 Integer count = (Integer) getServletContext().getAttribute("count"); //2 判斷是否獲得到統計數字 if(count == null){ //沒獲得到=> 將數字初始化為1 count = 1; }else{ //獲得到了=> 將數字加1 count += 1; } //3 輸出,放回到Application域中 response.getWriter().write("you are the "+ count+" vistors"); getServletContext().setAttribute("count",count); } }