kaptcha 인증 코드 생 성 예

다운로드 주소:
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>

 
 
이미지 코드 클래스
 
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;
	}
	
	
}

좋은 웹페이지 즐겨찾기