当前位置: 移动技术网 > IT编程>开发语言>Java > JSP实现网页访问统计

JSP实现网页访问统计

2017年12月08日  | 移动技术网IT编程  | 我要评论
最近学习jave ee 中的jsp网页开发,需要实现网页访问量的统计,刚开始不知道如何实现,后来问了一下老师,老师是这样回答我的:要实现网页访问的统计,你可以利用appli

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

<%@ 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服务器从新启动时,数据也被清零了。
下面还是具体实现:

<%@ 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

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

<%@ 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文本来实现网站的访问统计。

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文件代码:

<%@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> 

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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网