虽然在实际开发中很少去写一个检验码的功能毕竟网上有现有的例子,但有时在有空时开发这些控件也是一件提高编程能力的很好方法,
原理:在servlet中“画出”图形然后在写入response的outputStream,然后将jsp属性设为jpeg即可
Bean中画出图形
package com;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
public class ImageTest {
public ImageTest() {
}
public char mapTable[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', '1', '0', '2', '3', '4', '5', '6', '7',
'8', '9' };
public String getEnsure(int width, int height, OutputStream out) {
Random random = new Random();
if (width <= 0 || height <= 0) {
width = 85;
height = 40;
}
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
// 设置各种参数
g.setColor(new Color(0xDCDCDC));
g.fillRect(0, 0, width, height);
g.setColor(new Color(0xff0000));
g.drawRect(0, 0, width - 1, height - 1);
String strNum = "";
for (int i = 0; i < 4; i++) {
strNum += mapTable[(int) (mapTable.length * Math.random())];
}
g.setColor(Color.black);
g.setFont(new Font("Atlantic Inline", Font.PLAIN, 23));
String str = strNum.substring(0, 1);
g.drawString(str, 6, 27);
str = strNum.substring(1, 2);
g.drawString(str, 24, 25);
str = strNum.substring(2, 3);
g.drawString(str, 44, 28);
str = strNum.substring(3, 4);
g.drawString(str, 64, 25);
// 生成干扰点
for (int i = 0; i < 100; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
g.drawOval(x, y, 1, 1);
}
// 生成干扰线
for (int i = 0; i < 5; i++) {
int x1 = random.nextInt(width);
int x2 = random.nextInt(width);
int y1 = random.nextInt(height);
int y2 = random.nextInt(height);
g.setColor(new Color(random.nextInt(0xffffff)));//彩色的
g.drawLine(x1, y1, x2, y2);
}
g.dispose();
try {
ImageIO.write(image, "JPEG", out);
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return strNum;
}
}
image.jsp关键是contenttype的属性
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"
contentType="image/jpeg"%>
<jsp:useBean id="image" class="com.ImageTest" scope="page" />
<%
String str = image.getEnsure(0, 0, response.getOutputStream());
session.setAttribute("strNum", str);
%>
测试
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript">
function getImage(object) {
object.src="image.jsp?"+Math.random();
}
</script>
</head>
<body>
<img alt="加载中..." src="image.jsp" onclick="getImage(this)">
</body>
</html>