java浏览量功能代码 java浏览量功能代码大全
java如何统计网站访问量?
步骤一、建一个表,表名任意,这里取名为:visitorcounter,表的结构如下所示:
鹰潭ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:028-86922220(备注:SSL证书合作)期待与您的合作!
+-------+------------------+------+-----+------------+----------------+
| 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_num1)
loadTotalNum();
if(today_num1)
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() %br/
总的访问量: %=VisitorCounter.getTotalNum() %
java后台浏览量相关
拒绝伸手党,不过可以提供点思路
首先可以添加拦截器或过滤器,对访问的页面先拦截一次
对拦截到的url请求,根据请求的uri查询取得是否访问过当前url凭证,这个有多种选择:cookie、session、数据库等都成。
由于你要关闭浏览器后重新打开又统计,建议用cookie
既然能拿到它是否访问了当前页面,自然能统计。
代码方面还是那句,拒接伸手党!
网站访问量统计java代码怎样写?
DIV class="h"
%-- 记录网站访问次数 --%
%
Integer counter = (Integer)application.getAttribute("counter"); //先从application里面获取计数器的key的值
if(counter==null){
//如果该值为null,说明第一次访问
application.setAttribute("counter",1);
counter=(Integer)application.getAttribute("counter");
}else {
//如果该值不为空,取出来进行累加
int i = counter.intValue();
i++;
application.setAttribute("counter",i);//累加后再放进去
}
%
% User user =(User)session.getAttribute("users"); %
%="欢迎"+user.getName() % |您是第%=counter.intValue()%位访客
/DIV
谢谢~
网站访问量统计java代码?
public class Counter {
private int count;
// 每访问一次,计数器自加一
public int getCount() {
return ++count;
}
public void setCount(int count) {
this.count = count;
}
}
%-- 定义一个 session 范围内的计数器 记录个人访问信息 --%
jsp:useBean id="personCount" class="com.helloweenvsfei.jspweb.bean.Counter" scope="session" /
%-- 定义一个 application 范围内的计数器 记录所有人的访问信息 --%
jsp:useBean id="totalCount" class="com.helloweenvsfei.jspweb.bean.Counter" scope="application" /
div align="center"
form action="method.jsp" method="get"
fieldset style='width: 300'
legend计数器/legend
table align="center" width="400"
tr
td width=150 align="right" style="font-weight:bold; "您的访问次数:/td
td
%-- 获取个人的 访问次数 --%
jsp:getProperty name="personCount" property="count" / 次
/td
/tr
tr
td width=150 align="right" style="font-weight:bold; "总共的访问次数:/td
td
%-- 获取所有人的 访问次数 --%
jsp:getProperty name="totalCount" property="count" / 次
/td
/tr
/table
/fieldset
/form
/div
希望你能帮到你
JAVA我想做一个记录网页浏览次数的累加器的代码怎么写
可以在实现HttpSessionListener当session建立时会调用里面方法, 你就在application里+1就实现了累加了啊
当前名称:java浏览量功能代码 java浏览量功能代码大全
转载源于:http://cdiso.cn/article/docdgdh.html