• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
小不点儿大的由我做主!
流年似水!
博客园    首页    新随笔    联系   管理    订阅  订阅

无刷新面页的验证码

这两天搞了好久的这个,在网上下了好多资料,最后还是搞出来了。有好多是没有写验证码刷新的(也可能是我搜的不好!),最后找到一个,改了改程序就可以用了,下面把原码发到下面。对了,我的验证码图版是用JSP写的哦,有的人是用servlet写的,那个还要在WEB.XML中配置,有点麻烦!

生成图片的JSP文件:img.jsp

Code
<%@ page autoFlush="false"   import="java.util.*,java.awt.*,java.awt.image.*,com.sun.image.codec.jpeg.*,java.util.*" %>

<%
//set Chinese Char 
//Cody by JarryLi@gmail.com;
//homepage:jiarry.126.com
request.setCharacterEncoding(
"GBK");
response.setCharacterEncoding(
"GBK");
response.setContentType(
"text/html; charset=GBK");
%>
<%
String chose="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

char display[]
={'0',' ','0',' ','0',' ','0'},ran[]={'0','0','0','0'},temp;

Random rand
=new Random();

for(int i=0;i<4;i++)
{

temp
=chose.charAt(rand.nextInt(chose.length()));

display[i
*2]=temp;

ran[i]
=temp;
}

String random=String.valueOf(display);

session.setAttribute(
"random",String.valueOf(ran));
%>
<%

         out.clear();
         response.setContentType(
"image/jpeg");
         response.addHeader(
"pragma","NO-cache");
         response.addHeader(
"Cache-Control","no-cache");
         response.addDateHeader(
"Expries",0);
         
int width=80, height=30;
         BufferedImage image 
= new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
         Graphics g 
= image.getGraphics();
         
//以下填充背景颜色
         g.setColor(Color.yellow);
         g.fillRect(
0, 0, width, height);
        
//设置字体颜色
         g.setColor(Color.blue);
         Font font
=new Font("Arial",Font.PLAIN,20);
         g.setFont(font);
         
//g.drawString(random,5,14);
         g.drawString(random,
5,20);
         g.dispose();
         ServletOutputStream outStream 
= response.getOutputStream();
         JPEGImageEncoder encoder 
=JPEGCodec.createJPEGEncoder(outStream);
         encoder.encode(image);
         outStream.close();
    

登录面页:login.jsp

Code
<%@ page contentType="text/html;charset=gb2312" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>认证码输入页面</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache"> 
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache"> 
<META HTTP-EQUIV="Expires" CONTENT="0"> 
</head>
<body>
<form method=post action="check.jsp">
<table>
<tr>
<td align=left>系统产生的认证码:</td>
<td><img border=0 src="img.jsp" id="code"></td>
</tr>
<tr>
<td align=left>输入上面的认证码:</td>
<td><input type=text name=rand maxlength=4 value=""></td>
<td>
<script type="text/javascript"> 
function show(o){
//重载验证码
var timenow = new Date().getTime();
o.src
="img.jsp?d="+timenow;
//超时执行;
setTimeout(function(){
   o.src
="img.jsp?d="+timenow;
}
   ,
20);

}

</script>

<a href="javascript:show(document.getElementById('code'))">刷新</a></td>
</tr>
<tr>
<td colspan=2 align=center><input type=submit value="提交检测"></td>
</tr>
</form>
</body>
</html

检测页面:check.jsp

Code
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<title>认证码验证页面</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache"> 
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache"> 
<META HTTP-EQUIV="Expires" CONTENT="0"> 
</head>

<body>
<% 
String rand = (String)session.getAttribute("random");
String input = request.getParameter("rand");
%>
系统产生的认证码为: 
<%= rand %><br>
您输入的认证码为: 
<%= input %><br>
<br>
<%
if (rand.equals(input)) {
%>
<font color=green>输入相同,认证成功!</font>
<%
} 
else {
%>
<font color=red>输入不同,认证失败!</font>
<%
}
%>
</body>
</html

这个代码我测试过了,可以用的哦!生成的是随机的字母。


posted @ 2008-04-02 17:05  小不点儿大的由我做主!  阅读(179)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3