JSP网站访问人数计数器

首先是JSP页面文件,代码很简单,只有几行,主要代码放在javaBean类中:

(注意我利用图片显示访问量的数字,我是放在站点的images下的digit文件夹下)

这里的第一个javaBean设置成session是让每次同一人访问只算一次,除非他关闭浏览器,再进来才算两次。

这样同一次访问,刷新页面不累计访问量。
<% @ page language = " java " import = " java.util.*,java.io.* " pageEncoding = " gb2312 " %>
<% @ taglib prefix = " c " uri = " http://java.sun.com/jsp/jstl/core " %>

< html >

< head >
< title > LinQifo </ title >
</ head >

< body >

< jsp:useBean id = " a " scope = " session " class = " linQifoP41.addone " />
< jsp:useBean id = " b " scope = " page " class = " linQifoP41.display " />

已经有 <% b.counter();
for ( int i = 9 ;i >= 0 ; -- i)
{
out.print(b.img[i]);
}

%> 人访问该网页,欢迎您!

</ body >
</ html >

接下来是addone.java类,用来使访问量+1
package linQifoP41;

import java.io. * ;

public class addone {

public addone()
{
try
{
String path = "" ;
File f = new File(path, " counter.txt " );

if (f.exists())
{


FileReader fr = new FileReader(path + " counter.txt " );

BufferedReader br = new BufferedReader(fr);
String s = br.readLine();
int i = Integer.parseInt(s);
i ++ ;
System.out.print(i);
br.close();
s = Integer.toString(i);

FileWriter fw1 = new FileWriter(path + " counter.txt " );
PrintWriter pw = new PrintWriter(fw1);
pw.println(s);
pw.close();

}

else
{

f.createNewFile();
FileWriter fw = new FileWriter(path + " counter.txt " );
BufferedWriter bw = new BufferedWriter(fw);
bw.write( " 1 " );
bw.flush();
fw.close();

}

}
catch (IOException e)
{
System.out.print(e.toString());
}


}
}

第二个是用来显示访问量结果的display.java
package linQifoP41;

import java.io. * ;

public class display {

public String[] img = new String[ 10 ];
// 读取文件
public void counter()
{
try
{
String path = "" ;
File f = new File(path + " counter.txt " );

if (f.exists())
{
FileReader fr = new FileReader(path + " counter.txt " );
BufferedReader br = new BufferedReader(fr);
String s = br.readLine();
int i = Integer.parseInt(s);
int st = 10 ;
int j = 0 ;
while (j <= 9 )
{
img[j] = Integer.toString(i % st);
img[j] = img[j] + " .jpg " ;
img[j] = " images/digit/ " + img[j];
img[j] = " <img src= " + img[j] + " > " ;
img[j] += " </img> " ;
i /= 10 ;
j ++ ;
}
}
else
{
System.out.print( " counter.txt文件不存在! " );
}

}
catch (IOException e)
{
System.out.print(e.toString());
}
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值