代码都很简单,先看看效果,其它的直接看代码都会懂的:
验证码图片生成servlet:
总共就三步:
1)使用BufferedImage 创造一个图片
2)就是设置文字和一些干扰(颜色都是随机的)
a) 设置背景色
b) 随机画10条直线
c) 生成4个随机的数字字母,这就是验证码
3)然后使用ImageIO写入到response的outputstream
就这么简单。
package com.luoyh.servlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import java.io.FileInputStream;
import java.net.URL;
import java.net.URLDecoder;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
import java.util.Random;
import java.awt.Color;
import java.awt.Font;
/**
* 创建随机图片验证码的servlet
* 验证码存放在session当中
* 在HandlerCode做验证处理
* @see HandlerCode
*/
public class ValidateCode extends HttpServlet {
private static final long serialVersionUID = -1534235656L;
private static final int[] FONT_STYLES = {Font.PLAIN, Font.ITALIC, Font.BOLD};
// 0-9 a-z A-Z
private static final int[] CHS = {48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122};
private static final int LINE_NUM = 10;
// draw line numbers
private static final int FONT_NUM = 3;
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
try {
// w: image width, h: image height, x: draw string begin x
int w = 150, h = 40, x = 20;
// create code
String code = "";
BufferedImage newBi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics g = newBi.getGraphics();
setColor(g);
g.fillRect(0, 0, w, h);
// rand draw line
for(int i = 0; i < LINE_NUM; i ++) {
setColor(g);
g.drawLine(getRandom(w), getRandom(h), getRandom(w), getRandom(h));
}
// draw 4 character
for(int i = 0; i < 4; i ++) {
setColor(g);
setFont(g);
String ch = Character.toString((char)CHS[getRandom(62)]);
code += ch;
g.drawString(ch, x + getRandom(20), getRandom(h / 2) + 15 );
x += 25;
}
// put code in session
HttpSession session = request.getSession();
session.setAttribute("code", code);
System.out.println(code);
// write image in response ouput stream
ImageIO.write(newBi, "png", response.getOutputStream());
} catch (Exception ex) {
System.out.println("error");
}
}
private void setFont(Graphics g) {
g.setFont(new Font("Consolas", FONT_STYLES[getRandom(FONT_NUM)], 24));
}
private int getRandom(int rand) {
return new Random().nextInt(rand);
}
private void setColor(Graphics g) {
g.setColor(new Color(new Random().nextInt(0xffffff)));
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) {
doGet(request, response);
}
// test
// use load image
private FileInputStream readFile(String fileName) throws Exception {
URL url = Thread.currentThread().getContextClassLoader().getResource("com/luoyh/servlet");
String protocol = url.getProtocol(), code = url.getFile();
//System.out.println("protocol: " + protocol + "code: " + code);
String path = URLDecoder.decode(code, "utf-8");
return new FileInputStream(path + "/" + fileName);
}
}
下面就是验证的servlet:
package com.luoyh.servlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/**
* 验证码异步验证处理servlet
**/
public class HandlerCode extends HttpServlet {
/**
* success return 0 else error.
*
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
try {
String inputCode = request.getParameter("inputCode"),
sessionCode = (String) request.getSession().getAttribute("code");
if(sessionCode == null || inputCode == null) {
response.getWriter().write("-1");
return;
}
// is equals
if(inputCode.toUpperCase().equals(sessionCode.toUpperCase())) {
response.getWriter().write("0");
} else {
response.getWriter().write("1");
}
} catch (Exception ex) {
response.getWriter().write("-1");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
doGet(request, response);
}
}
页面代码:
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>-DEMO-</title>
<meta name="description" content="User login page" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script type="text/javascript" src="jquery-2.1.0.min.js"></script>
<style>
a {
text-decoration: none;
}
</style>
<script type="text/javascript">
$(function(){
// submit click handl
$('#submit').click(function() {
$.ajax({
url : '/demo/handler?inputCode=' + $('#code').val(),
success: function(d) {
if(d != '0') { // fail
refresh();
} else { // ok
$('#code').attr('disabled', '');
alert('Its ok!');
}
console.log(d);
}
});
});
// code refresh
// set input disabled and empty
var refresh = function() {
$('#code').removeAttr('disabled');
$('#code').val('');
$('#codeImg').attr('src', $('#codeImg').attr('_src') + '?t=' + new Date().getTime());
};
$('#refresh').click(function() {
refresh();
});
});
</script>
</head>
<body>
验证码:
<input id="code" type="text" value="" />
<a id="refresh" href="javascript:;">
<img _src="<%=request.getContextPath()%>/validate" id="codeImg" src="<%=request.getContextPath()%>/validate" alt="" width="150" height="40" />
</a>
<br/>
<input id="submit" type="button" value="确认" />
<hr>
</body>
</html>
web.xml
<servlet>
<servlet-name>validate</servlet-name>
<servlet-class>com.luoyh.servlet.ValidateCode</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>validate</servlet-name>
<url-pattern>/validate</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>handler</servlet-name>
<servlet-class>com.luoyh.servlet.HandlerCode</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>handler</servlet-name>
<url-pattern>/handler</url-pattern>
</servlet-mapping>