转:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PipedWriter;
import java.io.PrintWriter;
public class CountPerson {
//保存访问量,这样就不会出现从起tomcat访问量就从0算起
public static void saveCount(String fileName,long count){
try {
PrintWriter out = new PrintWriter(new FileWriter(fileName));
out.print(count);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//读出访问量
public static long replaceCount(String fileName){
long count =0;
File file = new File(fileName);
if(!file.exists()){
saveCount(fileName,0);
}
try {
BufferedReader bur = new BufferedReader(new FileReader(file));
count = Long.parseLong(bur.readLine());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
count=0;
}
return count;
}
//用图片显示访问量,有0-9个.gif图片
public static String drawingCount(long count){
String str = ""+count ;
String img = "";
for(int i=0;i<str.trim().length();i++){
img = img+"<img src='images\\"+str.charAt(i)+".gif'>";
}
return img;
}
}
-------------------------------------------------------------------
jsp显示页面
<html>
<head>
</head>
<%
long count = CountPerson.replaceCount(request.getRealPath("/")+"count.txt");
if(session.getAttribute("CHEN_CHAO")==null){
session.setAttribute("CHEN_CHAO","123");
session.setMaxInactiveInterval(60*60*24);
count = count+1;
CountPerson.saveCount(request.getRealPath("/")+"count.txt",count);
}
%>
<body>
<h3>该Web应用自起动访问量: <%=CountPerson.drawingCount(count) %></h3>
</body>
</html>
忘了说。。在目录下面会有一个count.txt的文本文件,里面记录的是访问数量(可以修改的)。。当要重新部署项目时要记得把这文件复制过去。
就如同上传文件的附近一样。