code.java 生成验证码
package com.tr.tool;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.Serializable;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* @author wangyun
*
*/
public class ValidateWorld implements Serializable {
private static final long serialVersionUID = 1L;
Image image = null;
HttpServletRequest request = null;
HttpServletResponse response = null;
public ValidateWorld(HttpServletRequest request, HttpServletResponse response){
this.request = request;
this.response = response;
}
public 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);
}
public BufferedImage creatImage(){
int width=150, height=50;
//生成随机类
Random random = new Random();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//获取图形上下文
Graphics g = image.getGraphics();
// 设定背景色
// g.setColor(getRandColor(130,200));
g.fillRect(0,0, width, height);
//设定字体
g.setFont(new Font("华文行楷",Font.CENTER_BASELINE,40));
//画边框
//g.setColor(getRandColor(200,250));
//g.drawRect(0,0,width,height);
// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
// g.setColor(getRandColor(20,250));
// for (int i=0;i<70;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 str1=randomStr(4);// 得到随机字符
HttpSession session = request.getSession();
session.setAttribute("rand",str1);
g.drawString(" ", 0,0 );
for (int i=0;i<4;i++ ){
String rand=str1.substring(i,i+1);
// 将认证码显示到图象中
g.setColor(new Color(20+random.nextInt(110),100+random.nextInt(110),20+random.nextInt(110)));
//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
g.drawString(rand,25*i+20,40);
}
// 图象生效
g.dispose();
return image;
}
// 得到随机字符
public String randomStr(int n) {
String str1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
String str2 = "";
int len = str1.length() - 1;
double r;
for (int i = 0; i < n; i++ ) {
r = (Math.random()) * len;
str2 = str2+str1.charAt((int) r);
}
return str2;
}
}
显示图片也img.jsp
<%@ page language="java" contentType="image/jpeg ; charset=UTF-8"
pageEncoding="UTF-8" import="java.awt.*,java.awt.image.*,javax.imageio.*,java.io.*"%>
<%@ page import ="com.tr.code"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
code c = new code(request,response);
BufferedImage image = c.creatImage();
OutputStream b = response.getOutputStream();
%>
<%
ImageIO.write(image, "JPEG", response.getOutputStream());
//必须添加以下两行否则tomcat下jsp出现getOutputStream() has already been called for this response异常
out.clear();
out = pageContext.pushBody();
%>
</body>
</html>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script>
function changeCode()
{
document.getElementById("cc").src="<%=path%>/img.jsp?a="+Math.random();
}
</script>
</head>
<body>
<form action="" method="post">
<table cellspacing="2px" cellpadding="2px" align="center">
<tr>
<td>用户名:</td>
<td><input type="text" id="name" value=""/></td>
</tr>
<tr>
<td>密 码:</td>
<td><input type="password" id="password" value=""/></td>
</tr>
<tr height="26px">
<td>验证码:</td><td valign="middle"><input type="text" id="validate"></td>
<td><a href="#" οnclick="changeCode();"><img alt="" border="0" id="cc" src="<%=path %>/img.jsp"/></a></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="button" name="button" value="submit"/></td>
</tr>
</table>
</form>
</body>
</html>