//validationCode类。
package com.validationCode;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
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 javax.servlet.http.HttpSession;
public class ValidateCodeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static int WIDTH = 65;//设置图片的宽度
private static int HEIGHT = 22;//设置图片的高度
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
response.setContentType("image/jpeg");
ServletOutputStream sos = response.getOutputStream();
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
char[] rands = generateCheckCode();
drawBackground(g);
drawRands(g, rands);
g.dispose();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(image, "JPEG", bos);
byte[] buf = bos.toByteArray();
response.setContentLength(buf.length);
sos.write(buf);
bos.close();
sos.close();
session.setAttribute("randomCode", new String(rands));
}
private void drawBackground(Graphics g) {
g.setColor(new Color(0xDCDCDC));
g.fillRect(0, 0, WIDTH, HEIGHT);
for (int i = 0; i < 120; i++) {
int x = (int) (Math.random() * WIDTH);
int y = (int) (Math.random() * HEIGHT);
int red = (int) (Math.random() * 255);
int green = (int) (Math.random() * 255);
int blue = (int) (Math.random() * 255);
g.setColor(new Color(red, green, blue));
g.drawOval(x, y, 1, 0);
}
}
private void drawRands(Graphics g, char[] rands) {
// g.setColor(Color.BLUE);
Random random = new Random();
int red = random.nextInt(110);
int green = random.nextInt(50);
int blue = random.nextInt(50);
g.setColor(new Color(red, green, blue));
g.setFont(new Font(null, Font.ITALIC | Font.BOLD, 18));
g.drawString("" + rands[0], 1, 17);
g.drawString("" + rands[1], 16, 15);
g.drawString("" + rands[2], 31, 18);
g.drawString("" + rands[3], 46, 16);
}
private char[] generateCheckCode() {
String chars = "13456789ABCDEFGHIJKLMNPQRSTUVWXY";
char[] rands = new char[4];
for (int i = 0; i < 4; i++) {
int rand = (int) (Math.random() * 32);
rands[i] = chars.charAt(rand);
}
return rands;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
//在servlet中判断输入验证码是否正确
validate=request.getParameter("random");
HttpSession session = request.getSession();
String randomCode = (String)session.getAttribute("randomCode");
if(validate.equalsIgnoreCase(randomCode)){
//如果正确就。。。。。
}
else{
request.getSession().setAttribute("errorMessage",
"验证码不匹配,请重新输入验证码!");
}
//jsp页面
<script language="javascript">
function changeCode()
{
document.getElementById('rc').src="imgcode?t="+new Date().getTime();
}
</script>
<div>
<img src="imgcode" id="rc"/>
<input type="text" name="random" maxlength="4"/>
<a href="javascript:changeCode();">看不清?单击换一张图片</a>
</div>
//web.xml
<servlet>
<servlet-name>ValidateCodeServlet</servlet-name>
<servlet-class>com.validationCode.ValidateCodeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ValidateCodeServlet</servlet-name>
<url-pattern>/imgcode</url-pattern>
</servlet-mapping>