网页验证码总所周知,前言略,看效果图
用最简单的话,完成最快的工作
1. 生成验证码图片的类
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* author : cnzxzc
*/
public class RandomValidateCode {
private Random random = new Random();
private int width = 80;//图片宽
private int height = 26;//图片高
private int lineNum = 5;//干扰线数量
private int pointNum = 30;//噪点数量
private int stringNum = 4;//随机产生字符数量
private Font font = new Font("Times New Roman", Font.ROMAN_BASELINE, 18);
private char[] index = {'a','A','0'};//验证码索引
/**
* 生成验证码字符串
*/
private char[] getValidateString(){
char[] c = new char[stringNum];
for(int i = 0; i<stringNum; i++) {
int offset = random.nextInt(3);
switch (offset) {
case 0:
case 1:
c[i] = (char) ((int) index[offset] + random.nextInt(26));
break;
case 2:
c[i] = (char) ((int) index[offset] + random.nextInt(10));
break;
}
}
return c;
}
/**
* 生成图片
* */
public void makeValidateImage(HttpServletRequest request, HttpServletResponse response){
//验证码
char[] text = getValidateString();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setFont(font);
g.setColor(Color.GREEN);
g.drawRect(0, 0, width, height);
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
//绘制干扰线
drowLine(g);
//绘制噪点
drowPoint(g);
//绘制随机字符
drowString(g, text);
try {
request.getSession().setAttribute("validateCode", new String(text));
ImageIO.write(image, "JPEG", response.getOutputStream());
} catch (Exception e){
e.printStackTrace();
}finally {
g.dispose();
}
}
/**
* 绘制字符串
* */
private void drowString(Graphics g, char[] text){
g.setColor(new Color(random.nextInt(101), random.nextInt(111), random.nextInt(121)));
int x = random.nextInt(width);
int y = random.nextInt(height);
//防止画图画出范围
while(x>25){ x -= 20; }
while(y<13){ y += 10; }
g.drawChars(text, 0, stringNum, x, y);
}
/**
* 绘制干扰线
*/
private void drowLine(Graphics g){
for(int i = 0; i < lineNum; i++) {
g.setColor(new Color(random.nextInt(101), random.nextInt(111), random.nextInt(121)));
int x1 = random.nextInt(width);
int y1 = random.nextInt(height);
int x2 = random.nextInt(width);
int y2 = random.nextInt(height);
g.drawLine(x1, y1, x2, y2);
}
}
/**
* 绘制噪声点
*/
private void drowPoint(Graphics g){
for(int i = 0; i < pointNum; i++) {
g.setColor(new Color(random.nextInt(101), random.nextInt(111), random.nextInt(121)));
int x = random.nextInt(width);
int y = random.nextInt(height);
g.drawLine(x, y, x, y);
}
}
}
2. Servlet文件有两个,一个用于启动,一个用于ajax校验
要是用struts或者Spring就不用两个文件这么麻烦了
//用于启动的servlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RandomValidateCode r = new RandomValidateCode();
r.makeValidateImage(request, response);
}
//用于校验的Servlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String validateCode = (String) (request.getSession().getAttribute("validateCode"));
response.getWriter().write(validateCode);
}
3. 页面代码( jsp )
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>sy6</title>
<script type="text/javascript" src="http://static.hdslb.com/js/jquery.min.js"></script>
<script type="text/javascript">
function check(code){
$.ajax({
type:"post",
url:"${pageContext.request.contextPath}/CheckServlet",
data: "code=" + code,
success:function(data){
if(data!=code) {
document.getElementById("mes").innerHTML = "<p style='color: red;'>错误</p>";
} else document.getElementById("mes").innerHTML = "<p style='color: GREEN;'>√</p>";
}
});
}
</script>
<style type="text/css">
#main{
width:540px;
padding:10px;
margin:250px auto;
text-align:center;
border-radius:5px;
box-shadow:-2px 0 10px #ccc,2px 0 10px #ccc;
}
#main button{
width: 70px;
height: 50px;
font-size: 16px;
color: white;
background-color: dodgerblue;
border: none;
border-radius: 10px;
box-shadow:-2px 0 10px #ccc,2px 0 10px #ccc;
}
</style>
</head>
<body>
<div id="main">
<form action="${pageContext.request.contextPath}/ForwardServlet" id="form">
<img id="validate" src="${pageContext.request.contextPath}/ValidateServlet">
<input type="text" name="key" onchange="check(this.value)"><div id="mes"></div><br>
<input type="submit">
</form>
</div>
</body>
</html>
效果如开头的图所示