JSP页面不能刷新、防止刷新的问题总结

发布时间:2007年04月24日      浏览次数:3051 次
<1>如果要求当发生选择、删除、修改等操作时,表单刷新,需要加上: window.returnValue=0.
例如leadership\leader_ctl.jsp
<body leftmargin="0" topmargin="0" >
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="leader.*" %>
<%
LeaderBean leader=new LeaderBean();
String strUserID=request.getParameter("Key");
session.setAttribute("EmployUserID",strUserID);
//System.out.println("strUserID:"+strUserID);
if (leader.PostEdit(request)){
//System.out.println("me5");
out.println("<script>window.returnValue=0;window.close();</script>");
return;
}
else{
if (request.getParameter("Oper")!=null){
if (request.getParameter("Oper").equals("delLeader")){
out.println("<script>alert('"+leader.LastOperMsg+"');window.returnValue=0;</script>");
//return;
}
if(request.getParameter("Oper").equals("setLeader")){
//System.out.println("me3");
out.println("<script>alert('"+leader.LastOperMsg+"');window.returnValue=0;window.close();</script>");
//System.out.println("me4");
//return;
}
if(request.getParameter("Oper").equals("delCompetence")){
////System.out.println("strUserID:"+strUserID);
out.println("<script>alert('"+leader.LastOperMsg+"');history.back();</script>");
//System.out.println("me4");
//return;
}
if(request.getParameter("Oper").equals("addCompetence")){
//System.out.println("addCompetence1");
out.println("<script>alert('"+leader.LastOperMsg+"');history.back();</script>");
//System.out.println("addCompetence2");
//return;
}
else{
if (request.getParameter("Oper").equals("qx")){
out.println("<script>alert('"+leader.LastOperMsg+"');window.returnValue=0;window.close();</script>");
//return;
}
else{
//System.out.println("me6");
out.println("<script>alert('"+leader.LastOperMsg+"');window.close();</script>");
//return;
}//3e
}
}
}
%>

<2>如果要求每隔一段时间从服务器上自动刷新,需要加上:
(1) 种方法:
<%response.setHeader("Refresh","15");%>
15妙刷新一次
(2) 种方法:
自动刷新网页
在HTML的与之间加入下面这段代码,则在5分钟之后正在浏览的页面将会自动变为target.html这一页。代码中300为刷新的延迟时间,以秒为单位。targer.html为你想转向的目标页,若为本页则为自动刷新本页。
<meta http-equiv="refresh" content="300; url=target.html">

(3) 种方法:演示效果:本页将在规定的时间自动刷新,代码提示:将下面的代码复制到〈head〉区
<script>
<!--
var limit="0:15"//修改刷新时间
if (document.images){
var parselimit=limit.split(":")
parselimit=parselimit[0]*60+parselimit[1]*1
}
function beginrefresh(){
if (!document.images)
return
if (parselimit==1)
window.location.reload()
else{
parselimit-=1
curmin=Math.floor(parselimit/60)
cursec=parselimit%60
if (curmin!=0)
curtime=curmin+"分"+cursec+"秒后重刷本页!"
else
curtime=cursec+"秒后重刷本页!"
window.status=curtime
setTimeout("beginrefresh()",1000)
}
}
window.onload=beginrefresh
//-->
</script>
<2>防止从服务器上自动刷新,需要加上:
<%@ 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>计数器</title></head>
<%!//同步更新计数器
synchronized void counter()
{
ServletContext application=getServletContext();
//构造application对象(可选)
String szPath=application.getRealPath("/");
//得到当前路径
szPath=szPath+"hits.txt";
//计数器文件 0-9999999999999...
方案2
jJsp计数器,同一电脑10秒内防刷新
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" http-equiv="refresh" >
<title>无标题文档</title>
<link href="css/counter.css" rel="stylesheet" type="text/css">
</head>
<body bgcolor="#FFFFFF">
<!--start counter -->
<div align="center">
<%@ page import="com.ckj.counter.*,java.io.*,java.util.Date"%>
<%Counter ct=new Counter();
String count="0";
String counterid="0";
try{
// String strDirPath = new File(application.getRealPath(request.getRequestURI())).getParent();
//System.out.println("目录的绝对路径:" + strDirPath + "<br/>");
String strFullPath = session.getServletContext().getRealPath("/");
System.out.println(strFullPath);
ct.path=strFullPath+"/counter/count.txt";
//1 session存储时间值,
//2 下次比较一下
if(session.getAttribute("counttime")==null ){ //如果 初次访问
count=ct.doCount();
// System.out.println("--------计算前11--------"+System.currentTimeMillis());
session.setAttribute("counttime",String.valueOf(System.currentTimeMillis()));}
else
{ //如果,刷新间隔超过
//System.out.println("--------刷新之差--------"+(System.currentTimeMillis()- Long.valueOf((String)session.getAttribute("counttime")).longValue()));
//大于10秒
if((System.currentTimeMillis()-Long.valueOf((String)session.getAttribute("counttime")).longValue())>9000) {
//System.out.println("--------计算后--------"+System.currentTimeMillis());
count=ct.doCount();
//session.removeAttribute("counttime");
session.setAttribute("counttime",String.valueOf(System.currentTimeMillis()));
}
//小于10秒
else
count=ct.getCount();
}
}
catch(Exception e)
{
}
//如果10秒内刷新,不计数
if(true){
}
%>
你是本站第 <span class="font12bgred" > <%=count%> </span> 位访问者!
<!-- end counter -->
</div>
</body>
</html>

方案3、防刷新图形计数器(已调试)
实例6:防刷新图形计数器(已调试)程序说明:上次做的计数器没有采用对文件的读写操作,虽然用到了javabean但是并没有用到scope中的几个参数:appliction,page,session这些参数分别代表了bean的存活的周期,appliction>session>page我们利用这个原理来防止用户刷新从而提高访问次数的漏洞。程序准备:如果你使用的是apache+resin那么请你在resin的根目录下建立一个文本文件counter.txt并在里面任意写一个数字,比如100之类的。其次,请建立一个文件夹用来保存这个程序的所有文件,并在该文件夹里建立一个子目录images,用来保存10张图片,图片格式为gif,图片名称从0--9,图片内容就是0--9十个数字,分别与图片名称对应就可了。程序文件:index.jsp, addone.java, display.javaindex.jsp用来显示纪录结果。addone.java 是一个javabean用来写纪录到文件display.java 也是一个javabean用来读取纪录到index.jsp程序源码:index.jsp
<%@ page import="popeyelin.*" contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
<title>JSP图形防刷新计数器</title>
</head>
<body>
<jsp:useBean id="a" scope="page" class="popeyelin.addone"/>
<jsp:useBean id="b" scope="page" class="popeyelin.display"/>
已经有
<% b.counter();
for(int i=9;i>=0;i--) out.print(b.img[i]);
%>
个人访问这个页面
</body>
</html> addone.java package popeyelin;
import java.io.*;
import java.lang.*;
public class addone{
private String s=new String(); //建立数组变量sl
public void addone(){
try {
BufferedReader buff=new BufferedReader(new FileReader("conuter.txt"));
String s=buff.readLine();
int i=Integer.parseInt(s); //将字符串变量s转化成整形
i++;
buff.close();//关闭对象
PrintWriter pw=new PrintWriter(new FileWriter("oounter.txt"));
String temp=Integer.toString(i);//将整形变量i转化成字符型
pw.print(temp);
pw.close();
}
catch(IOException e){
System.out.println(e.toString());
}
}
} display.java
package popeyelin;
import java.io.*;
import java.lang.*;
public class display{
public String[]img=new String[10];
public void counter(){
try{
BufferedReader buff=new BufferedReader(new FileReader("counter.txt"));
String s=buff.readLine();
int i=Integer.parseInt(s);
int st=10;
int j=0;
while(j<=9) {
img[j]=Integer.toString(i%st);
img[j]=img[j]+".GIF";
img[j]="images/"+img[j];
img[j]="<img src="+img[j]+">";
img[j]=img[j]+"</img>";
i/=10;
j++;
}
}
catch(IOException e){
System.out.println(e.toString());
}
}
}
编译addone.java和display.java后会生成一个文件夹,popeyelin,把这个文件夹拷贝到WEB-INF/class目录下,如果不存在,请手动建立。运行index.jsp你就可以看到这个图片计数器了程序分析:重点就在于对文件的读写,我们看如下代码
BufferedReader buff=new BufferedReader(new FileReader("conuter.txt"));
String s=buff.readLine();
int i=Integer.parseInt(s); //将字符串变量s转化成整形
i++;
buff.close();//关闭对象
PrintWriter pw=new PrintWriter(new FileWriter("oounter.txt"));
String temp=Integer.toString(i);//将整形变量i转化成字符型
pw.print(temp);
pw.close();
我们如果要对文件进行读写操作,就必须先后建立2个对象,来对文件分别进行读和写而且要注意,我们从文件里读出来的东西是字符串型的,如果我们要对他进行修改必须先转换成int型,要用到 integer.parseint(),如果我们要先东西到文件,同样要先将int转换成string用integer.tostring(),写文件用到pw.print()要写的内容可以保存在变量里,如:pw.print(temp),也可以直接写,如:pw.print("hello,world");记住,写完一定要关毕对象。pw.close()
方案4、用JSP制作页面防刷新计数器(PHP)
<body>
<%@ page import="java.io.*" %>
<%
String currentRecord = null;//保存文本的变量
BufferedReader file; //BufferedReader对象,用于读取文件数据
String nameOfTextFile = "e:\\count.txt";
//读取
file = new BufferedReader(new FileReader(nameOfTextFile));
String readStr =null;
int writeStr =0; //如果计数文本中的计数值为空则让它显示时变成1并写入
try { readStr = file.readLine(); }
catch (IOException e)
{ System.out.println("读取数据错误."); }
if (readStr == null) readStr = "没有任何记录";
//判断cookie,第一次登陆时加1,刷新时不累计计数
else if (request.getHeader("Cookie")==null)
{ writeStr = Integer.parseInt(readStr)+1;}
else { writeStr = Integer.parseInt(readStr);}
//写入时控制因为刷新引起的重复计数
if (request.getHeader("Cookie")==null) {
try {
PrintWriter pw = new PrintWriter(new FileOutputStream(nameOfTextFile));
pw.println(writeStr);
pw.close();}
catch(IOException e) {
out.println(e.getMessage());}
}
%>
<p align="center">您是傲雪寒梅网站的第<b><font color="red"><%=writeStr%></font></b>位客人。</p>
</body>
</html>
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!