把生成验证码图片的程序封装为一个JavaBean,这个JavaBean的源代码如下:
makeCertPic.java
package pic;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
/**
* @author dzy
* 生成验证码图片
*/
public class makeCertPic {
//验证码图片中可以出现的字符集,可根据需要修改
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'};
/**
* 功能:生成彩色验证码图片
* 参数width为生成图片的宽度,参数height为生成图片的高度,参数os为页面的输出流
*/
public String getCertPic(int width, int height, OutputStream os) {
if(width<=0)width=60;
if(height<=0)height=20;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
Graphics g = image.getGraphics();
// 设定背景色
g.setColor(new Color(0xDCDCDC));
g.fillRect(0, 0, width, height);
//画边框
g.setColor(Color.black);
g.drawRect(0,0,width-1,height-1);
// 取随机产生的认证码
String strEnsure = "";
// 4代表4位验证码,如果要生成更多位的认证码,则加大数值
for(int i=0; i<4; ++i) {
strEnsure+=mapTable[(int)(mapTable.length*Math.random())];
}
// 将认证码显示到图像中,如果要生成更多位的认证码,增加drawString语句
g.setColor(Color.black);
g.setFont(new Font("Atlantic Inline",Font.PLAIN,18));
String str = strEnsure.substring(0,1);
g.drawString(str,8,17);
str = strEnsure.substring(1,2);
g.drawString(str,20,15);
str = strEnsure.substring(2,3);
g.drawString(str,35,18);
str = strEnsure.substring(3,4);
g.drawString(str,45,15);
// 随机产生10个干扰点
Random rand = new Random();
for (int i=0;i<10;i++) {
int x = rand.nextInt(width);
int y = rand.nextInt(height);
g.drawOval(x,y,1,1);
}
// 释放图形上下文
g.dispose();
try {
// 输出图像到页面
ImageIO.write(image, "JPEG", os);
} catch (IOException e) {
return "";
}
return strEnsure;
}
}
在getCertPic()方法中,首先创建了一个内存图像的实例对象,再得到此内存图像的图形上下文对象,接着再用这个上下文对象画背景、边框。接下来,随机生成4个在mapTable[]数组中的字符,组成字符串作为验证字符串,并输出在内存中,为了造成一定的干扰,随机画了10个干扰点,如果要加大干扰效果,可再多画一些点。
makeCertPic.jsp页面用于调用生成验证码图片的JavaBean,并在客户端显示,源代码如下:
makeCertPic.jsp
<%@page contentType="image/jpeg" %>
<jsp:useBean id="image" scope="page" class="pic.makeCertPic" />
<%
Stringstr=image.getCertPic(0,0,response.getOutputStream());
// 将认证码存入SESSION
session.setAttribute("certCode", str);
%>
这里把生成的验证码作为session变量写入,因此在接收登录页面输入的数据页面中,可用用户输入的验证码和这个session变量作比较,如果相同则表示验证通过。
LoginPic.jsp
<%@ page contentType="text/html;charset=utf-8" %>
<html>
<head><title>登录页面</title></head>
<body>
<table align="center" border="0">
<tralign="center"><td><fontcolor="red"><html:errors/></font></td></tr>
<tr align="center"><td>系统登录</td></tr>
<form. action="loginCheck.jsp" method="post" focus="username">
<tr><td>用户名:<input type="text" name="username"/></td></tr>
<tr><td>密 码:<input type="password"name="password"/></td></tr>
<tr><td>验证码:<input type="text" name="certCode"/>
<img src="makeCertPic.jsp">
</td></tr>
<tralign="left"><td>
<input type="submit" value="确定"/></td></tr>
</form>
</table>
</body>
</html>
验证码的输入是否正确可用如下语句验证:
String certCode=request.getParameter("certCode");
if(certCode.equals((String)session.getAttribute("certCode")))
out.print("验证码输入正确");
else
out.print("验证码输入错误");
这里使用Servlet产生验证码
以下是JSP页面
<%@page contentType= "text/html; charset=utf-8" %>
<html>
<head>
<title>认证码输入页面</title>
<meta http-equiv= "Content-Type" content="text/html;charset=utf-8">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Expires" content="0">
<script type="text/javascript">
function fresh() {
window.location.reload();
}
function code(img) {
if(img != null) {
img.src = "imageurl?p="+Math.random();
}
}
</script>
</head>
<body>
<form action= "checkcode.jsp">
<table>
<tr>
<td align=left>系统产生的认证码:</td>
<td>
<img id="ig" border=0 src="imageurl" onclick="code(this)">
<input type=button value="看不清楚" onclick="code(document.getElementById('ig'))">
</td>
</tr>
<tr>
<td align=left>输入上面的认证码:</td>
<td>
<input type="text" name="rand" maxlength="5" >
</td>
</tr>
<tr>
<td colspan="2" align=center>
<input type="submit" value= "提交">
</td>
</tr>
</form>
</body>
</html>
要那么复杂吗?下面的代码,存为jsp文件(验证码名字叫做yzm,从session里取即可),即可:
<%@page contentType="text/html"%><%
response.setContentType("image/gif");
int width = 60, height = 20;
java.awt.image.BufferedImage image = new java.awt.image.BufferedImage(width, height,
java.awt.image.BufferedImage.TYPE_INT_RGB);
java.awt.Graphics g = image.getGraphics();
g.setColor(new java.awt.Color(0x444455));
g.fillRect(0, 0, width, height);
g.setColor(java.awt.Color.black);
g.drawRect(0, 0, width - 1, height - 1);
String rand = String.valueOf(System.currentTimeMillis() % 8999 + 1000);
session.setAttribute("yzm", rand);//验证码的session名称
g.setColor(new java.awt.Color(0xbbbbdd));
g.setFont(new java.awt.Font("Atlantic Inline", java.awt.Font.PLAIN, 18));
String Str = rand.substring(0, 1);
g.drawString(Str, 8, 17);
Str = rand.substring(1, 2);
g.drawString(Str, 20, 15);
Str = rand.substring(2, 3);
g.drawString(Str, 35, 18);
Str = rand.substring(3, 4);
g.drawString(Str, 45, 15);
java.util.Random random = new java.util.Random();
for (int i = 0; i < 200; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
g.drawOval(x, y, 0, 0);
}
g.dispose();
javax.servlet.ServletOutputStream outx = response.getOutputStream();
javax.imageio.ImageIO.write(image, "GIF", outx);
%>