java统计访问量代码 javaweb统计在线人数和访问量
网站访问量统计java代码怎样写?
DIV class="h"
成都创新互联公司成立与2013年,先为珠晖等服务建站,珠晖等地企业,进行企业商务咨询服务。为珠晖企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
%-- 记录网站访问次数 --%
%
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统计网站的访问量和在线人数的问题,有知道的直接发一个简单的例子我,多谢啦!
首先给你讲一下,我的理解,人数统计好说,因为,可已设置监听器,让其在罩扰服务器启动的时候进行监听,然后统计登录人数,但是统计在线人数,就麻烦点了,因为大多数人都不愿意点击注销按钮,直接关闭浏览器,这样session的关闭就麻烦了点,我先给出统计人数的代码,我想统计在线人数可以定时调用程序来统计,但是不是实时的,物嫌旦所以最大的障碍就是怎么在关闭浏览器的时候,就关者兆闭session
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class ApplicationListener implements ServletContextListener{
public void contextDestroyed(ServletContextEvent arg0) {
ServletContext sc = arg0.getServletContext();
Integer count = (Integer) sc.getAttribute("AccessCount");
try {
PrintWriter out = new PrintWriter(new FileWriter("c:/Users/god/count.txt"));// 将人数保存到文本文件
System.out.println("count:"+count);
out.print(count);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void contextInitialized(ServletContextEvent arg0) {
Integer count = 0;
ServletContext sc = arg0.getServletContext();
try {
Scanner in = new Scanner(new FileReader("c:/Users/god/count.txt"));// 读取已经产生的人数信息,然后进行累加
count = in.nextInt();
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
sc.setAttribute("AccessCount", count);
}
}
在web.xml文件里面进行设置
listener
listener-class
文件的位置也就是包名.ApplicationListener
/listener-class
/listener
统计的时候是设置拦截器,需要在struts.xml中设置
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
@SuppressWarnings("serial")
public class AccessInterceptor implements Interceptor{
private String id;
public void destroy() {
}
public void init() {
}
@SuppressWarnings("unchecked")
public String intercept(ActionInvocation ai) throws Exception {
ServletContext sc = ServletActionContext.getServletContext();
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
if(!session.getId().equals(id)){
Integer count = (Integer) sc.getAttribute("AccessCount");// 判断如果登录成功,统计数字自增
sc.setAttribute("AccessCount", count+1);
id = session.getId();
}
String result = ai.invoke();
return result;
}
}
java如何统计网站访问量?
步骤一、建一个举游表,表名任意,这里取名为: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_num1)
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() %br/
总的访问量: %=VisitorCounter.getTotalNum() %
网站标题:java统计访问量代码 javaweb统计在线人数和访问量
本文URL:http://ybzwz.com/article/ddpiooc.html