当前位置: 技术问答>java相关
求助:怎样在JSP页面中加每个新连接加一次的计数器?
来源: 互联网 发布时间:2015-04-14
本文导语: 我编了一个JavaBeans: package counter; import java.io.*; public class counter extends Object{ public String path=""; public String doCount() throws FileNotFoundException{ BufferedReader file; String countFile=path; fi...
我编了一个JavaBeans:
package counter;
import java.io.*;
public class counter extends Object{
public String path="";
public String doCount() throws FileNotFoundException{
BufferedReader file;
String countFile=path;
file=new BufferedReader(new FileReader(countFile));
String readStr="";
int writeStr=1;
try{
readStr=file.readLine();
}catch(IOException e)
{
System.out.println("Read Data Error!");
}
if(readStr=="")
readStr="No Record";
else
{
// writeStr=234;
writeStr=Integer.parseInt(readStr)+1;
}
try{
PrintWriter pw=new PrintWriter(new FileOutputStream(countFile));
pw.println(writeStr);
pw.close();
}catch(IOException e)
{
System.out.println("Write Error!");
}
return readStr;
}
}
然后在JSP主页中用
你是本网站的第位访客!
package counter;
import java.io.*;
public class counter extends Object{
public String path="";
public String doCount() throws FileNotFoundException{
BufferedReader file;
String countFile=path;
file=new BufferedReader(new FileReader(countFile));
String readStr="";
int writeStr=1;
try{
readStr=file.readLine();
}catch(IOException e)
{
System.out.println("Read Data Error!");
}
if(readStr=="")
readStr="No Record";
else
{
// writeStr=234;
writeStr=Integer.parseInt(readStr)+1;
}
try{
PrintWriter pw=new PrintWriter(new FileOutputStream(countFile));
pw.println(writeStr);
pw.close();
}catch(IOException e)
{
System.out.println("Write Error!");
}
return readStr;
}
}
然后在JSP主页中用
你是本网站的第位访客!
我得目的是想让每一个新连接计数器加一次,但是现在得结果是只要回到主页一次,计数器就加一次,让我好不烦恼。哪个高手指教一下:该怎样实现我得目的?
|
你在session中加入一个标记,在第一次访问时建立这个标记,此后检测这个标记,如果存在就不再让counter增长,这样就能控制了。我就是这么做的。