java如何统计网站访问量?

2024-12-19 21:12:19
推荐回答(5个)
回答1:

  步骤一、建一个表,表名任意,这里取名为:visitorcounter,表的结构如下所示:
  +-------+------------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+------------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| vdate | date | NO | | 2000-01-01 | |
| vnum | int(11) | NO | | 0 | |
+-------+------------------+------+-----+------------+----------------+
  步骤二、建立一个java类,名字也为:visitorcounter,类的内容如下:
  package com.hdzx.pub;
  import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
  public class VisitorCounter {
private final static String TABLE_NAME = "visitorcounter";
private static String today = null;
private static long today_num = 0;
private static long total_num = 0;
//加载访问量
public static void loadNum(){
if(total_num<1)
loadTotalNum();
if(today_num<1)
loadToadyNum();
}
//加载今日访问量
private static void loadToadyNum() {
// TODO Auto-generated method stub
DBConnect db = null;
ResultSet rs = null;
if(today==null)
today = getTodayDate();
String sql = "select vnum from "+TABLE_NAME+" where vdate='"+today+"'";
try {
db = new DBConnect();
rs = db.executeQuery(sql);
if(rs.next()){
today_num = rs.getLong("vnum");
}
else
{
sql = "insert into "+TABLE_NAME+"(vdate,vnum) values('"+today+"',0)";
db.executeUpdate(sql);
today_num = 0;
}
} catch (Exception e) {
// TODO: handle exception
today_num = 0;
System.out.println("com.hdzx.pub~VisitorCounter.incTotalCounter:获得访问人数");
}
}
//加载总访问量
private static void loadTotalNum() {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
DBConnect db = null;
ResultSet rs = null;
if(today==null)
today = getTodayDate();
String sql = "select vnum from "+TABLE_NAME+" where id=1";
try {
db = new DBConnect();
rs = db.executeQuery(sql);
if(rs.next()){
total_num = rs.getLong("vnum");
}
else
{
total_num = 0;
}
} catch (Exception e) {
// TODO: handle exception
total_num = 0;
System.out.println("com.hdzx.pub~VisitorCounter.incTotalCounter:获得访问人数");
}
}
//增加总的访问量
private static int incTotalCounter(){
int k = 0;
DBConnect db = null;
loadNum();
total_num = total_num+1;
String sql = "update "+TABLE_NAME+" set vnum="+total_num+" where id=1";
try {
db = new DBConnect();
k = db.executeUpdate(sql);
} catch (Exception e) {
// TODO: handle exception
System.out.println("com.hdzx.pub~VisitorCounter.incTotalCounter:增加访问人数");
}
return k;
}
//增加今日的访问量
public static int incTodayCounter(){
int k = 0;
DBConnect db = null;
String sql = null;
loadNum();
today_num += 1;
sql = "update "+TABLE_NAME+" set vnum="+today_num+" where vdate='"+today+"'";
try {
db = new DBConnect();
k = db.executeUpdate(sql);
if(k > 0)
incTotalCounter();
} catch (Exception e) {
// TODO: handle exception
System.out.println("com.hdzx.pub~VisitorCounter.incTotalCounter:增加访问人数");
}
return k;
}
//获得今天的日期
private static String getTodayDate(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(new Date());
}
///获得今日访问量
public static long getTodayNum(){
loadNum();
return today_num;
}
//获得总的访问量
public static long getTotalNum(){
loadNum();
return total_num;
}
}
步骤三、经过以上的步骤后,在页面中加入以下的代码,就可以实现网站访问量的统计工作:
  if(session.isNew())
{
VisitorCounter.incTodayCounter();
}
%>
今日访问量:<%=VisitorCounter.getTodayNum() %>

总的访问量: <%=VisitorCounter.getTotalNum() %>

回答2:

.如果是一个ip一天只能访问一次的话
每当用户访问的时候
用 String localIp=request.getLocalAddr();
取得用户ip
这里怎么说呢 应该把每次访问的ip都存起来
然后用这个新ip和 存起来的ip做下比较就

回答3:

这个简单啊,你只需要把参数写到application中去就可以啊,至于ip的话,只要一样的一天统计一次就可以了 ,application是ServletContext对象实例,生命周期是项目的启动和销毁阶段,里面设置的参数只要不断进行更新就可以啊

回答4:

学过servlet应该也会Listener吧
建立一个requestListener 有请求的时候就把请求连带用户ip信息存入数据库中(
时间判定,相同则不存,未发现相同则存入数据库

)

回答5:

页面加载的时候直接访问后台,取ip时间保存就好了;这是原始数据,如果一天算一次,那groupby时间(只取到yyyy-MM-dd)就好了。