登录随机验证码

1.java类源码
package code;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

/**
 * Created by Administrator on 2015/8/9.
 * 验证码生成类
 */
public class IdentityServlet extends HttpServlet{
    /**
     * 随机字符字典 去除阿拉伯数字 0,1;英文字母 O,I等难辨认的字符
     */
    public  static final char[] CHARS={'2','5','6','7','8','9','A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','j','k','l','m','n','p','q','r','s','t','u','v','w','x','y','z'};

    public static Random random=new Random();//随机数

    /**
     * 获取6位随机数
     * @return String
     */
    public  static  String getRandomString(){
        StringBuffer buffer = new StringBuffer();//字符缓存
        for (int i=0;i<6;i++){
            buffer.append(CHARS[random.nextInt(CHARS.length)]);//每次获取一个随机字符串
        }
        return  buffer.toString();
    }

    /**
     * 获取随机颜色
     * @return
     */
    public  static Color getRandomColor() {
        return new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255));
    }

    /**
     * 返回某个颜色的反色
     * @param color
     * @return
     */
    public static Color getReverseColor(Color color){
        return new Color(255 - color.getRed(),255 - color.getGreen(),255 - color.getBlue());
    }

    /**
     * get请求方法
     */
    public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
        response.setContentType("UTF-8");
        response.setContentType("image/jpeg");//设置输出类型为图片
        String randomString=getRandomString();//获取随机字符串
        System.out.println("随机字符串=" + randomString);
        request.getSession(true).setAttribute("randomString",randomString);//放到Session中

        int width=100;//图片宽度
        int height=30;//图片高度

        Color color=getRandomColor();//获取随机颜色
        Color reverse=getReverseColor(color);//获取反色

        BufferedImage bufferedImage=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);//创建一个彩色图片

        Graphics2D graphics2D=bufferedImage.createGraphics();//获取绘图对象
        graphics2D.setFont(new Font(Font.SANS_SERIF,Font.BOLD,16));//设置字体
        graphics2D.setColor(color);//设置颜色
        graphics2D.fillRect(0, 0, width, height);//绘制背景
        graphics2D.setColor(reverse);//设置反色颜色
        graphics2D.drawString(randomString,18,20);//

        for (int i = 0; i < random.nextInt(100); i++) {//绘制干扰点
            graphics2D.drawRect(random.nextInt(width),random.nextInt(height),1,1);//随机干扰点

        }

        ServletOutputStream out=response.getOutputStream();
        JPEGImageEncoder encoder= JPEGCodec.createJPEGEncoder(out);
        encoder.encode(bufferedImage);
        out.flush();
    }
}
2.页面调用 
<pre name="code" class="html"><%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>图片验证码</title>
  </head>
  <body>
      <script>
        function reloadImage(){
          document.getElementById("btn").disabled=true;
          document.getElementById("image_code").src="/code?ts="+new Date().getTime();
        }
      </script>
      <img src="/code" id="image_code" οnlοad="btn.disabled=false;">
      <input type="button" id="btn" value="换个图片" οnclick="reloadImage()"/>
  </body>
</html>


 

3.web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>piccode</servlet-name>
        <servlet-class>code.IdentityServlet</servlet-class>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>piccode</servlet-name>
        <url-pattern>/code</url-pattern>
    </servlet-mapping>
</web-app>
4.显示结果


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值