六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 21|回复: 0

Web 开发实现验证码输入功能

[复制链接]

升级  1%

55

主题

55

主题

55

主题

举人

Rank: 3Rank: 3

积分
203
 楼主| 发表于 2013-1-28 19:32:25 | 显示全部楼层 |阅读模式
首先建一个类生成一个验证码的图片代码如下

 import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.image.BufferedImage;import java.io.IOException;import java.io.OutputStream;import java.util.Random; import javax.imageio.ImageIO; public class MakeSecurityPic {              /**        * 生成验证码的字符数字组合        */       private char[] mapTable = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',                     'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',                     'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8',                     '9' };        /**        * 获取验证码图片        *         * @param width        *            图片的宽        * @param height        *            图片的高        * @param os        *            输出流        * @return        */       public String getSecurityPic(Integer width, Integer height, OutputStream os) {               if (width == 0)                     width = 60;              if (height == 0)                     height = 20;               BufferedImage image = new BufferedImage(width, height,                            BufferedImage.TYPE_INT_RGB);               //              Graphics2D g = image.createGraphics();               // 填充背景色              g.setColor(Color.white);              g.fillRect(0, 0, width, height);               // 绘制边框              g.setColor(Color.BLACK);              g.drawRect(0, 0, width - 1, height - 1);               // 验证码              String security = "";               // 随机生成验证码              Random random = new Random();              for (int i = 0; i < 4; i++) {                     security += mapTable[random.nextInt(mapTable.length)];              }               // 绘制验证码              g.setColor(Color.BLACK);              g.setFont(new Font("MS Sans Serif", Font.PLAIN, 18));               String temp = security.substring(0, 1);              g.drawString(temp, 7, 15);              temp = security.substring(1, 2);              g.drawString(temp, 18, 17);              temp = security.substring(2, 3);              g.drawString(temp, 28, 18);              temp = security.substring(3, 4);              g.drawString(temp, 40, 17);               // 绘制干扰点              g.setColor(Color.BLACK);              for (int i = 0; i < 20; i++) {                     g.drawOval(random.nextInt(width), random.nextInt(height), 0, 0);              }              // 释放资源              g.dispose();               try {                     // 输出图片到缓冲区                     ImageIO.write(image, "JPG", os);              } catch (IOException e) {                     // TODO Auto-generated catch block                     e.printStackTrace();              }              return security;       }} 
 

 
再建一个servlet 类型为
 response.setContentType("image/jpg");是一个图片类型的。代码如下:package com.security; import java.io.IOException;import java.io.PrintWriter; import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; public class MakeSecurityServlet extends HttpServlet {         public void doGet(HttpServletRequest request, HttpServletResponse response)                     throws ServletException, IOException {              doPost(request,response);       }         public void doPost(HttpServletRequest request, HttpServletResponse response)                     throws ServletException, IOException {                            response.setContentType("image/jpg");              MakeSecurityPic msp = new MakeSecurityPic();              String security = msp.getSecurityPic(0, 0, response.getOutputStream());                            request.getSession().setAttribute("security", security);       } } 

 
显示到jsp页面上如下:
       security:<input type="text" name="security"/><img id="securityPic" src="servlet/MakeSecurityServlet"><a href="#" >看不清楚,换一个</a><br>
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表