jsp验证码

 
在开发中验证码是比较常用到有效防止这种问题对某一个特定注册用户用特定程序暴力破解方式进行不断的登陆尝试的方式。
此演示程序包括三个文件:
1.index.jsp:登录页面
2.image.jsp:生成验证码图片页面
3.result.jsp:结果页面
 
【页面显示】
图片加载不成功
【页面代码】
1.index.jsp
 
xml 代码
  1. <html><body>      
  2. <form method=post action="result.jsp">      
  3. <input type=text name=input maxlength=4>      
  4. <img border=0 src="image.jsp">      
  5. <input type="submit" value="submit">      
  6. </form></body></html>  
[注意]:
(1)使用 maxlength属性来限制输入字符;
(2)使用< img>标签来显示生成的验证码图片.
 
2.image.jsp
  1. <%@ page contentType="image/JPEG"  
  2.     import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"  
  3.     pageEncoding="GBK"%>  
  4. <%!Color getRandColor(int fc, int bc) {//给定范围获得随机颜色   
  5.         Random random = new Random();   
  6.         if (fc > 255)   
  7.             fc = 255;   
  8.         if (bc > 255)   
  9.             bc = 255;   
  10.         int r = fc + random.nextInt(bc - fc);   
  11.         int g = fc + random.nextInt(bc - fc);   
  12.         int b = fc + random.nextInt(bc - fc);   
  13.         return new Color(r, g, b);   
  14.     }%>  
  15. <%   
  16.     //设置页面不缓存   
  17.     response.setHeader("Pragma", "No-cache");   
  18.     response.setHeader("Cache-Control", "no-cache");   
  19.     response.setDateHeader("Expires", 0);   
  20.   
  21.     // 在内存中创建图象   
  22.     int width = 60height = 20;   
  23.     BufferedImage image = new BufferedImage(width, height,   
  24.             BufferedImage.TYPE_INT_RGB);   
  25.   
  26.     // 获取图形上下文   
  27.     Graphics g = image.getGraphics();   
  28.   
  29.     //生成随机类   
  30.     Random random = new Random();   
  31.   
  32.     // 设定背景色   
  33.     g.setColor(getRandColor(200, 250));   
  34.     g.fillRect(0, 0, width, height);   
  35.   
  36.     //设定字体   
  37.     g.setFont(new Font("Times New Roman", Font.PLAIN, 18));   
  38.   
  39.     //画边框   
  40.     //g.setColor(new Color());   
  41.     //g.drawRect(0,0,width-1,height-1);   
  42.   
  43.     // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到   
  44.     g.setColor(getRandColor(160, 200));   
  45.     for (int i = 0; i < 100; i++) {   
  46.         int x = random.nextInt(width);   
  47.         int y = random.nextInt(height);   
  48.         int xl = random.nextInt(12);   
  49.         int yl = random.nextInt(12);   
  50.         g.drawLine(x, y, x + xl, y + yl);   
  51.     }   
  52.   

 

//数字的验证码。

<%!
Color getRandColor(int fc,int bc){
Random random = new Random();
if(fc>255) fc=255;
if(bc>255) bc=255;
int r=fc+random.nextInt(bc-fc);
int g=fc+random.nextInt(bc-fc);
int b=fc+random.nextInt(bc-fc);
return new Color(r,g,b);
}
%>
<%
try{
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
int width=60, height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
OutputStream os=response.getOutputStream();
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman",Font.PLAIN,18));
g.setColor(getRandColor(160,200));
for (int i=0;i<155;i++)
{
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x,y,x+xl,y+yl);
}
String sRand="";
for (int i=0;i<4;i++){
String rand=String.valueOf(random.nextInt(10));
sRand+=rand;
out.print("<script>document.getElementById('yanzmPic').value="+sRand+"</script>");
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
g.drawString(rand,13*i+6,16);
}
session.setAttribute("rand",sRand);
g.dispose();
ImageIO.write(image, "JPEG",os);
os.flush();
os.close();
os=null;
response.flushBuffer();
out.clear();
out = pageContext.pushBody();
}
catch(IllegalStateException e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}%>

  1.     // 取随机产生的认证码(4位数字)   
  2.     String sRand = "";   
  3.     for (int i = 0; i < 4; i++) {   
  4.         String rand = String.valueOf(random.nextInt(10));   
  5.         sRand += rand;   
  6.         // 将认证码显示到图象中   
  7.         g.setColor(new Color(20 + random.nextInt(110), 20 + random   
  8.         .nextInt(110), 20 + random.nextInt(110)));//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成   
  9.         g.drawString(rand, 13 * i + 6, 16);   
  10.     }   
  11.   
  12.     // 将认证码存入SESSION   
  13.     session.setAttribute("code", sRand);   
  14.   
  15.     // 图象生效   
  16.     g.dispose();   
  17.   
  18.     // 输出图象到页面   
  19.     ImageIO.write(image, "JPEG", response.getOutputStream());   
  20. %>  
[注意]:
(1)contentType值设置为"image/JPEG"
 
3.result.jsp
 
  < %@ page  language = "java"   import = "java.util.*"   pageEncoding = "GBK" % >   
  1. <html><body>  
  2. <%   
  3.     String input=request.getParameter("input");   
  4.     String code=(String)session.getAttribute("code");       
  5.     if(input.equals(code)){   
  6.         out.println("验证成功!");   
  7.     }else{   
  8.         out.println("验证失败!");   
  9.     }   
  10. %>  
  11. body>html>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值