简单的网站计数器(jsp)

来源:互联网 发布:钉钉打卡机mac地址 编辑:程序博客网 时间:2024/04/30 05:52

<%@ page contentType="text/html;charset=GB2312" %>
<HTML>
<head>
<title>网站计数器</title>
</head>
<BODY>
    <%! 
     synchronized void countPeople()//串行化计数函数
        {  ServletContext  application=getServletContext();
            //在java代码里取得application对象
           Integer number=(Integer)application.getAttribute("Count");
           if(number==null)  //如果是第1个访问本站
              { number=new Integer(1);
                application.setAttribute("Count",number);
              }
           else
              { number=new Integer(number.intValue()+1);
                application.setAttribute("Count",number);
              }
        }
    %>
    <% if(session.isNew())//如果是一个新的会话
          countPeople();
       Integer yourNumber=(Integer)application.getAttribute("Count");
    %>
欢迎访问本站,您是第
    <%=yourNumber%>
个访问用户。
</BODY>
</HTML>