JSP彩色验证码

JSP彩色验证码

生成有4个随机数字和杂乱背景的图片,数字和背景颜色会改变,服务器端刷新(用history.go(-1)也会变)

产生验证码图片的文件image.jsp


Java代码

   1.
<%@ page contentType="image/jpeg" import="java.awt.*,  
   2. java.awt.image.*,java.util.*,javax.imageio.*" %>  
   3. <%!  
   4. Color getRandColor(int fc,int bc){//给定范围获得随机颜色  
   5.         Random random = new Random();  
   6.         if(fc>255) fc=255;  
   7.         if(bc>255) bc=255;  
   8.         int r=fc+random.nextInt(bc-fc);  
   9.         int g=fc+random.nextInt(bc-fc);  
  10.         int b=fc+random.nextInt(bc-fc);  
  11.         return new Color(r,g,b);  
  12.         }  
  13. %>  
  14. <%  

  15. //设置页面不缓存 
  16.
response.setHeader("Pragma","No-cache");  
  17. response.setHeader("Cache-Control","no-cache");  
  18. response.setDateHeader("Expires", 0); 

  19.  
  20. // 在内存中创建图象 
  21.
int width=60, height=20;  
  22. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 

  23.  
  2
4. // 获取图形上下文  
  25. Graphics g = image.getGraphics();  
  26.   
  27. //生成随机类  
  28. Random random = new Random();  
  29.   
  30. // 设定背景色  
  31. g.setColor(getRandColor(200,250));  
  32. g.fillRect(0, 0, width, height);  
  33.   
  34. //设定字体  
  35. g.setFont(new Font("Times New Roman",Font.PLAIN,18));  

  36.  
  37. //画边框 
  38.
 //g.setColor(new Color());  
  39. //g.drawRect(0,0,width-1,height-1);  
  40.   
  41. // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到  
  42. g.setColor(getRandColor(160,200));  
  43. for (int i=0;i<155;i++)  
  44. {  
  45.  int x = random.nextInt(width);  
  46.  int y = random.nextInt(height);  
  47.         int xl = random.nextInt(12);  
  48.         int yl = random.nextInt(12);  
  49.  g.drawLine(x,y,x+xl,y+yl);  
  50. }  

  51.  
  52. // 取随机产生的认证码(4位数字) 
 53. String sRand="";  
  54. for (int i=0;i<4;i++){  
  55.     String rand=String.valueOf(random.nextInt(10));  
  56.     sRand+=rand;  
  57.     // 将认证码显示到图象中  
  58.     g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));  
  59. //调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成  
  60.     g.drawString(rand,13*i+6,16);  
  61. }  
  62.   
  63. // 将认证码存入session  
  64. session.setAttribute("rand",sRand);  
  65.   
  66. // 图象生效  
  67. g.dispose();  
  68.   
  69. // 输出图象到页面  
  70. ImageIO.write(image, "JPEG", response.getOutputStream());  
  71.   
  72.   
  73.   
  74. /** 
  75.  *这里是在释放在jsp中使用的对象,会调用response.getWriter(), 
  76.  *因为这个方法是和response.getOutputStream()相冲突的! 
  77.  * 
  78.  */  
  79. out.clear();  
  80. out = pageContext.pushBody();  
  81. %>   



使用验证码图片的文件input.jsp
Java代码

   1.
<%@ page contentType="text/html;charset=gb2312" %>  
   2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
   3. <html>  
   4. <head>  
   5. <title>认证码输入页面</title>  
   6. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">  
   7. <META HTTP-EQUIV="Pragma" CONTENT="no-cache">   
   8. <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">   
   9. <META HTTP-EQUIV="Expires" CONTENT="0">   
  10. </head>  
  11. <body>  
  12. <form method=post action="check.jsp">  
  13. <table>  
  14. <tr>  
  15. <td align=left>系统产生的认证码:</td>  
  16. <td><img border=0 src="image.jsp"></td>  
  17. </tr>  
  18. <tr>  
  19. <td align=left>输入上面的认证码:</td>  
  20. <td><input type=text name=rand maxlength=4 value=""></td>  
  21. </tr>  
  22. <tr>  
  23. <td colspan=2 align=center><input type=submit value="提交检测"></td>  
  24. </tr>  
  25. </form>  
  26. </body>  
  27. </html>   




验证的页面check.jsp
Java代码
   1. <%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>  
   2. <html>  
   3. <head>  
   4. <title>认证码验证页面</title>  
   5. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">  
   6. <META HTTP-EQUIV="Pragma" CONTENT="no-cache">   
   7. <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">   
   8. <META HTTP-EQUIV="Expires" CONTENT="0">   
   9. </head>  
  10.   
  11. <body>  
  12. <%   
  13.  String rand = (String)session.getAttribute("rand");  
  14.  String input = request.getParameter("rand");  
  15. %>  
  16. 系统产生的认证码为: <%= rand %><br>  
  17. 您输入的认证码为: <%= input %><br>  
  18. <br>  
  19. <%  
  20.   if (rand.equals(input)) {  
  21. %>  
  22. <font color=green>输入相同,认证成功!</font>  
  23. <%  
  24.   } else {  
  25. %>  
  26. <font color=red>输入不同,认证失败!</font>  
  27. <%  
  28.   }  
  29. %>  
  30. </body>  
  31. </html>   

你可能感兴趣的:(java,html,sql,jsp,cache)