|
常用链接
留言簿(2)
随笔档案(5)
文章档案(2)
搜索
最新评论
阅读排行榜
评论排行榜
Powered by: 博客园
模板提供:沪江博客
|
|
|
|
|
发新文章 |
|
|
1
2
JSP页面不能刷新、防止刷新的问题总结
3
添加时间:
2007
-
4
-
22
4
5
<
1
>
如果要求当发生选择、删除、修改等操作时,表单刷新,需要加上: window.returnValue
=
0
.
6
7
例如leadership\leader_ctl.jsp
8
9
<
body leftmargin
=
"
0
"
topmargin
=
"
0
"
>
10
<%
@ page contentType
=
"
text/html; charset=gb2312
"
language
=
"
java
"
import
=
"
java.sql.*
"
errorPage
=
""
%>
11
<%
@ page
import
=
"
leader.*
"
%>
12
<%
13
LeaderBean leader
=
new
LeaderBean();
14
String strUserID
=
request.getParameter(
"
Key
"
);
15
session.setAttribute(
"
EmployUserID
"
,strUserID);
16
//
System.out.println("strUserID:"+strUserID);
17
if
(leader.PostEdit(request))
{
18
//
System.out.println("me5");
19
out.println(
"
<script>window.returnValue=0;window.close();</script>
"
);
20
return
;
21
}
22
else
{
23
if
(request.getParameter(
"
Oper
"
)
!=
null
)
{
24
if
(request.getParameter(
"
Oper
"
).equals(
"
delLeader
"
))
{
25
out.println(
"
<script>alert('
"
+
leader.LastOperMsg
+
"
');window.returnValue=0;</script>
"
);
26
//
return;
27
}
28
if
(request.getParameter(
"
Oper
"
).equals(
"
setLeader
"
))
{
29
//
System.out.println("me3");
30
out.println(
"
<script>alert('
"
+
leader.LastOperMsg
+
"
');window.returnValue=0;window.close();</script>
"
);
31
//
System.out.println("me4");
32
//
return;
33
}
34
if
(request.getParameter(
"
Oper
"
).equals(
"
delCompetence
"
))
{
35
////
System.out.println("strUserID:"+strUserID);
36
out.println(
"
<script>alert('
"
+
leader.LastOperMsg
+
"
');history.back();</script>
"
);
37
//
System.out.println("me4");
38
//
return;
39
}
40
if
(request.getParameter(
"
Oper
"
).equals(
"
addCompetence
"
))
{
41
//
System.out.println("addCompetence1");
42
out.println(
"
<script>alert('
"
+
leader.LastOperMsg
+
"
');history.back();</script>
"
);
43
//
System.out.println("addCompetence2");
44
//
return;
45
}
46
else
{
47
if
(request.getParameter(
"
Oper
"
).equals(
"
qx
"
))
{
48
out.println(
"
<script>alert('
"
+
leader.LastOperMsg
+
"
');window.returnValue=0;window.close();</script>
"
);
49
//
return;
50
}
51
else
{
52
//
System.out.println("me6");
53
out.println(
"
<script>alert('
"
+
leader.LastOperMsg
+
"
');window.close();</script>
"
);
54
//
return;
55
}
//
3e
56
}
57
}
58
}
59
%>
60
61
<
2
>
如果要求每隔一段时间从服务器上自动刷新,需要加上:
62
63
(
1
) 种方法:
64
65
<%
response.setHeader(
"
Refresh
"
,
"
15
"
);
%>
66
67
15妙刷新一次
68
69
(
2
) 种方法:
70
71
自动刷新网页
72
在HTML的与之间加入下面这段代码,则在5分钟之后正在浏览的页面将会自动变为target.html这一页。代码中300为刷新的延迟时间,以秒为单位。targer.html为你想转向的目标页,若为本页则为自动刷新本页。
73
74
<
meta http
-
equiv
=
"
refresh
"
content
=
"
300; url=target.html
"
>
75
76
(
3
) 种方法:演示效果:本页将在规定的时间自动刷新,代码提示:将下面的代码复制到〈head〉区
77
78
<
script
>
79
<!--
80
var limit
=
"
0:15
"
//
修改刷新时间
81
if
(document.images)
{
82
var parselimit
=
limit.split(
"
:
"
)
83
parselimit
=
parselimit[
0
]
*
60
+
parselimit[
1
]
*
1
84
}
85
function beginrefresh()
{
86
if
(
!
document.images)
87
return
88
if
(parselimit
==
1
)
89
window.location.reload()
90
else
{
91
parselimit
-=
1
92
curmin
=
Math.floor(parselimit
/
60
)
93
cursec
=
parselimit
%
60
94
if
(curmin
!=
0
)
95
curtime
=
curmin
+
"
分
"
+
cursec
+
"
秒后重刷本页!
"
96
else
97
curtime
=
cursec
+
"
秒后重刷本页!
"
98
window.status
=
curtime
99
setTimeout(
"
beginrefresh()
"
,
1000
)
100
}
101
}
102
window.onload
=
beginrefresh
103
//
-->
104
</
script
>
105
106
<
2
>
防止从服务器上自动刷新,需要加上:
107
108
<%
@ page contentType
=
"
text/html;charset=gb2312
"
%><%
@ page language
=
"
java
"
import
=
"
java.io.*
"
%>
109
<
html
>
110
<
head
>
111
<
meta http
-
equiv
=
"
Content-Type
"
content
=
"
text/html; charset=gb2312
"
>
112
<
title
>
计数器
</
title
></
head
>
113
<%!
//
同步更新计数器
114
synchronized
void
counter()
115
{
116
ServletContext application
=
getServletContext();
117
//
构造application对象(可选)
118
String szPath
=
application.getRealPath(
"
/
"
);
119
//
得到当前路径
120
szPath
=
szPath
+
"
hits.txt
"
;
121
//
计数器文件 0-9999999999999
122
123
方案2
124
125
jJsp计数器,同一电脑10秒内防刷新
126
127
<%
@ page contentType
=
"
text/html; charset=gb2312
"
language
=
"
java
"
import
=
"
java.sql.*
"
errorPage
=
""
%>
128
<!
DOCTYPE HTML PUBLIC
"
-//W3C//DTD HTML 4.01 Transitional//EN
"
"
http://www.w3.org/TR/html4/loose.dtd
"
>
129
<
html
>
130
<
head
>
131
<
meta http
-
equiv
=
"
Content-Type
"
content
=
"
text/html; charset=gb2312
"
>
132
<
meta name
=
"
GENERATOR
"
http
-
equiv
=
"
refresh
"
>
133
<
title
>
无标题文档
</
title
>
134
<
link href
=
"
css/counter.css
"
temp_href
=
"
css/counter.css
"
rel
=
"
stylesheet
"
type
=
"
text/css
"
>
135
</
head
>
136
<
body bgcolor
=
"
#FFFFFF
"
>
137
<!--
start counter
-->
138
<
div align
=
"
center
"
>
139
<%
@ page
import
=
"
com.ckj.counter.*,java.io.*,java.util.Date
"
%>
140
<%
Counter ct
=
new
Counter();
141
String count
=
"
0
"
;
142
String counterid
=
"
0
"
;
143
try
{
144
//
String strDirPath = new File(application.getRealPath(request.getRequestURI())).getParent();
145
//
System.out.println("目录的绝对路径:" + strDirPath + "<br/>");
146
String strFullPath
=
session.getServletContext().getRealPath(
"
/
"
);
147
System.out.println(strFullPath);
148
ct.path
=
strFullPath
+
"
/counter/count.txt
"
;
149
//
1 session存储时间值,
150
//
2 下次比较一下
151
if
(session.getAttribute(
"
counttime
"
)
==
null
)
{
//
如果 初次访问
152
count
=
ct.doCount();
153
//
System.out.println("--------计算前11--------"+System.currentTimeMillis());
154
session.setAttribute(
"
counttime
"
,String.valueOf(System.currentTimeMillis()));}
155
else
156
{
//
如果,刷新间隔超过
157
//
System.out.println("--------刷新之差--------"+(System.currentTimeMillis()- Long.valueOf((String)session.getAttribute("counttime")).longValue()));
158
//
大于10秒
159
if
((System.currentTimeMillis()
-
Long.valueOf((String)session.getAttribute(
"
counttime
"
)).longValue())
>
9000
)
{
160
//
System.out.println("--------计算后--------"+System.currentTimeMillis());
161
count
=
ct.doCount();
162
//
session.removeAttribute("counttime");
163
session.setAttribute(
"
counttime
"
,String.valueOf(System.currentTimeMillis()));
164
}
165
//
小于10秒
166
else
167
count
=
ct.getCount();
168
}
169
}
170
catch
(Exception e)
171
{
172
}
173
//
如果10秒内刷新,不计数
174
if
(
true
)
{
175
}
176
%>
177
你是本站第
<
span
class
=
"
font12bgred
"
>&
nbsp;
<%=
count
%>&
nbsp;
</
span
>
位访问者!
178
<!--
end counter
-->
179
</
div
>
180
</
body
>
181
</
html
>
182
183
方案3、防刷新图形计数器(已调试)
184
185
实例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
186
187
<%
@ page
import
=
"
popeyelin.*
"
contentType
=
"
text/html; charset=gb2312
"
language
=
"
java
"
%>
188
<
html
>
189
<
head
>
190
<
title
>
JSP图形防刷新计数器
</
title
>
191
</
head
>
192
<
body
>
193
<
jsp:useBean id
=
"
a
"
scope
=
"
page
"
class
=
"
popeyelin.addone
"
/>
194
<
jsp:useBean id
=
"
b
"
scope
=
"
page
"
class
=
"
popeyelin.display
"
/>
195
已经有
196
<%
b.counter();
197
for
(
int
i
=
9
;i
>=
0
;i
--
) out.print(b.img[i]);
198
%>
199
个人访问这个页面
200
</
body
>
201
</
html
>
addone.java
package
popeyelin;
202
import
java.io.
*
;
203
import
java.lang.
*
;
204
public
class
addone
{
205
private
String s
=
new
String();
//
建立数组变量sl
206
public
void
addone()
{
207
try
{
208
BufferedReader buff
=
new
BufferedReader(
new
FileReader(
"
conuter.txt
"
));
209
String s
=
buff.readLine();
210
int
i
=
Integer.parseInt(s);
//
将字符串变量s转化成整形
211
i
++
;
212
buff.close();
//
关闭对象
213
PrintWriter pw
=
new
PrintWriter(
new
FileWriter(
"
oounter.txt
"
));
214
String temp
=
Integer.toString(i);
//
将整形变量i转化成字符型
215
pw.print(temp);
216
pw.close();
217
}
218
catch
(IOException e)
{
219
System.out.println(e.toString());
220
}
221
}
222
}
display.java
223
package
popeyelin;
224
import
java.io.
*
;
225
import
java.lang.
*
;
226
public
class
display
{
227
public
String[]img
=
new
String[
10
];
228
public
void
counter()
{
229
try
{
230
BufferedReader buff
=
new
BufferedReader(
new
FileReader(
"
counter.txt
"
));
231
String s
=
buff.readLine();
232
int
i
=
Integer.parseInt(s);
233
int
st
=
10
;
234
int
j
=
0
;
235
while
(j
<=
9
)
{
236
img[j]
=
Integer.toString(i
%
st);
237
img[j]
=
img[j]
+
"
.GIF
"
;
238
img[j]
=
"
images/
"
+
img[j];
239
img[j]
=
"
<img src=
"
+
img[j]
+
"
temp_src=
"
+
img[j]
+
"
>
"
;
240
img[j]
=
img[j]
+
"
</img>
"
;
241
i
/=
10
;
242
j
++
;
243
}
244
}
245
catch
(IOException e)
{
246
System.out.println(e.toString());
247
}
248
}
249
}
250
251
编译addone.java和display.java后会生成一个文件夹,popeyelin,把这个文件夹拷贝到WEB
-
INF
/
class
目录下,如果不存在,请手动建立。运行index.jsp你就可以看到这个图片计数器了程序分析:重点就在于对文件的读写,我们看如下代码
252
253
BufferedReader buff
=
new
BufferedReader(
new
FileReader(
"
conuter.txt
"
));
254
String s
=
buff.readLine();
255
int
i
=
Integer.parseInt(s);
//
将字符串变量s转化成整形
256
i
++
;
257
buff.close();
//
关闭对象
258
PrintWriter pw
=
new
PrintWriter(
new
FileWriter(
"
oounter.txt
"
));
259
String temp
=
Integer.toString(i);
//
将整形变量i转化成字符型
260
pw.print(temp);
261
pw.close();
262
263
我们如果要对文件进行读写操作,就必须先后建立2个对象,来对文件分别进行读和写而且要注意,我们从文件里读出来的东西是字符串型的,如果我们要对 他进行修改必须先转换成int型,要用到 integer.parseint(),如果我们要先东西到文件,同样要先将int转换成string用integer.tostring(),写文件用 到pw.print()要写的内容可以保存在变量里,如:pw.print(temp),也可以直接写,如:pw.print(
"
hello, world
"
);记住,写完一定要关毕对象。pw.close()
264
265
方案4、用JSP制作页面防刷新计数器(PHP)
266
267
<
body
>
268
<%
@ page
import
=
"
java.io.*
"
%>
269
<%
270
String currentRecord
=
null
;
//
保存文本的变量
271
BufferedReader file;
//
BufferedReader对象,用于读取文件数据
272
String nameOfTextFile
=
"
e:\\count.txt
"
;
273
//
读取
274
file
=
new
BufferedReader(
new
FileReader(nameOfTextFile));
275
String readStr
=
null
;
276
int
writeStr
=
0
;
//
如果计数文本中的计数值为空则让它显示时变成1并写入
277
try
{ readStr
=
file.readLine(); }
278
catch
(IOException e)
279
{ System.out.println(
"
读取数据错误.
"
); }
280
if
(readStr
==
null
) readStr
=
"
没有任何记录
"
;
281
//
判断cookie,第一次登陆时加1,刷新时不累计计数
282
else
if
(request.getHeader(
"
Cookie
"
)
==
null
)
283
{ writeStr
=
Integer.parseInt(readStr)
+
1
;}
284
else
{ writeStr
=
Integer.parseInt(readStr);}
285
//
写入时控制因为刷新引起的重复计数
286
if
(request.getHeader(
"
Cookie
"
)
==
null
)
{
287
try
{
288
PrintWriter pw
=
new
PrintWriter(
new
FileOutputStream(nameOfTextFile));
289
pw.println(writeStr);
290
pw.close();}
291
catch
(IOException e)
{
292
out.println(e.getMessage());}
293
}
294
%>
295
<
p align
=
"
center
"
>
您是傲雪寒梅网站的第
<
b
><
font color
=
"
red
"
><%=
writeStr
%></
font
></
b
>
位客人。
</
p
>
296
</
body
>
297
</
html
>
298
|
|