<%@ page contentType="text/html;charset=gb2312"%>
<%@ page language="java" import="java.io.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>計數(shù)器</title>
</head>
<%!
//同步更新計數(shù)器
synchronized void counter(){
ServletContext application=getServletContext(); //構(gòu)造application對象(可選)
String szPath=application.getRealPath("/"); //得到當(dāng)前路徑
szPath=szPath+"hits.txt"; //計數(shù)器文件 0-9999999999999...
String szRecord=""; //記數(shù) String
int nRecord=0; //記數(shù) int
try{
BufferedReader file=new BufferedReader(new FileReader(szPath));
szRecord=file.readLine(); //讀取計數(shù)器文件
}
catch(IOException e){
e.printStackTrace(System.err);
}
if(szRecord==null) szRecord="0"; //如果計數(shù)器文件為空
nRecord=java.lang.Integer.parseInt(szRecord)+1; //計數(shù)器+1
try{
File f=new File(szPath);
PrintWriter pw=new PrintWriter(new FileWriter(f));
pw.print(nRecord); //寫文件
pw.close();
}
catch(IOException e){
System.out.println(e);
}
}
%>
<%
//顯示計數(shù)器
if(session.isNew()){ //如果是新用戶
counter();
}
String Path=application.getRealPath("/");
String szPath=Path+"hits.txt";
String szRecord="";
BufferedReader file=new BufferedReader(new FileReader(szPath));
try{
szRecord=file.readLine();
}
catch(IOException e){
System.out.println("文件未找到!");
}
//顯示7位數(shù)字gif圖像
String szOut="<body topmargin='0' leftmargin='0'>";
int i=0;
int k=7-szRecord.length(); //"0"的個數(shù)
for (i=0;i<k-1;i++){ //顯示"0"
szOut=szOut+"<img src='images/0.gif'>";
}
for (i=0;i<szRecord.length();i++){ //顯示非"0"
szOut=szOut+"<img src='images/"+ szRecord.charAt(i) +".gif'>";
}
szOut=szOut+"</body>";
out.println(szOut);
%>
</html>
<%@ page language="java" import="java.io.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>計數(shù)器</title>
</head>
<%!
//同步更新計數(shù)器
synchronized void counter(){
ServletContext application=getServletContext(); //構(gòu)造application對象(可選)
String szPath=application.getRealPath("/"); //得到當(dāng)前路徑
szPath=szPath+"hits.txt"; //計數(shù)器文件 0-9999999999999...
String szRecord=""; //記數(shù) String
int nRecord=0; //記數(shù) int
try{
BufferedReader file=new BufferedReader(new FileReader(szPath));
szRecord=file.readLine(); //讀取計數(shù)器文件
}
catch(IOException e){
e.printStackTrace(System.err);
}
if(szRecord==null) szRecord="0"; //如果計數(shù)器文件為空
nRecord=java.lang.Integer.parseInt(szRecord)+1; //計數(shù)器+1
try{
File f=new File(szPath);
PrintWriter pw=new PrintWriter(new FileWriter(f));
pw.print(nRecord); //寫文件
pw.close();
}
catch(IOException e){
System.out.println(e);
}
}
%>
<%
//顯示計數(shù)器
if(session.isNew()){ //如果是新用戶
counter();
}
String Path=application.getRealPath("/");
String szPath=Path+"hits.txt";
String szRecord="";
BufferedReader file=new BufferedReader(new FileReader(szPath));
try{
szRecord=file.readLine();
}
catch(IOException e){
System.out.println("文件未找到!");
}
//顯示7位數(shù)字gif圖像
String szOut="<body topmargin='0' leftmargin='0'>";
int i=0;
int k=7-szRecord.length(); //"0"的個數(shù)
for (i=0;i<k-1;i++){ //顯示"0"
szOut=szOut+"<img src='images/0.gif'>";
}
for (i=0;i<szRecord.length();i++){ //顯示非"0"
szOut=szOut+"<img src='images/"+ szRecord.charAt(i) +".gif'>";
}
szOut=szOut+"</body>";
out.println(szOut);
%>
</html>