关于网站统计或投票系统的多种方法---JSP

来源:互联网 发布:电信光纤网络 编辑:程序博客网 时间:2024/06/05 14:21

1、采用数据库存储数据

适用于大型网站,是最常用的技术。

2、利用XML或文本文档存储统计数据

XML可作为小型数据库使用。该方法与方法一类似,区别在于一个是将数据存储于服务器,一个是存储与客户端。

3、利用servlet的servletcontext声明一个context对象,存储数据

或利用jsp的application对象

此种方法,在服务器重新启动后重新计数。

4、声明全局变量,存储数据

最简单的办法

注意事项:

1.考虑并发:利用synchronized实现同步

2.第三种方法中存储的数据为Object类型,若统计数据没有,则null,应转换为0显示给客户端。

附:第三种方法部分代码

Integer count=null;

ServletContext context=getServletContext();

      synchronized(context)
       {
       count= (Integer) context.getAttribute("renwu1");
                 if (null == count)
           {
                  count= new Integer(1);
           }
           else
           {
            count= new Integer(count.intValue() + 1);
              }
               context.setAttribute("renwu1", count);
       }