分类: Java
2006-03-11 21:40:45
Tags: jsp记数器[刷新不走字]嘎嘎! |
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page language="java" import="java.io.*"%>
<%!
步更新计数器
synchronized void counter(){
ServletContext application=getServletContext(); 造application对象(可选)
String szPath=application.getRealPath("/"); 到当前路径
szPath=szPath+"hits.txt"; 数器文件 0-9999999999999...
String szRecord=""; 数 String
int nRecord=0; 数 int
try{
BufferedReader file=new BufferedReader(new FileReader(szPath));
szRecord=file.readLine(); 取计数器文件
}
catch(IOException e){
e.printStackTrace(System.err);
}
if(szRecord==null) szRecord="0"; 果计数器文件为空
nRecord=java.lang.Integer.parseInt(szRecord)+1; 数器+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);
}
}
%>
<%
示计数器
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位数字gif图像
String szOut="