3种使用java生成验证码的方法.docx

上传人:b****5 文档编号:29052891 上传时间:2023-07-20 格式:DOCX 页数:32 大小:30.77KB
下载 相关 举报
3种使用java生成验证码的方法.docx_第1页
第1页 / 共32页
3种使用java生成验证码的方法.docx_第2页
第2页 / 共32页
3种使用java生成验证码的方法.docx_第3页
第3页 / 共32页
3种使用java生成验证码的方法.docx_第4页
第4页 / 共32页
3种使用java生成验证码的方法.docx_第5页
第5页 / 共32页
点击查看更多>>
下载资源
资源描述

3种使用java生成验证码的方法.docx

《3种使用java生成验证码的方法.docx》由会员分享,可在线阅读,更多相关《3种使用java生成验证码的方法.docx(32页珍藏版)》请在冰豆网上搜索。

3种使用java生成验证码的方法.docx

3种使用java生成验证码的方法

[java]

//image.jsp

<%@pagecontentType="image/jpeg"

import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"

pageEncoding="GBK"%>

<%!

ColorgetRandColor(intfc,intbc){//给定范围获得随机颜色

Randomrandom=newRandom();

if(fc>255)

fc=255;

if(bc>255)

bc=255;

intr=fc+random.nextInt(bc-fc);

intg=fc+random.nextInt(bc-fc);

intb=fc+random.nextInt(bc-fc);

returnnewColor(r,g,b);

}%>

<%

//设置页面不缓存

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

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

response.setDateHeader("Expires",0);

//在内存中创建图象

//通过这里可以修改图片大小

intwidth=85,height=23;

BufferedImageimage=newBufferedImage(width,height,

BufferedImage.TYPE_INT_RGB);

//获取图形上下文

//g相当于笔

Graphicsg=image.getGraphics();

//生成随机类

Randomrandom=newRandom();

//设定背景色

g.setColor(getRandColor(200,250));

//画一个实心的长方,作为北京

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

//设定字体

g.setFont(newFont("黑体",Font.PLAIN,18));

//画边框

g.setColor(Color.BLUE);

g.drawRect(0,0,width-1,height-1);

//随机产生155条干扰线,使图象中的认证码不易被其它程序探测到

g.setColor(getRandColor(160,200));

for(inti=0;i<155;i++){

intx=random.nextInt(width);

inty=random.nextInt(height);

intxl=random.nextInt(12);

intyl=random.nextInt(12);

g.drawLine(x,y,x+xl,y+yl);

}

//取随机产生的认证码(4位数字)

//Stringrand=request.getParameter("rand");

//rand=rand.substring(0,rand.indexOf("."));

StringsRand="";

//如果要使用中文,必须定义字库,可以使用数组进行定义

//这里直接写中文会出乱码,必须将中文转换为unicode编码

String[]str={"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","i","j",

"k","m","n","p","s","t","u","v","w","x","y","z",

"1","2","3","4","5","6","7","8","9"};

for(inti=0;i<5;i++){

Stringrand=str[random.nextInt(str.length)];

sRand+=rand;

//将认证码显示到图象中

g.setColor(newColor(20+random.nextInt(110),20+random

.nextInt(110),20+random.nextInt(110)));//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成

g.drawString(rand,16*i+6,19);

}

//将认证码存入SESSION

session.setAttribute("rand",sRand);

//图象生效

g.dispose();

//输出图象到页面

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

out.clear();

out=pageContext.pushBody();

%>

//使用验证码的页面login.jsp

<%@pagecontentType="text/html"pageEncoding="GBK"%>

登陆页面

<%

//乱码解决

request.setCharacterEncoding("GBK");

%>

登陆程序


<%=request.getAttribute("info")!

=null?

request

.getAttribute("info"):

""%>

用户ID:


密  码:


验证码:


//image.jsp

<%@pagecontentType="image/jpeg"

import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"

pageEncoding="GBK"%>

<%!

ColorgetRandColor(intfc,intbc){//给定范围获得随机颜色

Randomrandom=newRandom();

if(fc>255)

fc=255;

if(bc>255)

bc=255;

intr=fc+random.nextInt(bc-fc);

intg=fc+random.nextInt(bc-fc);

intb=fc+random.nextInt(bc-fc);

returnnewColor(r,g,b);

}%>

<%

//设置页面不缓存

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

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

response.setDateHeader("Expires",0);

//在内存中创建图象

//通过这里可以修改图片大小

intwidth=85,height=23;

BufferedImageimage=newBufferedImage(width,height,

BufferedImage.TYPE_INT_RGB);

//获取图形上下文

//g相当于笔

Graphicsg=image.getGraphics();

//生成随机类

Randomrandom=newRandom();

//设定背景色

g.setColor(getRandColor(200,250));

//画一个实心的长方,作为北京

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

//设定字体

g.setFont(newFont("黑体",Font.PLAIN,18));

//画边框

g.setColor(Color.BLUE);

g.drawRect(0,0,width-1,height-1);

//随机产生155条干扰线,使图象中的认证码不易被其它程序探测到

g.setColor(getRandColor(160,200));

for(inti=0;i<155;i++){

intx=random.nextInt(width);

inty=random.nextInt(height);

intxl=random.nextInt(12);

intyl=random.nextInt(12);

g.drawLine(x,y,x+xl,y+yl);

}

//取随机产生的认证码(4位数字)

//Stringrand=request.getParameter("rand");

//rand=rand.substring(0,rand.indexOf("."));

StringsRand="";

//如果要使用中文,必须定义字库,可以使用数组进行定义

//这里直接写中文会出乱码,必须将中文转换为unicode编码

String[]str={"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","i","j",

"k","m","n","p","s","t","u","v","w","x","y","z",

"1","2","3","4","5","6","7","8","9"};

for(inti=0;i<5;i++){

Stringrand=str[random.nextInt(str.length)];

sRand+=rand;

//将认证码显示到图象中

g.setColor(newColor(20+random.nextInt(110),20+random

.nextInt(110),20+random.nextInt(110)));//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成

g.drawString(rand,16*i+6,19);

}

//将认证码存入SESSION

session.setAttribute("rand",sRand);

//图象生效

g.dispose();

//输出图象到页面

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

out.clear();

out=pageContext.pushBody();

%>

//使用验证码的页面login.jsp

<%@pagecontentType="text/html"pageEncoding="GBK"%>

登陆页面

<%

//乱码解决

request.setCharacterEncoding("GBK");

%>

登陆程序


<%=request.getAttribute("info")!

=null?

request

.getAttribute("info"):

""%>

用户ID:


密  码:


验证码:


效果如下:

 

 

(2)、使用Servlet生成验证码

[java]

//IdentityServlet.java代码如下:

packagecom.helloweenvsfei.servlet;

importjava.awt.Color;

importjava.awt.Font;

importjava.awt.Graphics2D;

importjava.awt.image.BufferedImage;

importjava.io.IOException;

importjava.util.Random;

importjavax.servlet.ServletException;

importjavax.servlet.ServletOutputStream;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

importcom.sun.image.codec.jpeg.JPEGCodec;

importcom.sun.image.codec.jpeg.JPEGImageEncoder;

publicclassIdentityServletextendsHttpServlet{

/**

*

*/

privatestaticfinallongserialVersionUID=-479885884254942306L;

publicstaticfinalchar[]CHARS={'2','3','4','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'};

publicstaticRandomrandom=newRandom();

publicstaticStringgetRandomString(){

StringBufferbuffer=newStringBuffer();

for(inti=0;i<6;i++){

buffer.append(CHARS[random.nextInt(CHARS.length)]);

}

returnbuffer.toString();

}

publicstaticColorgetRandomColor(){

returnnewColor(random.nextInt(255),random.nextInt(255),random

.nextInt(255));

}

publicstaticColorgetReverseColor(Colorc){

returnnewColor(255-c.getRed(),255-c.getGreen(),255-c

.getBlue());

}

publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

response.setContentType("image/jpeg");

StringrandomString=getRandomString();

request.getSession(true).setAttribute("randomString",randomString);

intwidth=100;

intheight=30;

Colorcolor=getRandomColor();

Colorreverse=getReverseColor(color);

BufferedImagebi=newBufferedImage(width,height,

BufferedImage.TYPE_INT_RGB);

Graphics2Dg=bi.createGraphics();

g.setFont(newFont(Font.SANS_SERIF,Font.BOLD,16));

g.setColor(color);

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

g.setColor(reverse);

g.drawString(randomString,18,20);

for(inti=0,n=random.nextInt(100);i

g.drawRect(random.nextInt(width),random.nextInt(height),1,1);

}

//转成JPEG格式

ServletOutputStreamout=response.getOutputStream();

JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(out);

encoder.encode(bi);

out.flush();

}

publicstaticvoidmain(String[]args){

System.out.println(getRandomString());

}

}

//Web.xml的配置为:

IdentityServlet

com.helloweenvsfei.servlet.IdentityServlet

IdentityServlet

/servlet/IdentityServlet

//测试页面identity.html为:

DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">

identity.html

---->

相关搜索

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1