JSP中实现网页访问统计的方法【转】
我采用的是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);//更新文件记录
%>


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>我们的友谊海枯石烂! 你是第 <%=count %> 位访客</p>
JSP中实现网页访问统计的方法【转】的更多相关文章
- PHP 简易网页访问统计
传统的网页访问统计,已经有很多,如 51la.百度统计.站长统计 一般都需要引用JS,在你的网页内嵌入JS,这个操作存在风险,并且不可控. 可以考虑使用 [img src.css src.link h ...
- jsp中四种传递参数的方法
jsp中四种传递参数的方法如下: 1.form表单 2.request.setAttribute();和request.getAttribute(); 3.超链接:<a herf="i ...
- wuzhicms访问统计实现方法
实现目标:程序实现了对整站页面pv的统计文件的位置:coreframe/app/content/pv.php代码预览: /** * 总站访问次数统计 */ defined('IN_WZ') or ex ...
- (转)JSP中四种传递参数的方法:
1.form表单 2.request.setAttribute();和request.getAttribute(); 3.超链接:<a herf="index.jsp"?a= ...
- Angular 项目打包之后,部署到springboot项目中,刷新访问404解决方法
解决方法1: app.module.ts文件添加两行代码: import { LocationStrategy, HashLocationStrategy } from '@angular/commo ...
- 在Silverlight中打开网页的几种方法
HtmlPage.PopupWindow HtmlPopupWindowOptions option = new HtmlPopupWindowOptions(); option.Directorie ...
- Android中使用adb访问SQLite的方法
(1)打开命令提示符,输入:adb,按回车,如果得到下面一大堆命令说明(如图 1),表示adb的配置是成功的,如果提示"不是内部或外部命令,也不是可运行的程序或批处理文件",那么需 ...
- WPF中打开网页的两种方法
1.浏览器打开 Process proc = new System.Diagnostics.Process(); proc.StartInfo.FileName = "http://www. ...
- 对jsp中的js进行调试的方法
在js中 输入debugger 就可以了
随机推荐
- Partition--分区切换
现有数据表[dbo].[staging_TB1_20131018-104722]和分区表[dbo].[TB1],需要将分区表和数据表中做数据交换 CREATE TABLE [dbo].[staging ...
- 从hive将数据导出到mysql(转)
从hive将数据导出到mysql http://abloz.com 2012.7.20 author:周海汉 在上一篇文章<用sqoop进行mysql和hdfs系统间的数据互导>中,提到s ...
- HBase介绍(3)---框架结构及流程
HBASE依托于Hadoop的HDFS作为存储基础,因此结构也很类似于Hadoop的Master-Slave模式,Hbase Master Server 负责管理所有的HRegion Server,但 ...
- 使用pycharm专业版创建虚拟环境
Location为工程地址 D:\My_python 第二个Location为 虚拟环境放在这个工程下 Base interpreter:基于那个解释器来创建虚拟环境 Create后进入到目录查看下
- 百万数据测试 Entity Framework 到底有多慢
测试环境 硬件:阿里云乞丐配置 操作系统:Centos 7 CPU: 1核 内存:1 GB (I/O优化) 网络:1Mbps(峰值) 软件 .net core 2.0 ZKEACMS For .net ...
- Bitnami WordPress如何让默认URL指向WordPress目录?
Bitnami WordPress下载地址:https://bitnami.com/stack/wordpress/installer ,根据自己操作系统选择对应版本,我是windows server ...
- 带你玩转Visual Studio——带你高效开发
VassistX的简单介绍与下载安装 简单介绍 VassistX的全称是Visual Assist X,是whole tomato开发的一个非常好用的插件,可用于VC6.0及Visual Studio ...
- [转]解读Unity中的CG编写Shader系列8——多光源漫反射
前文中完成最简单的漫反射shader只是单个光源下的漫反射,而往往场景中不仅仅只有一个光源,那么多个光源的情况下我们的物体表面的漫反射强度如何叠加在一起呢?前文打的tag "LightMod ...
- day04.4-装饰器
1. 装饰器 装饰器本身就是函数,功能是为其他函数添加附加功能 装饰器的设计原则: (a). 不修改被修饰函数的源代码: (b). 不修改被修改函数的调用方法. 装饰器=高阶函数+函数嵌套+闭包 装饰 ...
- 微信小程序之WebSocket
本文版权归 OSChina jsongo0 所有,转载请标明出处,以示尊重! 原文:https://my.oschina.net/jsongo/blog/757871 为什么需要websocket?传 ...