某html格式的主页,人家让我写个jsp格式的计数器,嵌在主页的iframe里,最好不要那种,刷新一次就加一,而是根据session或ip来加一.我自己实现了一个计数器,是基于session来加一的.但这样就来问题了,iframe无法把session的值传过来,所以表现在主页上,就是刷新一次计数加一了.不知高手们能帮帮我么?是好有详细的代码和说明.谢谢了!以下是我实现的代码,数字保存在count2.txt文件里:<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.io.*" %><html>
<body>
<%! int n=0;
String szRecord="";
File file=new File("count2.txt"); //此文件在tomcat/bin目录下
synchronized void count(){
if(!file.exists())
{ n++;
try{
file.createNewFile();
PrintWriter pw=new PrintWriter(new FileWriter(file));
pw.print(n); //将计数写入文件
pw.close();
}catch(IOException e){
e.printStackTrace();
}
}
else{
try{
BufferedReader br=new BufferedReader(new FileReader(file));
szRecord=br.readLine(); //从文件中读数
n=Integer.parseInt(szRecord)+1;
PrintWriter pw=new PrintWriter(new FileWriter(file));
pw.print(n); //加一后重新写入文件
pw.close();
}
catch(IOException e){
e.printStackTrace();
}
}
}
%>
<% if(session.isNew())
{ count();
String str=String.valueOf(n);
session.setAttribute("count",str); //??????
}
%>
<% int showNo=8; //表示实际在页面上显示的数位
int[] shuwei=new int[showNo];
int rest;
int i=n; int wei=0;
do{
rest=i%10;
shuwei[wei++]=rest;
i/=10;
}while(i>0);
if(wei<showNo)
for(int k=wei;k<showNo;k++)
shuwei[k]=0;
%>
<p><table ><tr><td>您是第</td><td><table cellspacing="0"><tr><%
for(int j=showNo-1;j>=0;j--){
int image=shuwei[j];
out.write("<td><img src=\"images/"+image+".gif\"></td>");
}
%></tr></table></td><td>个访问本站的人</td></tr></table>
</body>
</html>
<%@ page import="java.io.*" %><html>
<body>
<%! int n=0;
String szRecord="";
File file=new File("count2.txt"); //此文件在tomcat/bin目录下
synchronized void count(){
if(!file.exists())
{ n++;
try{
file.createNewFile();
PrintWriter pw=new PrintWriter(new FileWriter(file));
pw.print(n); //将计数写入文件
pw.close();
}catch(IOException e){
e.printStackTrace();
}
}
else{
try{
BufferedReader br=new BufferedReader(new FileReader(file));
szRecord=br.readLine(); //从文件中读数
n=Integer.parseInt(szRecord)+1;
PrintWriter pw=new PrintWriter(new FileWriter(file));
pw.print(n); //加一后重新写入文件
pw.close();
}
catch(IOException e){
e.printStackTrace();
}
}
}
%>
<% if(session.isNew())
{ count();
String str=String.valueOf(n);
session.setAttribute("count",str); //??????
}
%>
<% int showNo=8; //表示实际在页面上显示的数位
int[] shuwei=new int[showNo];
int rest;
int i=n; int wei=0;
do{
rest=i%10;
shuwei[wei++]=rest;
i/=10;
}while(i>0);
if(wei<showNo)
for(int k=wei;k<showNo;k++)
shuwei[k]=0;
%>
<p><table ><tr><td>您是第</td><td><table cellspacing="0"><tr><%
for(int j=showNo-1;j>=0;j--){
int image=shuwei[j];
out.write("<td><img src=\"images/"+image+".gif\"></td>");
}
%></tr></table></td><td>个访问本站的人</td></tr></table>
</body>
</html>
就是这个第几位访问的用户里面的几不是由Iframe自己计算的,而是由页面文件带过去的。
这样,即便刷洗也没有问题了。
就是在每次调用Iframe时,都会把这个值传递过去,而不是由Iframe文件生成?
这样,你便可以在主页面里面根据Session来判断,即便刷新Iframe也没有关系了。
就是说当前是第几位访客的判断不是在Iframe里进行,而只是从主页面文件中读取。
就是说当前是第几位访客的判断不是在Iframe里进行,而只是从主页面文件中读取。
你可以在登陆的时候计算
把计算结果放到session中保存.
而后就直接去session中保存的那个显示即可.