生成验证码的几种方式
- 格式:pdf
- 大小:110.18 KB
- 文档页数:6
⽣成验证码的⼏种⽅式
⽣成验证码的⼏种⽅式
1,在jsp页⾯中直接⽣成验证码
image.jsp源码:
//image.jsp
<%@ page contentType="image/jpeg"
import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"
pageEncoding="GBK"%>
<%!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 = 85, height = 23;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 获取图形上下⽂
// g相当于笔
Graphics g = image.getGraphics();
//⽣成随机类
Random random = new Random();
// 设定背景⾊
g.setColor(getRandColor(200, 250));
// 画⼀个实⼼的长⽅,作为北京
g.fillRect(0, 0, width, height);
//设定字体
g.setFont(new Font("⿊体", Font.PLAIN, 18));
//画边框
g.setColor(Color.BLUE);
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 rand = request.getParameter("rand");
//rand = rand.substring(0,rand.indexOf("."));
String sRand = "";
// 如果要使⽤中⽂,必须定义字库,可以使⽤数组进⾏定义
// 这⾥直接写中⽂会出乱码,必须将中⽂转换为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 (int i = 0; i < 5; i++) {
String rand = str[random.nextInt(str.length)];
sRand += rand;
// 将认证码显⽰到图象中
g.setColor(new Color(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源码(使⽤验证码的页⾯):
//使⽤验证码的页⾯login.jsp
<%@ page contentType="text/html" pageEncoding="GBK"%>
登陆页⾯
<%
// 乱码解决
request.setCharacterEncoding("GBK");
%>
登陆程序
<%=request.getAttribute("info") != null ? request
.getAttribute("info") : ""%>
⽤户ID:
密 码:
验证码:
效果如下:
2,使⽤Servlet⽣成验证码
IdentityServlet.java源码:
//IdentityServlet.java代码如下:
package com.helloweenvsfei.servlet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class IdentityServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -479885884254942306L;
public static final char[] 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' };
public static Random random = new Random();