JSP簡單實現統計網頁訪問次數
需求:統計網頁的訪問次數
核心思想:利用application對象,將訪問次數的信息放入application對象中,每次訪問就+1。這里利用了application對象每次只有當應用關閉才被銷毀的特性。
核心代碼如下:
<% Object obj =application.getAttribute("counter"); if(obj==null){ application.setAttribute("counter", new Integer(1)); out.print("該頁面已被訪問1次!"); }else{ int count=Integer.parseInt(obj.toString()); count++; out.print("該頁面已被訪問了"+count+"次!"); application.setAttribute("counter", count); } %>
