天天看點

JSP簡單實作統計網頁通路次數

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

%>