我采用的是jsp网页,但是不管采用什么语言,原理是一样的。

第一种,单页面统计。就是说,只要点击这个页面就会统计一次。

<body>
<%!//在这种标记中定义的变量为全局变量
    int count=0;
    synchronized void count(){
      count++;
    }
%>
<% count(); out.println("这是第"+count+"个访问者!"); %>
</body>

第二种,是利用jsp的内置对象application进行统计。这个程序结果运行分析,也是访问一次页面统计一次。感觉还是不够好。真正满意的是浏览器打开网页,到关闭网页算一次,这样统计比较实际。

<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>

第三种,利用jsp的application和session进行统计。它的原理是,访问者打开浏览器到关闭浏览器算一次访问。每次打开首页,创建一个session,这个session直到浏览器关闭才失效。但总体来说,比前两种要好。但是有一个一个缺陷,那就是当jsp服务器重启时,累计的统计数就清零了。

<%
int n = 0;
String count = (String) application.getAttribute("counter");
if (counter != null)
n = Integer.parseInt(counter);
if (session.isNew())
     ++n;
out.print("你是第" + n + "位访客");
counter = String.valueOf(n);
application.setAttribute("counter", counter);
%>

第四种,就是保存到txt文本中,那样重启服务器也不会丢失了。

public class counter {

    public static void writeFile(String filename, long count) {

        try {
PrintWriter out = newPrintWriter(new FileWriter(filename));
out.println(count);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
} public static long readFile(String filename) {
File f = new File(filename);
long count = 0;
if (!f.exists()) {
writeFile(filename, 0);
}
try {
BufferedReader in = newBufferedReader(newFileReader(f));
try {
count = Long.parseLong(in.readLine());
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return count;
}
}

下面是你要计数的jsp页面,在里面添上以下内容就ok了

<%@pageimport="com.benb.servlet.counter"%>
<%
counterCountFileHandler=newcounter();//创建对象
longcount=CountFileHandler.readFile(request.getRealPath("/")+"count.txt");
//读取文件获取数据赋给count
count=count+1;//修改记录,数据加1
out.println(count);//显示记录数
CountFileHandler.writeFile(request.getRealPath("/")+"count.txt",count);//更新文件记录
%>
但是还是不是很好,也是每次访问首页就计数一次。怎么样百分百满意呢?
 
最后一种完美解决方法,session和application加文本保存结合就完美了,不管重启服务器,还是能百分百记录所有的访问记录。
写一个severlet类似前面,就是long类型改成int类型。
public class Counter extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public Counter() {
super();
} public static void writeFile(String filename, int count) { try {
PrintWriter out = new PrintWriter(new FileWriter(filename));
out.println(count);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
} public static int readFile(String filename) {
File f = new File(filename);
int count = 0;
if (!f.exists()) {
writeFile(filename, 0);
}
try {
BufferedReader in = new BufferedReader(new FileReader(f));
try {
count = Integer.parseInt(in.readLine());
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return count;
} public void init() throws ServletException {
// Put your code here
} }

页面编码如下

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.tozhan.cn.GetDataDB" %>
<%@ page import="com.tozhan.cn.news.New" %>
<%@ page import="com.tozhan.cn.Counter" %>
<%
  Counter CountFileHandler=new Counter();//创建对象
  int count=0;
  if(application.getAttribute("count")==null){
    count=CountFileHandler.readFile(request.getRealPath("/")+"count.txt"); //读取文件获取数据赋给count
    application.setAttribute("count",new Integer(count));
  }
  count=(Integer)application.getAttribute("count");
  if(session.isNew()) ++count;
  application.setAttribute("count",count);
  CountFileHandler.writeFile(request.getRealPath("/")+"count.txt",count);//更新文件记录
%>
<p>我们的友谊海枯石烂! 你是第&nbsp;<%=count %>&nbsp;位访客</p>

最新文章

  1. EntityFramework之Log(五)
  2. 配置文件的生成,关于“make menuconfig”
  3. mysql 主主复制的配置流程
  4. hdu 3038 How Many Answers Are Wrong ( 带 权 并 查 集 )
  5. SQL Server 中的存储过程
  6. Cisco Anyconnect Secure Mobility Client
  7. 海蜘蛛网络科技官方网站 :: 做最好的中文软路由 :: 软件路由器 :: 软路由 :: 软件路由 :: RouterOs
  8. php 便利数组方法
  9. ios 添加PCH文件
  10. Python简要学习笔记
  11. c#通过反射获取自定义属性
  12. 宋宝华:关于Ftrace的一个完整案例【转】
  13. 微信小程序web-view实例
  14. java中package指什么
  15. jQuery 选择器 筛选器 样式操作 文本操作 属性操作 文档处理 事件 动画效果 插件 each、data、Ajax
  16. python list和numpy互换
  17. httpclient http状态管理
  18. ubuntu16.04-caffe安装过程详解-草稿
  19. 5.log4j报错
  20. 不错的SDL源码分析

热门文章

  1. c# 跨线程访问窗体UI
  2. python index()函数
  3. “全栈2019”Java第六十四章:接口与静态方法详解
  4. 解决winform datagridview的ClearSelection无效问题
  5. linux安装报错之:ifconfig command not found解决
  6. 极光开发者沙龙 JIGUANG MEETUP —— 移动应用性能优化实践
  7. UIResponder笔记
  8. software installing
  9. POJ3074 Sudoku
  10. 使用pageHelper遇到的问题