JSP:访客计数实现的几种方式利与弊
2014-02-22 13:57
369 查看
一:同时利用application对象和session对象来统计
这种方法的原理是从打开浏览器到关闭浏览器算是访问一次。优点:刷新、返回等操作不算做一次访问。缺陷:当jsp服务器从新启动时,数据也被清零了。
代码片段:
<body>
<div>
<% if(application.getAttribute("count") == null){
application.setAttribute("count", new Integer(0));
}
if(session.isNew()){
int count = (Integer)application.getAttribute("count");
count++;
application.setAttribute("count",count);
}
%>
<font color="red" >到目前为止已有<%=application.getAttribute("count") %>客户访问该网页</font>
</div>
<br>
</body>
二:将统计数据存储在本地的文件当中
例如一个简单的txt文件。优点:不用担心服务器重启带来的清零烦恼;缺点:影响服务器端响应客户端请求的时间。
代码片段:
创建一个类:JSPCount
[java]
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 JSPCount {
//写入文件的方法
public static void write2File(String filename, long count){
try{
PrintWriter out = new PrintWriter(new FileWriter(filename));
out.println(count);
out.close();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
//读文件的方法
public static long readFromFile(String filename){
File file = new File(filename);
long count = 0;
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
write2File(filename, 0);
}
try{
BufferedReader in = new BufferedReader(new FileReader(file));
try{
count = Long.parseLong(in.readLine());
}
catch (NumberFormatException e) {
// TODO: handle exception
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}
return count;
}
} 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 JSPCount {
//写入文件的方法
public static void write2File(String filename, long count){
try{
PrintWriter out = new PrintWriter(new FileWriter(filename));
out.println(count);
out.close();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
//读文件的方法
public static long readFromFile(String filename){
File file = new File(filename);
long count = 0;
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
write2File(filename, 0);
}
try{
BufferedReader in = new BufferedReader(new FileReader(file));
try{
count = Long.parseLong(in.readLine());
}
catch (NumberFormatException e) {
// TODO: handle exception
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}
return count;
}
}
在WebRoot目录下建jsp文件:count.jsp[plain]
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%@ page import="org.wwj.count.JSPCount" %>
<html>
<head>
<title>java 计数器程序</title>
</head>
<body>
<%
JSPCount CountFileHandler = new JSPCount();
//读取文件
long count = CountFileHandler.readFromFile(request.getRealPath("/") + "count.txt");
count = count + 1; //修改记录 +1
out.print(count); //显示数据
//更新文件内容。
CountFileHandler.write2File(request.getRealPath("/") + "count.txt", count);
%>
</body>
</html> <%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%@ page import="org.wwj.count.JSPCount" %>
<html>
<head>
<title>java 计数器程序</title>
</head>
<body>
<%
JSPCount CountFileHandler = new JSPCount();
//读取文件
long count = CountFileHandler.readFromFile(request.getRealPath("/") + "count.txt");
count = count + 1; //修改记录 +1
out.print(count); //显示数据
//更新文件内容。
CountFileHandler.write2File(request.getRealPath("/") + "count.txt", count);
%>
</body>
</html>
程序运行之后会在tomcat下的webapps目录下的对应的web项目生成一个count.txt文本文件
4.第三种方法,只是保存了访问的统计数据罢了,但没有保证刷新页面的时候不会自增,这样还是不好。当然总会有解决的办法的,一般的解决方案就是结合各种方案的优点。下面是由session对象+application对象+txt文本来实现网站的访问统计。[java]
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;
import javax.servlet.http.HttpServlet;
public class Counter extends HttpServlet{
//写入文件的方法
public static void write2File(String filename, long count){
try{
PrintWriter out = new PrintWriter(new FileWriter(filename));
out.println(count);
out.close();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
//读文件的方法
public static long readFromFile(String filename){
File file = new File(filename);
long count = 0;
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
write2File(filename, 0);
}
try{
BufferedReader in = new BufferedReader(new FileReader(file));
try{
count = Long.parseLong(in.readLine());
}
catch (NumberFormatException e) {
// TODO: handle exception
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}
return count;
}
} 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;import javax.servlet.http.HttpServlet;public class Counter extends HttpServlet{
//写入文件的方法
public static void write2File(String filename, long count){
try{
PrintWriter out = new PrintWriter(new FileWriter(filename));
out.println(count);
out.close();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
//读文件的方法
public static long readFromFile(String filename){
File file = new File(filename);
long count = 0;
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
write2File(filename, 0);
}
try{
BufferedReader in = new BufferedReader(new FileReader(file));
try{
count = Long.parseLong(in.readLine());
}
catch (NumberFormatException e) {
// TODO: handle exception
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}
return count;
}
}
jsp文件代码:[plain]
<%@page import="org.servlet.count.Counter"%>
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<html>
<head>
<title>java 计数器程序</title>
</head>
<body>
<%
Counter CountFileHandler = new Counter();
long count = 0;
if(application.getAttribute("count") == null){
count = CountFileHandler.readFromFile(request.getRealPath("/") + "count.txt");
application.setAttribute("count", new Long(count));
}
count = (Long)application.getAttribute("count");
if(session.isNew()){
count++;
application.setAttribute("count", count);
//更新文件目录
CountFileHandler.write2File(request.getRealPath("/") + "count.txt",count);
}
%>
访问人数:<%=count %>
</body>
</html> <%@page import="org.servlet.count.Counter"%>
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<html>
<head>
<title>java 计数器程序</title>
</head>
<body>
<%
Counter CountFileHandler = new Counter();
long count = 0;
if(application.getAttribute("count") == null){
count = CountFileHandler.readFromFile(request.getRealPath("/") + "count.txt");
application.setAttribute("count", new Long(count));
}
count = (Long)application.getAttribute("count");
if(session.isNew()){
count++;
application.setAttribute("count", count);
//更新文件目录
CountFileHandler.write2File(request.getRealPath("/") + "count.txt",count);
}
%>
访问人数:<%=count %>
</body>
</html>
这种方法的原理是从打开浏览器到关闭浏览器算是访问一次。优点:刷新、返回等操作不算做一次访问。缺陷:当jsp服务器从新启动时,数据也被清零了。
代码片段:
<body>
<div>
<% if(application.getAttribute("count") == null){
application.setAttribute("count", new Integer(0));
}
if(session.isNew()){
int count = (Integer)application.getAttribute("count");
count++;
application.setAttribute("count",count);
}
%>
<font color="red" >到目前为止已有<%=application.getAttribute("count") %>客户访问该网页</font>
</div>
<br>
</body>
二:将统计数据存储在本地的文件当中
例如一个简单的txt文件。优点:不用担心服务器重启带来的清零烦恼;缺点:影响服务器端响应客户端请求的时间。
代码片段:
创建一个类:JSPCount
[java]
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 JSPCount {
//写入文件的方法
public static void write2File(String filename, long count){
try{
PrintWriter out = new PrintWriter(new FileWriter(filename));
out.println(count);
out.close();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
//读文件的方法
public static long readFromFile(String filename){
File file = new File(filename);
long count = 0;
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
write2File(filename, 0);
}
try{
BufferedReader in = new BufferedReader(new FileReader(file));
try{
count = Long.parseLong(in.readLine());
}
catch (NumberFormatException e) {
// TODO: handle exception
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}
return count;
}
} 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 JSPCount {
//写入文件的方法
public static void write2File(String filename, long count){
try{
PrintWriter out = new PrintWriter(new FileWriter(filename));
out.println(count);
out.close();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
//读文件的方法
public static long readFromFile(String filename){
File file = new File(filename);
long count = 0;
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
write2File(filename, 0);
}
try{
BufferedReader in = new BufferedReader(new FileReader(file));
try{
count = Long.parseLong(in.readLine());
}
catch (NumberFormatException e) {
// TODO: handle exception
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}
return count;
}
}
在WebRoot目录下建jsp文件:count.jsp[plain]
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%@ page import="org.wwj.count.JSPCount" %>
<html>
<head>
<title>java 计数器程序</title>
</head>
<body>
<%
JSPCount CountFileHandler = new JSPCount();
//读取文件
long count = CountFileHandler.readFromFile(request.getRealPath("/") + "count.txt");
count = count + 1; //修改记录 +1
out.print(count); //显示数据
//更新文件内容。
CountFileHandler.write2File(request.getRealPath("/") + "count.txt", count);
%>
</body>
</html> <%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%@ page import="org.wwj.count.JSPCount" %>
<html>
<head>
<title>java 计数器程序</title>
</head>
<body>
<%
JSPCount CountFileHandler = new JSPCount();
//读取文件
long count = CountFileHandler.readFromFile(request.getRealPath("/") + "count.txt");
count = count + 1; //修改记录 +1
out.print(count); //显示数据
//更新文件内容。
CountFileHandler.write2File(request.getRealPath("/") + "count.txt", count);
%>
</body>
</html>
程序运行之后会在tomcat下的webapps目录下的对应的web项目生成一个count.txt文本文件
4.第三种方法,只是保存了访问的统计数据罢了,但没有保证刷新页面的时候不会自增,这样还是不好。当然总会有解决的办法的,一般的解决方案就是结合各种方案的优点。下面是由session对象+application对象+txt文本来实现网站的访问统计。[java]
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;
import javax.servlet.http.HttpServlet;
public class Counter extends HttpServlet{
//写入文件的方法
public static void write2File(String filename, long count){
try{
PrintWriter out = new PrintWriter(new FileWriter(filename));
out.println(count);
out.close();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
//读文件的方法
public static long readFromFile(String filename){
File file = new File(filename);
long count = 0;
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
write2File(filename, 0);
}
try{
BufferedReader in = new BufferedReader(new FileReader(file));
try{
count = Long.parseLong(in.readLine());
}
catch (NumberFormatException e) {
// TODO: handle exception
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}
return count;
}
} 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;import javax.servlet.http.HttpServlet;public class Counter extends HttpServlet{
//写入文件的方法
public static void write2File(String filename, long count){
try{
PrintWriter out = new PrintWriter(new FileWriter(filename));
out.println(count);
out.close();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
//读文件的方法
public static long readFromFile(String filename){
File file = new File(filename);
long count = 0;
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
write2File(filename, 0);
}
try{
BufferedReader in = new BufferedReader(new FileReader(file));
try{
count = Long.parseLong(in.readLine());
}
catch (NumberFormatException e) {
// TODO: handle exception
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}
return count;
}
}
jsp文件代码:[plain]
<%@page import="org.servlet.count.Counter"%>
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<html>
<head>
<title>java 计数器程序</title>
</head>
<body>
<%
Counter CountFileHandler = new Counter();
long count = 0;
if(application.getAttribute("count") == null){
count = CountFileHandler.readFromFile(request.getRealPath("/") + "count.txt");
application.setAttribute("count", new Long(count));
}
count = (Long)application.getAttribute("count");
if(session.isNew()){
count++;
application.setAttribute("count", count);
//更新文件目录
CountFileHandler.write2File(request.getRealPath("/") + "count.txt",count);
}
%>
访问人数:<%=count %>
</body>
</html> <%@page import="org.servlet.count.Counter"%>
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<html>
<head>
<title>java 计数器程序</title>
</head>
<body>
<%
Counter CountFileHandler = new Counter();
long count = 0;
if(application.getAttribute("count") == null){
count = CountFileHandler.readFromFile(request.getRealPath("/") + "count.txt");
application.setAttribute("count", new Long(count));
}
count = (Long)application.getAttribute("count");
if(session.isNew()){
count++;
application.setAttribute("count", count);
//更新文件目录
CountFileHandler.write2File(request.getRealPath("/") + "count.txt",count);
}
%>
访问人数:<%=count %>
</body>
</html>
相关文章推荐
- 利用APACHE及SHELL做当日时间端内流量统计
- 蓝信流量统计及概要日志系统
- 演示:使用Sniffer统计与分析流量 推荐
- 演示:使用NBAR统计与分析流量 推荐
- 项目中用到的一个简单的流量统计例子-java流量统计
- 华为S系列交换机流策略使用方式和流量统计
- 给你的博客添加一些实用工具
- 交换机涉及丢包问题进行流量统计
- 简易网站流量统计工具
- 流量统计 +NetWorkStatsManager
- iOS流量监控代码
- iOS获取设备流量使用情况 iPhone Data Usage Tracking/Monitoring
- android流量统计
- Hadoop实战学习(3)-手机流量统计
- 终极版:Andorid手机流量监测思路与代码实现(一)
- Android TrafficStats类的使用
- 屏蔽所有统计代码(51.la cnzz 百度统计 谷歌分析师adsense、屏蔽淘宝客广告代码)的方法
- 代码实现Android流量统计
- QT---基于WinPcap的局域网络管理工具(主机扫描、包过滤、ARP攻击、端口扫描)
- 104_流量统计