利用servlet生成简单的验证码

在web.xml配置:

代码
  1. <servlet>  
  2.     <servlet-name>image</servlet-name>  
  3.     <servlet-class>org.test.web.AuthImage</servlet-class>  
  4. </servlet>  
  5.   
  6. <servlet-mapping>  
  7.     <servlet-name>image</servlet-name>  
  8.  <url-pattern>/authImage</url-pattern>  
  9. </servlet-mapping>  
<script>render_code();</script>

 

其servlet源代码:

代码
  1. public class AuthImage extends HttpServlet   
  2. {   
  3.   
  4.     private static final String CONTENT_TYPE = "text/html; charset=gb2312";   
  5.     //设置字母的大小,大小   
  6.     private Font mFont = new Font("Times New Roman", Font.PLAIN, 17);   
  7.     public void init() throws ServletException   
  8.     {   
  9.         super.init();   
  10.     }   
  11.     Color getRandColor(int fc,int bc)   
  12.     {   
  13.         Random random = new Random();   
  14.         if(fc>255) fc=255;   
  15.         if(bc>255) bc=255;   
  16.         int r=fc+random.nextInt(bc-fc);   
  17.         int g=fc+random.nextInt(bc-fc);   
  18.         int b=fc+random.nextInt(bc-fc);   
  19.         return new Color(r,g,b);   
  20.     }   
  21.   
  22.     public void service(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException   
  23.     {   
  24.         response.setHeader("Pragma","No-cache");   
  25.         response.setHeader("Cache-Control","no-cache");   
  26.         response.setDateHeader("Expires"0);   
  27.         //表明生成的响应是图片   
  28.         response.setContentType("image/jpeg");   
  29.            
  30.         int width=100, height=18;   
  31.         BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);   
  32.            
  33.         Graphics g = image.getGraphics();   
  34.         Random random = new Random();   
  35.         g.setColor(getRandColor(200,250));   
  36.         g.fillRect(11, width-1, height-1);   
  37.         g.setColor(new Color(102,102,102));   
  38.         g.drawRect(00, width-1, height-1);   
  39.         g.setFont(mFont);   
  40.   
  41.         g.setColor(getRandColor(160,200));   
  42.   
  43.         //画随机线   
  44.         for (int i=0;i<155;i++)   
  45.         {   
  46.             int x = random.nextInt(width - 1);   
  47.             int y = random.nextInt(height - 1);   
  48.             int xl = random.nextInt(6) + 1;   
  49.             int yl = random.nextInt(12) + 1;   
  50.             g.drawLine(x,y,x + xl,y + yl);   
  51.         }   
  52.   
  53.         //从另一方向画随机线   
  54.         for (int i = 0;i < 70;i++)   
  55.         {   
  56.             int x = random.nextInt(width - 1);   
  57.             int y = random.nextInt(height - 1);   
  58.             int xl = random.nextInt(12) + 1;   
  59.             int yl = random.nextInt(6) + 1;   
  60.             g.drawLine(x,y,x - xl,y - yl);   
  61.         }   
  62.   
  63.         //生成随机数,并将随机数字转换为字母   
  64.         String sRand="";   
  65.         for (int i=0;i<6;i++)   
  66.         {   
  67.             int itmp = random.nextInt(26) + 65;   
  68.             char ctmp = (char)itmp;   
  69.             sRand += String.valueOf(ctmp);   
  70.             g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));   
  71.             g.drawString(String.valueOf(ctmp),15*i+10,16);   
  72.         }   
  73.   
  74.         HttpSession session = request.getSession(true);   
  75.         session.setAttribute("rand",sRand);   
  76.         g.dispose();   
  77.         ImageIO.write(image, "JPEG", response.getOutputStream());   
  78.     }   
  79.     public void destroy()   
  80.     {   
  81.     }   
  82. }  
<script>render_code();</script>

 

怎样用:
在页面显示随机图片,可在JSP页面中增加如下代码即可:

代码
  1. <img src="authImage">  
From: http://lighter.iteye.com
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值