如何在jsp中有验证码


  1. ============servlet========================
  2. import java.awt.Color;  
  3. import java.awt.Font;  
  4. import java.awt.Graphics2D;  
  5. import java.awt.image.BufferedImage;  
  6. import java.io.IOException;  
  7. import java.util.Random;  
  8.   
  9. import javax.servlet.ServletException;  
  10. import javax.servlet.ServletOutputStream;  
  11. import javax.servlet.http.HttpServlet;  
  12. import javax.servlet.http.HttpServletRequest;  
  13. import javax.servlet.http.HttpServletResponse;  
  14.   
  15. import com.sun.image.codec.jpeg.JPEGCodec;  
  16. import com.sun.image.codec.jpeg.JPEGImageEncoder;  
  17.   
  18. public class IdentityServlet extends HttpServlet{  
  19.     public static final char[] CHARS={'2','3','4','5','6','7','8','9','A','B','C',  
  20.         'D','E','F','G','H','K','M','L','N','L','X','Y','Z','Q'};  
  21.     public static Random random=new Random();  
  22.       
  23.     public static String getRandomString(){  
  24.         StringBuffer buffer=new StringBuffer();  
  25.         //随即产生6位数  
  26.         for(int i=0;i<6;i++){  
  27.             buffer.append(CHARS[random.nextInt(CHARS.length)]);  
  28.         }  
  29.         System.out.println(buffer.toString());  
  30.         return buffer.toString();  
  31.     }  
  32.     //获取随即的颜色  
  33.     public static Color getRandomColor(){  
  34.         return new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255));  
  35.     }  
  36.     //返回某颜色的反色  
  37.     public static Color getReverseColor(Color c){  
  38.         return new Color(255-c.getRed(), 255-c.getGreen(),255-c.getBlue());  
  39.     }  
  40.       
  41.       
  42.     @Override  
  43.     protected void doGet(HttpServletRequest request, HttpServletResponse response)  
  44.             throws ServletException, IOException {  
  45.         response.setContentType("image/jpeg");  
  46.           
  47.         String randomString=getRandomString();  
  48.         request.getSession().setAttribute("randomString", randomString);  
  49.         int width=100;//图片宽度  
  50.         int height=30;//图片高度  
  51.           
  52.         Color color=getRandomColor();   //随即颜色。用于背景颜色  
  53.         Color reverse=getReverseColor(color);//反色,用于前背景  
  54.         //创建一个彩色图片  
  55.         BufferedImage bi=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
  56.         //获取绘图对象  
  57.         Graphics2D g=bi.createGraphics();  
  58.         g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));//设置字体  
  59.         g.setColor(color);  //设置颜色  
  60.         g.fillRect(00, width, height);//绘制背景  
  61.         g.setColor(reverse);//设置颜色  
  62.         g.drawString(randomString, 1820);  
  63.           
  64.         for(int i=0;i<100;i++){  
  65.             g.drawRect(random.nextInt(width), random.nextInt(height),11); //随即噪音点  
  66.         }  
  67.           
  68.         ServletOutputStream out=response.getOutputStream(); //转换JPEG格式  
  69.           
  70.         JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out); //编码器  
  71.         encoder.encode(bi);  
  72.         out.flush();    //输出到客户端  
  73.     }  
  74.       
  75.       
  76.     @Override  
  77.     protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
  78.             throws ServletException, IOException {  
  79.         // TODO Auto-generated method stub  
  80.         super.doPost(req, resp);  
  81.     }  


====================web.xml配置=======================


[html]   view plain copy
  1. <servlet>  
  2.         <servlet-name>IdentityServlet</servlet-name>  
  3.         <servlet-class>com.shxt.IdentityServlet</servlet-class>  
  4. </servlet>  
  5.   
  6. <servlet-mapping>  
  7.         <servlet-name>IdentityServlet</servlet-name>  
  8.         <url-pattern>/servlet/IdentityServlet</url-pattern>  
  9. </servlet-mapping> 


=======================jsp/html中(javascript)的代码=======================

[html]   view plain copy
  1.  <script type="text/javascript">  
  2. function reloadImage(){  
  3.     document.getElementById('btn').disabled=true;  
  4.     document.getElementById('identity').src='servlet/IdentityServlet?ts='+new Date().getTime();  
  5. }  
  6.   </script> 

=======================jsp/html中(body标签)代码===========================================


[html]   view plain copy
  1.  <body>  
  2. <img  src="servlet/IdentityServlet" id="identity" onload="btn.disabled=false;">  
  3. <input type="button" value="更换图片" onclick="reloadImage()" id="btn">  
  4. </body> 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值