jsp网站访问次数统计的几种方法(比较简单的几种)
来源:互联网 发布:pusher js 编辑:程序博客网 时间:2024/05/18 01:55
我采用的是jsp网页,但是不管采用什么语言,原理是一样的。最近在完善暑假做的一个简单年级网站,遇到很多问题。我想很多新手都会遇到这些问题,就把自己的心得体会分享给大家。因为我找到的解决方法,也是很多网友分享的。
第一种,单页面统计。就是说,只要点击这个页面就会统计一次。
第一种,单页面统计。就是说,只要点击这个页面就会统计一次。
<%@ page contentType="text/html;charset=GB2312" %> <html> <head> <title> java计数器程序片 </title> </head> <body> <%!//在这种标记中定义的变量为全局变量 int count=0; synchronized void count(){ count++; } %> <% count(); out.println("这是第"+count+"个访问者!"); %> </body> </html> 第二中,是利用jsp的内置对象application进行统计。这个程序结果运行分析,也是访问一次页面统计一次。感觉还是不够好。真正满意的是浏览器打开网页,到关闭网页算一次,这样统计比较实际。 <%@ page contentType="text/html;charset=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>第三中,利用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文本中,那样重启服务器也不会丢失了。写一个severlet
counter.java
package com.benb.servlet;
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 counter {
public static void writeFile(String filename, long count) {
try {
PrintWriter out = new PrintWriter(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 = new BufferedReader(new FileReader(f));
try {
count = Long.parseLong(in.readLine());
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return count;
}
}
package com.benb.servlet;
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 counter {
public static void writeFile(String filename, long count) {
try {
PrintWriter out = new PrintWriter(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 = new BufferedReader(new FileReader(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了
<%@ page import="com.benb.servlet.counter"%>
<%
counter CountFileHandler=new counter(); //创建对象
long count=CountFileHandler.readFile(request.getRealPath("/")+"
count.txt");
//读取文件获取数据赋给count
count=count+1;//修改记录,数据加1
out.println(count);//显示记录数
CountFileHandler.writeFile(request.getRealPath("/")+"count.txt",
count);//更新文件记录
%>
<%@ page import="com.benb.servlet.counter"%>
<%
counter CountFileHandler=new counter(); //创建对象
long count=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类型。
package com.tozhan.cn;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
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
}
}
页面编码如下
import java.io.IOException;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
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);//更新文件记录
%>
<%@ 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空间,做实验的简单网站 http://vnia.v018010.10000net.cn/class_news.jsp 现在正在完善中
这是我的51CTO博客 http://2199572.blog.51cto.com/2189572/719181
记述我学习java的里程
新用户注册 刷新评论列表
- jsp网站访问次数统计的几种方法(比较简单的几种)
- jsp网站访问次数统计的几种方法
- jsp网站访问次数统计的几种方法
- jsp统计网站的访问次数
- 网站统计访问次数的方法
- 网站统计访问次数的方法
- 网站统计访问次数的方法
- 统计网站的访问次数
- 几种统计一段字符串中所有字符出现次数的方法
- 几种比较简单的人物抠图方法
- 统计学习的几种方法
- 时间统计的几种方法
- 几种统计字符串个数的方法
- 几种压缩算法的简单比较
- 几种结构模式的简单比较
- 几种简单排序的比较
- 几种排序方法的比较
- ios监听的几种方法比较
- Outlook 2010 从磁盘读取一个HTML文件作为Email的Body
- 红帽使用OpenShift PaaS为云中Java提供综合生命周期支持
- 基于TCP协议的网络编程--socket编程(2)
- struts和spring集成时不能加载struts.xml文件的原因
- C#委托的一些应用,个人肤浅的一些操作
- jsp网站访问次数统计的几种方法(比较简单的几种)
- 关于DLL的函数
- 李开复大战方舟子第二季:我与奥巴马是同学
- 【转贴】总结下开发调试MSP430系列的一些另类问题 ——备份下以便查阅
- only for test - 4000 characters
- [转]大话PHP之性能
- virtualbox调节ubuntu虚拟机的大小
- 再论C++之垃圾回收(GC)
- C语言深入学习系列 - 字节对齐&内存管理
找优秀程序员,就在博客园
· 阿里巴巴中国站会员数突破5000万
· MarketWatch:Facebook IPO应向谷歌取经
· YouTube十月点击量近210亿次 Facebook居第二
· 谷歌搜索主页拟取消长条导航栏 改为下拉菜单
· Facebook IPO后将展开收购 Twitter或成目标
登录
· 石家庄高级.NET工程师(月薪6K-8K)(盛安德科技)
· 知识库技术编辑(博客园)
· CRM研发工程师(年薪15W以上)(网易有道)
· 中高级.NET程序员(沪江网)
· 上海.NET工程师(中国房产信息集团有限公司)
· 北京Web自动化测试经理(北京捷报数据)
· c#小软件(SaveClassic)开发手记--(1)前言
· 第三章:关于Web Forms
· D2010,indy10.5.5,一些问题解决方法
· 微软ASP.NET站点部署指南(8):部署Code-Only更新
· 你不能错过的50个网页设计者的博客(上)
· HTML 5 & CSS 3的新交互特性
· 我为什么向后端工程师推荐Node.js
· 高德纳(Knuth)谈计算机程序设计艺术(上)
· 整洁的代码 VS 卓越的代码
· 浅谈 HTML5 的 DOM Storage 机制