我采用的是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>

JSP中实现网页访问统计的方法【转】的更多相关文章

  1. PHP 简易网页访问统计

    传统的网页访问统计,已经有很多,如 51la.百度统计.站长统计 一般都需要引用JS,在你的网页内嵌入JS,这个操作存在风险,并且不可控. 可以考虑使用 [img src.css src.link h ...

  2. jsp中四种传递参数的方法

    jsp中四种传递参数的方法如下: 1.form表单 2.request.setAttribute();和request.getAttribute(); 3.超链接:<a herf="i ...

  3. wuzhicms访问统计实现方法

    实现目标:程序实现了对整站页面pv的统计文件的位置:coreframe/app/content/pv.php代码预览: /** * 总站访问次数统计 */ defined('IN_WZ') or ex ...

  4. (转)JSP中四种传递参数的方法:

    1.form表单 2.request.setAttribute();和request.getAttribute(); 3.超链接:<a herf="index.jsp"?a= ...

  5. Angular 项目打包之后,部署到springboot项目中,刷新访问404解决方法

    解决方法1: app.module.ts文件添加两行代码: import { LocationStrategy, HashLocationStrategy } from '@angular/commo ...

  6. 在Silverlight中打开网页的几种方法

    HtmlPage.PopupWindow HtmlPopupWindowOptions option = new HtmlPopupWindowOptions(); option.Directorie ...

  7. Android中使用adb访问SQLite的方法

    (1)打开命令提示符,输入:adb,按回车,如果得到下面一大堆命令说明(如图 1),表示adb的配置是成功的,如果提示"不是内部或外部命令,也不是可运行的程序或批处理文件",那么需 ...

  8. WPF中打开网页的两种方法

    1.浏览器打开 Process proc = new System.Diagnostics.Process(); proc.StartInfo.FileName = "http://www. ...

  9. 对jsp中的js进行调试的方法

    在js中 输入debugger 就可以了

随机推荐

  1. 纯分享scp协议如何工作

    scp协议是什么, wiki上说: Secure copy or SCP is a means of securely transferring computer files between a lo ...

  2. ResorceGovernor--基础和Demo

    资源调控器分为三部分:1:资源池,将资源CPU/MEMORY划分到不同的载体上2:负载组,承载负载并将负载映射到不同的资源池3: 分类函数,将不同回话映射到不同的负载组08提供两种预定义的系统资源池1 ...

  3. CDC--Demo

    --CDC通过对事务日志的异步读取,记录DML操作的发生时间.--类型和实际影响的数据变化,然后将这些数据记录到启用--CDC时自动创建的表中.通过cdc相关的存储过程,可以获--取详细的数据变化情况 ...

  4. 解决SqlServer 2005 sa帐户不能登录问题

    允许 sa 用户远程是很危险的.推荐的做法是在本地新建一个允许远程连接的用户. 1.启用TCP/IP协议. 2.配置监听端口(1433). net -an 检查本地端口是否建立监听,使用 在线IP端口 ...

  5. C语言C++编程学习:排序原理分析

    C语言是面向过程的,而C++是面向对象的 C和C++的区别: C是一个结构化语言,它的重点在于算法和数据结构.C程序的设计首要考虑的是如何通过一个过程,对输入(或环境条件)进行运算处理得到输出(或实现 ...

  6. leecode刷题(9)-- 有效的数独

    leecode刷题(9)-- 有效的数独 有效的数独 描述: 判断一个 9x9 的数独是否有效.只需要根据以下规则,验证已经填入的数字是否有效即可. 数字 1-9 在每一行只能出现一次. 数字 1-9 ...

  7. [Maven实战-许晓斌]-[第二章]-2.4设置HTTP代理

  8. [译文]casperjs 的API-casper模块

    Casper class: 可以通过这个模块的create()方法来获取这个模块的一个实例,这是最容易的: var casper = require('casper').create(); 我们也可以 ...

  9. Layout2:StackPanel(补交作业)

    <StackPanel Orientation="Horizontal" VerticalAlignment="Bottom" > <Rect ...

  10. Python3之 contextlib

    Python中当我们们打开文本时,通常会是用with语句,with语句允许我们非常方便的使用资源,而不必担心资源没有关闭. with open('/path/filename', 'r') as f: ...