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 就可以了
随机推荐
- docker 命令大全
http://www.runoob.com/docker/docker-command-manual.html
- 虚拟化 - Docker
Docker Desktop (for windows and mac) Docker Desktop 注意 安装时有可能卡主,可以关掉重新装 重启启动时,可能报错"VIRTUALIZATI ...
- element-ui学习
1. 小技巧 使用导航路由 配置el-menu-item的index属性,如照片 也可使用数据动态配置导航菜单:参考 走马灯高度自适应 动态设置Carousel的height参数
- MVC的 url 传递参数无效
有些符号(例如“=”)在URL中 直接传递是无效的,如果要在URL中传递这些特殊符号,那么就要使用他们的编码了.下表中列出了一些URL特殊符号及编码 十六进制值 1. + URL 中+号表 ...
- 解决winform datagridview的ClearSelection无效问题
因为把方法放在了界面的构造方法里,此时datagridview还没绘制出来,所以ClearSelection方法无效,放在control或form的load方法里就没问题了 参考:https://ww ...
- 洛谷P5211 [ZJOI2017]字符串(线段树+乱搞)
题面 传送门 题解 为什么大佬们全都是乱搞的--莫非这就是传说中的暴力能进队,乱搞能AC-- 似乎有位大佬能有纯暴力+玄学优化\(AC\)(不算上\(uoj\)的\(Hack\)数据的话--这要是放到 ...
- 使用bluebird解决promise兼容性问题
//引入promiseif(!Promise){ var Promise = require("bluebird"); // Configure Promise.config({ ...
- vue培训记录
在公司做了一次vue相关的培训,自己整理了一些大纲.供大家参考学习! ### 1. 项目构成及原理 [Vue](https://cn.vuejs.org/)###* 主流框架见解及差别 * react ...
- opencv学习笔记(七)---图像金字塔
图像金字塔指的是同一图像不同分辨率的子图的集合,有向下取样金字塔,向上取样金字塔,拉普拉斯金字塔....它是图像多尺度表达的一种,最主要的是用于图像的分割 向下取样金字塔指高分辨率图像向低分辨率图像的 ...
- ajax 常用格式
$.ajax({ url: "/", //请求路径 type: "post",//请求方式 data: "json", //发送请求的数据格 ...