首先是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 >
<% @ 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());
}
}
}
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());
}
}
}
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());
}
}
}