`
fonter
  • 浏览: 857556 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

kaptcha生成验证码例子

    博客分类:
  • J2SE
阅读更多

下载地址:

http://code.google.com/p/kaptcha/

 

生成图片 jsp页面

 

<%@page import="javax.imageio.ImageIO"%>
<%@page import="java.awt.image.BufferedImage"%>
<%@page import="com.yongmail.web.ImageCode"%>
<%@page import="com.yongmail.utils.ToolUtil"%>
<%
	String sid = request.getParameter("sid");
	if (ToolUtil.isEmpty(sid))
		return;
	out.clear();
	response.setDateHeader("Expires", 0);
	// Set standard HTTP/1.1 no-cache headers.
	response.setHeader("Cache-Control",
			"no-store, no-cache, must-revalidate");
	// Set IE extended HTTP/1.1 no-cache headers (use addHeader).
	response.addHeader("Cache-Control", "post-check=0, pre-check=0");
	// Set standard HTTP/1.0 no-cache header.
	response.setHeader("Pragma", "no-cache");

	// return a jpeg
	response.setContentType("image/jpeg");

	// create the text for the image
	String capText = ImageCode.getProducer().createText();
	// store the text in the session
	request.getSession().setAttribute(sid, capText);

	// create the image with the text
	BufferedImage bi = ImageCode.getProducer().createImage(capText);

	ServletOutputStream outStr = response.getOutputStream();

	// write the data out
	ImageIO.write(bi, "jpg", outStr);
	try {
		outStr.flush();
	} finally {
		outStr.close();
	}
%>

 

显示页面

 

<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%@page import="com.yongmail.utils.TimeUtil"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>申请账号</title>
</head>
<%
String id = request.getSession().getId();
String countId = TimeUtil.genId();
 %>
<body>
<form action="submitApply.jsp?countId=<%=countId %>" method="post">
<input name="" type="text" />
<input type="submit" name="Submit" value="提交" />
</form>

<% out.print("<img src=\"getCode.jsp?sid="+id+"_"+countId+"\"/>"); %>
</body>
</html>

 

 

提交后取得验证码示例

 

<%@ page contentType="text/html; charset=utf-8" language="java" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>注册</title>
</head>

<body>
<%
String countId = request.getParameter("countId");
System.out.println((String)request.getSession().getAttribute(request.getSession().getId()+"_"+countId));
 %>
</body>
</html>

 

 

ImageCode类

 

import java.util.Properties;

import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.util.Config;

public class ImageCode {

	private static Producer kaptchaProducer = null;
	private static Properties props = new Properties();
	private static Config config = new Config(props);
	public static Producer getProducer(){
		if(kaptchaProducer == null){
			kaptchaProducer = (Producer) config.getProducerImpl();
		}
		return kaptchaProducer;
	}
	
	
}

 

1
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics