日期:2014-05-17  浏览次数:20662 次

JS不刷新验证码
code.jsp

<%
//验证码边框的长高
int width = 60;
int height = 20;
//用RGB模式输出图像区域
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//定义画笔
Graphics graph = image.getGraphics();
//设置验证码框背景色0-255
graph.setColor(new Color(200, 200, 200));
//填充矩形
graph.fillRect(0, 0, width, height);
//产生1000-9999之间的随机数
Random rnd = new Random();
int rndNum = rnd.nextInt(8999) + 1000;
//此处为何转换为String型的用int型的效果一样?
String rndStr = String.valueOf(rndNum);
session.setAttribute("rndStr", rndNum);
//设置矩形区域中随机数及干扰点的颜色
graph.setColor(Color.RED);
//设置随机数的字体大小
graph.setFont(new Font("",Font.PLAIN,20));
//在已有的矩形区域中绘制随机数
graph.drawString(rndStr, 10, 17);
//随机产生100个干扰点
for (int i = 0; i < 100; i++)
{
int x = rnd.nextInt(width);
int y = rnd.nextInt(height);
//设置干扰点的位置长宽
graph.drawOval(x, y, 1, 1);
}
//将图像输出到页面上
ImageIO.write(image, "JPEG", response.getOutputStream());
//清空缓冲区
out.clear();
out = pageContext.pushBody();
%>

能够产生验证码.
刷新用的js


<script type="text/javascript">
function refresh(){
form1.image.src="code.jsp";
}
</script>


表单名为form1
用验证码的地方

<TR>
<TD style="HEIGHT: 28px" align=center> 
验证码 
</TD>
<TD style="HEIGHT: 28px">
<INPUT id=txtcode style="WIDTH: 130px" name=txtcode>
</TD>
<TD style="HEIGHT: 28px">
<img name="image" src="code.jsp" border="0"
onclick="refresh()" />
</TD>
</TR>


刷新页面能产生验证码,但点击验证码无法实现刷新。
------最佳解决方案--------------------
缓存吧,form1.image.src="code.jsp?random="+new Date().getTime();
------其他解决方案--------------------
引用:
引用:缓存吧,form1.image.src="code.jsp?random="+new Date().getTime();
谢谢诶 已经好了 加的这个时间是什么意思啊
防止地址相同,为了重新加载,用随机数也可以
------其他解决方案--------------------
引用:
缓存吧,form1.image.src="code.jsp?random="+new Date().getTime();

谢谢诶 已经好了 加的这个时间是什么意思啊