图片验证码的实现--(java web)

网站上,为了防止有人恶意使用程序传送垃圾信息……使用图片验证码。要求登录者要输入相关图片上的信息。
今天试着写了一个这样的程序,希望可以供交流。
Servlet:


package cn.edu.bzu;

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.imageio.ImageIO;
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 GetKey extends HttpServlet {
//定义可选择的字符
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'};
static Random random=new Random();

public String getRandomString(){
StringBuffer buffer=new StringBuffer();
for(int i=0;i<6;i++){ //生成六个字符
buffer.append(CHARS[random.nextInt(CHARS.length)]);
}
return buffer.toString();
}

public static Color getRandomColor(){ //得到随机颜色
return new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255));
}

public static Color getReverseColor(Color c){ //得到颜色的反色
return new Color(255-c.getRed(),255-c.getGreen(),255-c.getBlue());
}

public void doGet (HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
response.setContentType("image/jpeg"); //设置输出类型
String randomString=getRandomString();
//将getSession()设置为true,当会话不存在是返回null
request.getSession(true).setAttribute("randomString",randomString);

//设置图片的宽、高
int width=100;
int height=30;

Color bcolor=getRandomColor(); //前景色
Color fcolor=getReverseColor(getRandomColor()); //设置背景色

//创建一个彩色图片
BufferedImage bimage=new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
//创建绘图对象
Graphics2D g=bimage.createGraphics();
//字体样式为宋体,加粗,20磅
g.setFont(new Font("宋体",Font.BOLD,20));
//先画出背景色
g.setColor(bcolor);
g.fillRect(0,0,width,height);
//再画出前景色
g.setColor(fcolor);
//绘制随机字符
g.drawString(randomString, 20,22);
//画出干扰点
for(int i=0,n=random.nextInt(100);i<n;i++){
g.drawRect(random.nextInt(width), random.nextInt(height),1, 1);
}

ServletOutputStream outstream=response.getOutputStream();
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(outstream);

encoder.encode(bimage);
outstream.flush();


}
}

====================================================
前台的JSP文件:
<%@ page language="java" contentType="text/html;charset=utf-8" %>
<%@ page import="cn.edu.bzu.GetKey" %>
<jsp:useBean id="show" scope="page" class="cn.edu.bzu.GetKey"/>
<html>
<head><title>登陆</title>
<script language="javascript" type="text/javascript">
function changeImage(){
document.getElementByIdx_x("ch").disabled=true;
document.getElementByIdx_x("imc").src="http://localhost:8080/ExerciseTest/GetKey?ts="+newDate().getTime();
}
</script>
</head>
<body>
<form action="GoTest" method="get">
<table width="400" height="200" border="1" borderColor="blue">
<tr>
<td colspan="4" align="center">
后台登陆
</td>
</tr>
<tr>
<td>用户名:</td>
<td colspan="3"><input type="text" name="username" class="width:200px;height:30px;font-family:微软雅黑"/></td>
</tr>
<tr>
<td>
密&nbsp;&nbsp;码:
</td>
<td colspan="3">
<input type="password" name="pass" class="width:200px;height:30px;"/>
</td>
</tr>
<tr>
<td>
验证码:
</td>
<td>
<input type="text" name="checkkey" class="width:80px;height:30px;padding-top:4px;"/>
</td>
<td>
<img src="http://localhost:8080/ExerciseTest/GetKey" id="imc" onload="ch.disabled=false;"/>
</td>
<td>
<a href="javascript:onclick=changeImage()" id="ch">看不清,换一张</a>
</td>
</tr>
<tr>
<td align="center" >
=================================================
前台我只是简单的整了一下:效果如下图

图片验证码的实现--(java <wbr>web)
============================================
这样你就可以对验证码、用户名和密码进行验证了。因为验证码是放在了session中的。
可以通过request.getSession().getAttribute("randomString").toString()获得。
然后对其进行验证即可。

posted @ 2013-04-25 09:55  刘竹青  阅读(152)  评论(0编辑  收藏  举报