verifycode.jsp:
<%@ page contentType="image/jpeg" %>
<%@ page import="java.awt.*, java.awt.image.*" %>
<%@ page import="java.util.*, javax.imageio.*" %>
<%!
//生成随机颜色
Color getRandomColor(int fc, int bc)
{
Random r = new Random();
if(fc > 255)
fc = 255;
if(bc > 255)
bc = 255;
int red = fc + r.nextInt(bc - fc);
int green = fc + r.nextInt(bc - fc);
int blue = fc + r.nextInt(bc - fc);
return new Color(red, green, blue);
}
%>
<%
//设置页面不缓存
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
//创建随机类
Random r = new Random();
//在内存中创建图像,宽度为width, 高度为height
int width = 60;
int height = 20;
BufferedImage pic = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//获取图形上下文环境
Graphics gc = pic.getGraphics();
//设定背景色并填充
gc.setColor(getRandomColor(200, 250));
gc.fillRect(0, 0, width, height);
//设定图形上下文字体
gc.setFont(new Font("Times New Roman", Font.PLAIN, 18));
//随机产生200条干扰直线
gc.setColor(getRandomColor(160, 200));
for(int i = 0; i < 200; ++i)
{
int x1 = r.nextInt(width);
int y1 = r.nextInt(height);
int x2 = r.nextInt(15);
int y2 = r.nextInt(15);
gc.drawLine(x1, y1, x1+x2, y1+y2);
}
//随机产生100个干扰点
gc.setColor(getRandomColor(120, 240));
for(int i = 0; i < 100; ++i)
{
int x = r.nextInt(width);
int y = r.nextInt(height);
gc.drawOval(x, y, 0, 0);
}
//随机产生4位数字的验证码
String RS = "";
String rn = "";
for(int i = 0; i < 4; ++i)
{
rn = String.valueOf(r.nextInt(10));
RS += rn;
gc.setColor(new Color(20 + r.nextInt(110), 20 + r.nextInt(110), 20 + r.nextInt(110)));
gc.drawString(rn, 13* i + 6, 16);
}
//释放图形上下文环境
gc.dispose();
//将认证码RS存入session
session.setAttribute("random", RS);
//输出生成的验证码图像
ImageIO.write(pic, "jpeg", response.getOutputStream());
%>
上面是显示验证码的信息。
调用:<img alt="" src="verifycode.jsp"/>,我是想知道,怎么判断文本档的内容与验证码是否相等,