侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

JSP实现网页访问统计

Java  /  管理员 发布于 7年前   162

最近学习Jave EE 中的jsp网页开发,需要实现网页访问量的统计,刚开始不知道如何实现,后来问了一下老师,老师是这样回答我的:要实现网页访问的统计,你可以利用application对象来实现,不能用seesion对象,因为session是属于同一个会话的,关掉浏览器数据就没有了,而application是在同一浏览器下的,只要是同一个浏览器,将数据保存在applicaiton对象中,这样就可以保证数据的不变性。其实这些我都懂,我只是不知道如何在jsp用代码实现。后来我只能上网看看有没有具体的解决方案,搜了很久,没有我想要的答案,我想要实现的只是简单的统计,没有实现更加复杂的功能。后来还是在CSDN这里找到了答案,在这里简单总结一下实现网页访问统计的几种方法:
1. 利用application对象进行统计,得到的效果是每进入一次该网页就统计一次。但效果不怎么好,因为一般统计网页访问量,刷新是不算进统计里的,这里就是这种缺点。
具体实现是:

<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%> <html>  <head>  <title>java 计数器程序</title>  </head>  <body>  <%  if (application.getAttribute("count") == null) {   application.setAttribute("count", new Integer(0));  }  Integer count = (Integer) application.getAttribute("count");  application    .setAttribute("count", new Integer(count.intValue() + 1));  count = (Integer) application.getAttribute("count");  %>   <center>这是第<%=count.intValue()%>个访问者</center>  </body>  </html> 

 2.为了解决上面的问题,有了另一种方法,就是同时利用application对象和session对象来统计,这种方法的原理是从打开浏览器到关闭浏览器算是访问一次,刷新、返回等操作不算做一次访问。但还是有缺陷,当jsp服务器从新启动时,数据也被清零了。
下面还是具体实现:

<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%> <html>  <head>  <title>java 计数器程序</title>  </head>  <body>  <%  int n = 0; String counter = (String)application.getAttribute("counter");  if(counter != null){   n = Integer.parseInt(counter);  }  if(session.isNew())   ++n;  %>   <center>这是第<%out.print(n);%>个访问者</center>   <%   counter = String.valueOf(n);   application.setAttribute("counter", counter);    %>  </body>  </html> 

3. 第三种方法是将统计数据存储在本地的文件当中,比如存储在一个txt文件当中。
这是为了解决重启服务器之后数据不用担心会丢失。
创建一个类:JSPCount

import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter;   public class JSPCount {  //写入文件的方法  public static void write2File(String filename, long count){   try{    PrintWriter out = new PrintWriter(new FileWriter(filename));    out.println(count);    out.close();   } catch (IOException e) {    // TODO: handle exception    e.printStackTrace();   }  }    //读文件的方法  public static long readFromFile(String filename){   File file = new File(filename);   long count = 0;   if(!file.exists()){    try {     file.createNewFile();    } catch (IOException e) {     // TODO Auto-generated catch block     e.printStackTrace();    }    write2File(filename, 0);   }   try{    BufferedReader in = new BufferedReader(new FileReader(file));    try{     count = Long.parseLong(in.readLine());    }    catch (NumberFormatException e) {     // TODO: handle exception     e.printStackTrace();    } catch (IOException e) {     // TODO Auto-generated catch block     e.printStackTrace();    }   } catch (FileNotFoundException e) {    // TODO: handle exception    e.printStackTrace();   }   return count;  } } 

 在WebRoot目录下建jsp文件:count.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%> <%@ page import="org.wwj.count.JSPCount" %> <html>  <head>  <title>java 计数器程序</title>  </head>  <body>  <%  JSPCount CountFileHandler = new JSPCount();  //读取文件  long count = CountFileHandler.readFromFile(request.getRealPath("/") + "count.txt");  count = count + 1; //修改记录 +1  out.print(count); //显示数据  //更新文件内容。  CountFileHandler.write2File(request.getRealPath("/") + "count.txt", count);    %>  </body>  </html> 

程序运行之后会在tomcat下的webapps目录下的对应的web项目生成一个count.txt文本文件

4.第四种方法,只是保存了访问的统计数据罢了,但没有保证刷新页面的时候不会自增,这样还是不好。当然总会有解决的办法的,一般的解决方案就是结合各种方案的优点。下面是由session对象+application对象+txt文本来实现网站的访问统计。

import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter;  import javax.servlet.http.HttpServlet;  public class Counter extends HttpServlet{  //写入文件的方法  public static void write2File(String filename, long count){   try{    PrintWriter out = new PrintWriter(new FileWriter(filename));    out.println(count);    out.close();   } catch (IOException e) {    // TODO: handle exception    e.printStackTrace();   }  }    //读文件的方法  public static long readFromFile(String filename){   File file = new File(filename);   long count = 0;   if(!file.exists()){    try {     file.createNewFile();    } catch (IOException e) {     // TODO Auto-generated catch block     e.printStackTrace();    }    write2File(filename, 0);   }   try{    BufferedReader in = new BufferedReader(new FileReader(file));    try{     count = Long.parseLong(in.readLine());    }    catch (NumberFormatException e) {     // TODO: handle exception     e.printStackTrace();    } catch (IOException e) {     // TODO Auto-generated catch block     e.printStackTrace();    }   } catch (FileNotFoundException e) {    // TODO: handle exception    e.printStackTrace();   }   return count;  } } 

jsp文件代码:

<%@page import="org.servlet.count.Counter"%> <%@ page language="java" import="java.util.*" pageEncoding="GB2312"%> <html>  <head>   <title>java 计数器程序</title>  </head>  <body>  <%  Counter CountFileHandler = new Counter();  long count = 0;  if(application.getAttribute("count") == null){   count = CountFileHandler.readFromFile(request.getRealPath("/") + "count.txt");   application.setAttribute("count", new Long(count));  }   count = (Long)application.getAttribute("count");  if(session.isNew()){   count++;   application.setAttribute("count", count);   //更新文件目录   CountFileHandler.write2File(request.getRealPath("/") + "count.txt",count);   }  %>  访问人数:<%=count %>   </body> </html> 

以上四种方法,是每一次改进才得到的方法,如果要实现网站访问统计,当然最后一种是最好的,知识不是一步登天,需要在问题上不断改进,获得最终的解决方案,当然最后一种不一定是最好的,实现策略上,如果可以利用数据库也是可以的,但我认为每次访问网站都要读和写数据库,这样效率就降低了。


  • 上一条:
    JSP中图片的上传与显示方法实例详解
    下一条:
    jsp实现简单验证码的方法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在java中实现的脱敏工具类代码示例分享(0个评论)
    • zookeeper安装流程步骤(0个评论)
    • 在java中你背的“八股文”可能已经过时了(2个评论)
    • 在php8.0+版本中使用属性来增加值代码示例(3个评论)
    • java 正则表达式基础,实例学习资料收集大全 原创(0个评论)
    • 近期文章
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-11
    • 2018-03
    • 2020-03
    • 2023-05
    • 2023-11
    • 2024-01
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客