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

%>
原文地址:https://www.cnblogs.com/xtuxiongda/p/8996339.html