一个JSP生成验证码源程序(数字和中文)

数字的验证码源码
<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %>
<%!
Color getRandColor(int fc,int bc){//给定范围获得随机颜色
        Random random = new Random();
        if(fc>255) fc=255;
        if(bc>255) bc=255;
        int r=fc+random.nextInt(bc-fc);
        int g=fc+random.nextInt(bc-fc);
        int b=fc+random.nextInt(bc-fc);
        return new Color(r,g,b);
        }
%>
<%
//设置页面不缓存
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);

// 在内存中创建图象
int width=60, height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

// 获取图形上下文
Graphics g = image.getGraphics();

//生成随机类
Random random = new Random();

// 设定背景色
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);

//设定字体
g.setFont(new Font("Times New Roman",Font.PLAIN,18));

//画边框
//g.setColor(new Color());
//g.drawRect(0,0,width-1,height-1);


// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
g.setColor(getRandColor(160,200));
for (int i=0;i<155;i++)
{
int x = random.nextInt(width);
int y = random.nextInt(height);
        int xl = random.nextInt(12);
        int yl = random.nextInt(12);
g.drawLine(x,y,x+xl,y+yl);
}

// 取随机产生的认证码(4位数字)
String sRand="";
for (int i=0;i<4;i++){
    String rand=String.valueOf(random.nextInt(10));
    sRand+=rand;
    // 将认证码显示到图象中
    g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
    g.drawString(rand,13*i+6,16);
}

// 将认证码存入SESSION
session.setAttribute("rand",sRand);


// 图象生效
g.dispose();

// 输出图象到页面
ImageIO.write(image, "JPEG", response.getOutputStream());
%>



中文的验证码源码

<%@ page  pageEncoding = "gb2312" contentType="image/jpeg" import = "javax.imageio.*,java.util.*,java.awt.image.*,java.awt.*" %>


<%!

    //在此处 获取并生成随机颜色

    Color getRandColor(Random random, int ff, int cc) {

       if (ff > 255)

           ff = 255;

       if (cc > 255)

           cc = 255;

       int r = ff + random.nextInt(cc - ff);

       int g = ff + random.nextInt(cc - ff);

       int b = ff + random.nextInt(cc - ff);

       return new Color(r, g, b);

    } %>


<%

    //在此处 设置JSP页面无缓存

    response.setHeader( "Pragma" , "No-cache" );

    response.setHeader( "Cache-Control" , "no-cache" );

    response.setDateHeader( "Expires" , 0);

    // 设置图片的长宽

    int width = 130;
    int height = 30;

    //设定被随机选取的中文字,此处中文字内容过多,不一一列出,只是举例说明下。
    String base = "\u9752\u534a\u706b\u6cd5\u9898\u5efa\u8d76\u4f4d\u5531\u6d77\u4e03\u5973\u4efb\u4ef6\u611f\u51c6\u97f3\u7b54\u54e5\u9645\u65e7\u795e\u5ea7\u7ae0\u538b\u6162\u53d4\u80cc\u7ec6...省略文字。。。" ;

    //设置 备选随机汉字的个数

    int length = base.length();

    // 创建缓存图像

    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    // 获取图像
    Graphics g = image.getGraphics();

    // 创建随机函数的实例

    Random random = new Random();

    //此处 设定图像背景色

    g.setColor(getRandColor(random, 188, 235));

    g.fillRect(0, 0, width, height);

    //设置随机 备选的字体类型

    String[] fontTypes = { "\u5b8b\u4f53" , "\u65b0\u5b8b\u4f53" ,

           "\u9ed1\u4f53" , "\u6977\u4f53" , "\u96b6\u4e66" };

    int fontTypesLength = fontTypes.length;

    // 在图片背景上增加噪点,增加图片分析难度

    g.setColor(getRandColor(random, 180, 199));

    g.setFont( new Font( "Times New Roman" , Font.PLAIN, 14));

    for ( int i = 0; i < 4; i++) {

       g.drawString( "@*@*@*@*@*@*@*" ,

       0, 5 * (i + 2));

    }

    // 取随机产生的验证码 (4 个汉字 )

    // 保存生成的汉字字符串

    String sRand = "" ;

    for ( int i = 0; i < 4; i++) {

       int start = random.nextInt(length);

       String rand = base.substring(start, start + 1);

       sRand += rand;

       // 设置图片上字体的颜色

       g.setColor(getRandColor(random, 10, 150));

       // 设置字体格式

       g.setFont( new Font(fontTypes[random.nextInt(fontTypesLength)],

       Font.BOLD, 18 + random.nextInt(6)));

       // 将此汉字画到验证图片上面

       g.drawString(rand, 24 * i + 10 + random.nextInt(8), 24);

    }

    // 将验证码存入S ession中

    session.setAttribute( "rand" , sRand);

    g.dispose();

    //将 图象输出到JSP页面中

    ImageIO.write(image, "JPEG" , response.getOutputStream());

    //关闭流

    out.clear();

    out=pageContext.pushBody();  

%>

你可能感兴趣的:(jsp,cache)