JSP中实现网页访问统计的方法

JSP中实现网页访问统计的方法

最近学习Jave EE 中的jsp网页开发,需要实现网页访问量的统计,刚开始不知道如何实现,后来问了一下老师,老师是这样回答我的:要实现网页访问的统计,你可以利用application对象来实现,不能用seesion对象,因为session是属于同一个会话的,关掉浏览器数据就没有了,而application是在同一浏览器下的,只要是同一个浏览器,将数据保存在applicaiton对象中,这样就可以保证数据的不变性。其实这些我都懂,我只是不知道如何在jsp用代码实现。后来我只能上网看看有没有具体的解决方案,搜了很久,没有我想要的答案,我想要实现的只是简单的统计,没有实现更加复杂的功能。后来还是在CSDN这里找到了答案,在这里简单总结一下实现网页访问统计的几种方法:

1. 利用application对象进行统计,得到的效果是每进入一次该网页就统计一次。但效果不怎么好,因为一般统计网页访问量,刷新是不算进统计里的,这里就是这种缺点。

具体实现是:

  1. <%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>  
  2. <html>  
  3.   <head>  
  4.     <title>java 计数器程序</title>  
  5.   </head>  
  6.   <body>  
  7.   <%  
  8.     if (application.getAttribute("count") == null) {  
  9.         application.setAttribute("count", new Integer(0));  
  10.     }  
  11.     Integer count = (Integer) application.getAttribute("count");  
  12.     application  
  13.             .setAttribute("count", new Integer(count.intValue() + 1));  
  14.     count = (Integer) application.getAttribute("count");  
  15.   %>  
  16.      <center>这是第<%=count.intValue()%>个访问者</center>  
  17.   </body>  
  18.   </html>  
<%@ page language="java" import="java.util.*" pageEncoding="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>

 

2.为了解决上面的问题,有了另一种方法,就是同时利用application对象和session对象来统计,这种方法的原理是从打开浏览器到关闭浏览器算是访问一次,刷新、返回等操作不算做一次访问。但还是有缺陷,当jsp服务器从新启动时,数据也被清零了。

下面还是具体实现:

  1. <%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>  
  2. <html>  
  3.   <head>  
  4.     <title>java 计数器程序</title>  
  5.   </head>  
  6.   <body>  
  7.   <%  
  8.     int n = 0; String counter = (String)application.getAttribute("counter");  
  9.     if(counter != null){  
  10.         n = Integer.parseInt(counter);  
  11.     }  
  12.     if(session.isNew())  
  13.         ++n;  
  14.   %>  
  15.      <center>这是第<%out.print(n);%>个访问者</center>  
  16.      <%  
  17.      counter = String.valueOf(n);  
  18.      application.setAttribute("counter", counter);  
  19.          %>  
  20.   </body>  
  21.   </html>  
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<html>
  <head>
    <title>java 计数器程序</title>
  </head>
  <body>
  <%
  	int n = 0; String counter = (String)application.getAttribute("counter");
  	if(counter != null){
  		n = Integer.parseInt(counter);
  	}
  	if(session.isNew())
  		++n;
  %>
  	 <center>这是第<%out.print(n);%>个访问者</center>
  	 <%
  	 counter = String.valueOf(n);
  	 application.setAttribute("counter", counter);
  	 	 %>
  </body>
  </html>


3. 第三种方法是将统计数据存储在本地的文件当中,比如存储在一个txt文件当中。

这是为了解决重启服务器之后数据不用担心会丢失。

创建一个类:JSPCount

 

  1. import java.io.BufferedReader;  
  2. import java.io.File;  
  3. import java.io.FileNotFoundException;  
  4. import java.io.FileReader;  
  5. import java.io.FileWriter;  
  6. import java.io.IOException;  
  7. import java.io.PrintWriter;  
  8.   
  9.   
  10. public class JSPCount {  
  11.     //写入文件的方法  
  12.     public static void write2File(String filename, long count){  
  13.         try{  
  14.             PrintWriter out = new PrintWriter(new FileWriter(filename));  
  15.             out.println(count);  
  16.             out.close();  
  17.         } catch (IOException e) {  
  18.             // TODO: handle exception  
  19.             e.printStackTrace();  
  20.         }  
  21.     }  
  22.       
  23.     //读文件的方法  
  24.     public static long readFromFile(String filename){  
  25.         File file = new File(filename);  
  26.         long count = 0;  
  27.         if(!file.exists()){  
  28.             try {  
  29.                 file.createNewFile();  
  30.             } catch (IOException e) {  
  31.                 // TODO Auto-generated catch block  
  32.                 e.printStackTrace();  
  33.             }  
  34.             write2File(filename, 0);  
  35.         }  
  36.         try{  
  37.             BufferedReader in = new BufferedReader(new FileReader(file));  
  38.             try{  
  39.                 count = Long.parseLong(in.readLine());  
  40.             }  
  41.             catch (NumberFormatException e) {  
  42.                 // TODO: handle exception  
  43.                 e.printStackTrace();  
  44.             } catch (IOException e) {  
  45.                 // TODO Auto-generated catch block  
  46.                 e.printStackTrace();  
  47.             }  
  48.         } catch (FileNotFoundException e) {  
  49.             // TODO: handle exception  
  50.             e.printStackTrace();  
  51.         }  
  52.         return count;  
  53.     }  
  54. }  
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

  1. <%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>  
  2. <%@ page import="org.wwj.count.JSPCount" %>  
  3. <html>  
  4.   <head>  
  5.     <title>java 计数器程序</title>  
  6.   </head>  
  7.   <body>  
  8.   <%  
  9.     JSPCount CountFileHandler = new JSPCount();  
  10.     //读取文件  
  11.     long count = CountFileHandler.readFromFile(request.getRealPath("/")  + "count.txt");  
  12.     count = count + 1;  //修改记录 +1  
  13.     out.print(count);   //显示数据  
  14.     //更新文件内容。  
  15.     CountFileHandler.write2File(request.getRealPath("/")  + "count.txt", count);  
  16.       
  17.    %>  
  18.   </body>  
  19.   </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文本来实现网站的访问统计。

  1. import java.io.BufferedReader;  
  2. import java.io.File;  
  3. import java.io.FileNotFoundException;  
  4. import java.io.FileReader;  
  5. import java.io.FileWriter;  
  6. import java.io.IOException;  
  7. import java.io.PrintWriter;  
  8.   
  9. import javax.servlet.http.HttpServlet;  
  10.   
  11. public class Counter extends HttpServlet{  
  12.     //写入文件的方法  
  13.     public static void write2File(String filename, long count){  
  14.         try{  
  15.             PrintWriter out = new PrintWriter(new FileWriter(filename));  
  16.             out.println(count);  
  17.             out.close();  
  18.         } catch (IOException e) {  
  19.             // TODO: handle exception  
  20.             e.printStackTrace();  
  21.         }  
  22.     }  
  23.       
  24.     //读文件的方法  
  25.     public static long readFromFile(String filename){  
  26.         File file = new File(filename);  
  27.         long count = 0;  
  28.         if(!file.exists()){  
  29.             try {  
  30.                 file.createNewFile();  
  31.             } catch (IOException e) {  
  32.                 // TODO Auto-generated catch block  
  33.                 e.printStackTrace();  
  34.             }  
  35.             write2File(filename, 0);  
  36.         }  
  37.         try{  
  38.             BufferedReader in = new BufferedReader(new FileReader(file));  
  39.             try{  
  40.                 count = Long.parseLong(in.readLine());  
  41.             }  
  42.             catch (NumberFormatException e) {  
  43.                 // TODO: handle exception  
  44.                 e.printStackTrace();  
  45.             } catch (IOException e) {  
  46.                 // TODO Auto-generated catch block  
  47.                 e.printStackTrace();  
  48.             }  
  49.         } catch (FileNotFoundException e) {  
  50.             // TODO: handle exception  
  51.             e.printStackTrace();  
  52.         }  
  53.         return count;  
  54.     }  
  55. }  
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文件代码:

  1. <%@page import="org.servlet.count.Counter"%>  
  2. <%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>  
  3. <html>  
  4.     <head>  
  5.         <title>java 计数器程序</title>  
  6.     </head>  
  7.     <body>  
  8.     <%  
  9.     Counter CountFileHandler = new Counter();  
  10.     long count = 0;  
  11.     if(application.getAttribute("count") == null){  
  12.         count = CountFileHandler.readFromFile(request.getRealPath("/") + "count.txt");  
  13.         application.setAttribute("count", new Long(count));  
  14.     }     
  15.     count = (Long)application.getAttribute("count");  
  16.     if(session.isNew()){  
  17.         count++;  
  18.         application.setAttribute("count", count);  
  19.         //更新文件目录  
  20.         CountFileHandler.write2File(request.getRealPath("/") + "count.txt",count);  
  21.         }  
  22.     %>  
  23.     访问人数:<%=count %>  
  24.         </body>  
  25. </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>



 

 

 以上四种方法,是每一次改进才得到的方法,如果要实现网站访问统计,当然最后一种是最好的,知识不是一步登天,需要在问题上不断改进,获得最终的解决方案,当然最后一种不一定是最好的,实现策略上,如果可以利用数据库也是可以的,但我认为每次访问网站都要读和写数据库,这样效率比较低,比较不好。

版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值