public class Counter {
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)建站!專(zhuān)注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、小程序設(shè)計(jì)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶(hù)創(chuàng)新互聯(lián)還提供了晉城免費(fèi)建站歡迎大家使用!
private int count;
// 每訪問(wèn)一次,計(jì)數(shù)器自加一
public int getCount() {
return ++count;
}
public void setCount(int count) {
this.count = count;
}
}
%-- 定義一個(gè) session 范圍內(nèi)的計(jì)數(shù)器 記錄個(gè)人訪問(wèn)信息 --%
jsp:useBean id="personCount" class="com.helloweenvsfei.jspweb.bean.Counter" scope="session" /
%-- 定義一個(gè) application 范圍內(nèi)的計(jì)數(shù)器 記錄所有人的訪問(wèn)信息 --%
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計(jì)數(shù)器/legend
table align="center" width="400"
tr
td width=150 align="right" style="font-weight:bold; "您的訪問(wèn)次數(shù):/td
td
%-- 獲取個(gè)人的 訪問(wèn)次數(shù) --%
jsp:getProperty name="personCount" property="count" / 次
/td
/tr
tr
td width=150 align="right" style="font-weight:bold; "總共的訪問(wèn)次數(shù):/td
td
%-- 獲取所有人的 訪問(wèn)次數(shù) --%
jsp:getProperty name="totalCount" property="count" / 次
/td
/tr
/table
/fieldset
/form
/div
希望你能幫到你
DIV class="h"
%-- 記錄網(wǎng)站訪問(wèn)次數(shù) --%
%
Integer counter = (Integer)application.getAttribute("counter"); //先從application里面獲取計(jì)數(shù)器的key的值
if(counter==null){
//如果該值為null,說(shuō)明第一次訪問(wèn)
application.setAttribute("counter",1);
counter=(Integer)application.getAttribute("counter");
}else {
//如果該值不為空,取出來(lái)進(jìn)行累加
int i = counter.intValue();
i++;
application.setAttribute("counter",i);//累加后再放進(jìn)去
}
%
% User user =(User)session.getAttribute("users"); %
%="歡迎"+user.getName() % |您是第%=counter.intValue()%位訪客
/DIV
謝謝~
根據(jù)你所謂的結(jié)束日期 往前推一周的時(shí)間算出開(kāi)始日期,具體的代碼懶得寫(xiě)了 你查下Calendar類(lèi)的用法 里面有這個(gè)推前一周的寫(xiě)法 希望能幫到你
步驟一、建一個(gè)表,表名任意,這里取名為:visitorcounter,表的結(jié)構(gòu)如下所示:
+-------+------------------+------+-----+------------+----------------+
| 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 | |
+-------+------------------+------+-----+------------+----------------+
步驟二、建立一個(gè)java類(lèi),名字也為:visitorcounter,類(lèi)的內(nèi)容如下:
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;
//加載訪問(wèn)量
public static void loadNum(){
if(total_num1)
loadTotalNum();
if(today_num1)
loadToadyNum();
}
//加載今日訪問(wèn)量
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:獲得訪問(wèn)人數(shù)");
}
}
//加載總訪問(wèn)量
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:獲得訪問(wèn)人數(shù)");
}
}
//增加總的訪問(wèn)量
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:增加訪問(wèn)人數(shù)");
}
return k;
}
//增加今日的訪問(wèn)量
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:增加訪問(wèn)人數(shù)");
}
return k;
}
//獲得今天的日期
private static String getTodayDate(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(new Date());
}
///獲得今日訪問(wèn)量
public static long getTodayNum(){
loadNum();
return today_num;
}
//獲得總的訪問(wèn)量
public static long getTotalNum(){
loadNum();
return total_num;
}
}
步驟三、經(jīng)過(guò)以上的步驟后,在頁(yè)面中加入以下的代碼,就可以實(shí)現(xiàn)網(wǎng)站訪問(wèn)量的統(tǒng)計(jì)工作:
if(session.isNew())
{
VisitorCounter.incTodayCounter();
}
%
今日訪問(wèn)量:%=VisitorCounter.getTodayNum() %br/
總的訪問(wèn)量: %=VisitorCounter.getTotalNum() %
分享題目:后臺(tái)數(shù)量統(tǒng)計(jì)java代碼 java統(tǒng)計(jì)報(bào)表數(shù)據(jù)量大怎么做
本文來(lái)源:http://jinyejixie.com/article38/dopcgpp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、網(wǎng)站維護(hù)、網(wǎng)頁(yè)設(shè)計(jì)公司、App設(shè)計(jì)、手機(jī)網(wǎng)站建設(shè)、軟件開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)