点击验证码无刷新重新加载验证码图片

来源:互联网 发布:网络电话卡有哪些 编辑:程序博客网 时间:2024/05/22 02:23

点击验证码无刷新重新加载验证码图片

   生成验证码图片的image.jsp:

Java代码  收藏代码
  1. <%@page contentType="image/jpeg;charset=utf-8"%>  
  2. <%@page import="java.util.*,java.awt.*,java.awt.image.*,javax.imageio.*" %>  
  3. <%@ page import="java.io.OutputStream" %>  
  4. <html>  
  5.  <body>  
  6.   <%!   
  7.    Color getRandColor(int fc,int bc)  
  8.    {  
  9.     Random rd=new Random();  
  10.     if(fc>255) fc=255;  
  11.     if(bc>255) bc=255;  
  12.     int red=fc+rd.nextInt(bc-fc);  
  13.     int green=fc+rd.nextInt(bc-fc);  
  14.     int blue=fc+rd.nextInt(bc-fc);  
  15.     return new Color(red,green,blue);  
  16.    }  
  17.    %>  
  18.    <%   
  19.     Random r=new Random();  
  20.     response.addHeader("Pragma","No-cache");  
  21.     response.addHeader("Cache-Control","no-cache");  
  22.     response.addDateHeader("expires",0);     
  23.     int width=90;  
  24.     int height=23;  
  25.    BufferedImage pic=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);  
  26.    Graphics gc=pic.getGraphics();  
  27.    gc.setColor(getRandColor(200,250));  
  28.    gc.fillRect(0,0,width,height);  
  29.    String[] rNum ={"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f",  
  30.       "g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w",  
  31.       "x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N",  
  32.       "O","P","Q","R","S","T","U","V","W","X","Y","Z"};  
  33.    int[] style = {Font.PLAIN,Font.BOLD,Font.ITALIC,Font.PLAIN+Font.BOLD,  
  34.      Font.BOLD+Font.ITALIC,Font.PLAIN+Font.ITALIC,Font.PLAIN+Font.BOLD+Font.ITALIC};  
  35.    gc.setColor(Color.WHITE);  
  36.    gc.drawLine(0,30,90,10);  
  37.    gc.setColor(getRandColor(160,200));  
  38.    for (int i=0;i<50;i++)      
  39.    {  
  40.     int x = r.nextInt(width);      
  41.     int y = r.nextInt(height);      
  42.     int xl = r.nextInt(10);      
  43.     int yl = r.nextInt(10);      
  44.     gc.drawLine(x,y,x+xl,y+yl);      
  45.    }  
  46.    gc.setColor(getRandColor(60,150));  
  47.    String rt = "";  
  48.    for(int i=0;i<4;i++){  
  49.     String temp = rNum[r.nextInt(62)];  
  50.     rt = rt+temp;  
  51.     gc.setFont(new Font("Times New Roman",style[r.nextInt(7)],15));  
  52.     gc.drawString(temp,5+i*15+r.nextInt(10),10+r.nextInt(10));  
  53.    }  
  54.    gc.dispose();  
  55.    session.setAttribute("randNum",rt);     
  56.    OutputStream os=response.getOutputStream();  
  57.    ImageIO.write(pic,"JPEG",os);  
  58.    System.out.println("当前验证码为:"+session.getAttribute("randNum"));  
  59.    os.flush();  
  60.    os.close();  
  61.    os=null;  
  62.    response.flushBuffer();  
  63.    out.clear();  
  64.    out = pageContext.pushBody();  
  65.     %>  
  66.  </body>  
  67. </html>  

实现无刷新重新加载步骤: 

1.在需要使用验证码的网页文件头部加上js代码:

写道
<script language="JavaScript"> 
function reloadcode(){ 
var verify=document.getElementById('safecode'); 
verify.setAttribute('src','image.jsp?'+Math.random()); 
//这里必须加入随机数不然地址相同无法重新加载 

</script> 

 

2.然后再验证码图片里面写onclick呼出上面的函数重新加载:

  

写道
<img src="image.jsp" id="safecode" border="0" onclick="reloadcode()" 
style="cursor:hand;padding:2px 8px 0pt 3px;" /> 

3.两个文件在同一目录下,点击图片即可实现验证码重新加载

 

参考:http://zhidao.baidu.com/question/97875728.html
         http://www.itwis.com/html/programme/javascript/20080416/1296.html

0 0
原创粉丝点击