继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

kaptcha验证码的相关代码

追梦21
关注TA
已关注
手记 1
粉丝 3
获赞 1

servlet部分

```## 标题文字 ##
import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.util.Config;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
import javax.imageio.ImageIO;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class KaptchaServlet extends HttpServlet implements Servlet {
private Properties props;
private Producer kaptchaProducer;
private String sessionKeyValue;

public KaptchaServlet() {
    this.props = new Properties();

    this.kaptchaProducer = null;

    this.sessionKeyValue = null;
}

public void init(ServletConfig conf) throws ServletException {
    super.init(conf);

    ImageIO.setUseCache(false);

    Enumeration initParams = conf.getInitParameterNames();
    while (initParams.hasMoreElements()) {
        String key = (String) initParams.nextElement();
        String value = conf.getInitParameter(key);
        this.props.put(key, value);
    }

    Config config = new Config(this.props);
    this.kaptchaProducer = config.getProducerImpl();
    this.sessionKeyValue = config.getSessionKey();
}

public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    resp.setDateHeader("Expires", 0L);

    resp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");

    resp.addHeader("Cache-Control", "post-check=0, pre-check=0");

    resp.setHeader("Pragma", "no-cache");

    resp.setContentType("image/jpeg");

    String capText = this.kaptchaProducer.createText();
    String s1 = capText.substring(0, 1);
    String s2 = capText.substring(1, 2);
    int r = Integer.valueOf(s1).intValue() + Integer.valueOf(s2).intValue();

    req.getSession().setAttribute(this.sessionKeyValue, String.valueOf(r));

    BufferedImage bi = this.kaptchaProducer.createImage(s1+"+"+s2+"=?");

    ServletOutputStream out = resp.getOutputStream();

    ImageIO.write(bi, "jpg", out);
    try {
        out.flush();
    } finally {
        out.close();
    }
}

}

web.xml部分 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<servlet>
<servlet-name>kaptcha</servlet-name>
<servlet-class>
<!-- com.google.code.kaptcha.servlet.KaptchaServlet -->
KaptchaServlet
</servlet-class>
<!-- kaptcha验证码使用 | 学步园 http://www.xuebuyuan.com/981239.html -->
<init-param>
<description>图片边框,合法值:yes,no</description>
<param-name>kaptcha.border</param-name>
<param-value>yes</param-value>
</init-param>
<init-param>
<description>
边框颜色,合法值:r,g,b(and optional alpha)或者
white,black,blue.
</description>
<param-name>kaptcha.border.color</param-name>
<param-value>red</param-value>
</init-param>
<init-param>
<description>边框厚度,合法值:>0</description>
<param-name>kaptcha.border.thickness</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<description>图片宽度 200</description>
<param-name>kaptcha.image.width</param-name>
<param-value>300</param-value>
</init-param>
<init-param>
<description>图片高度 50</description>
<param-name>kaptcha.image.height</param-name>
<param-value>70</param-value>
</init-param>
<init-param>
<description>图片实现类</description>
<param-name>kaptcha.producer.impl</param-name>
<param-value>
com.google.code.kaptcha.impl.DefaultKaptcha
</param-value>
</init-param>
<init-param>
<description>文本实现类</description>
<param-name>kaptcha.textproducer.impl</param-name>
<param-value>
<!--com.google.code.kaptcha.text.impl.DefaultTextCreator -->
Chinesetext
</param-value>
</init-param>
<init-param>
<description>文本集合,验证码值从此集合中获取</description>
<param-name>kaptcha.textproducer.char.string</param-name>
<param-value>abcdefghijklmnopqrstuvwsyz1234567890</param-value>
</init-param>
<init-param>
<description>验证码长度 5</description>
<param-name>kaptcha.textproducer.char.length</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<description>字体Arial,Courier</description>
<param-name>kaptcha.textproducer.font.names</param-name>
<param-value>宋体</param-value>
</init-param>
<init-param>
<description>字体大小40px</description>
<param-name>kaptcha.textproducer.font.size</param-name>
<param-value>50</param-value>
</init-param>
<init-param>
<description>字体颜色,合法值:r,g,b或者white,black,blue</description>
<param-name>kaptcha.textproducer.font.color</param-name>
<param-value>black</param-value>
</init-param>
<init-param>
<description>文字间隔 2</description>
<param-name>kaptcha.textproducer.char.space</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<description>干扰实现类</description>
<param-name>kaptcha.noise.impl</param-name>
<param-value>
<!-- com.google.code.kaptcha.impl.NoNoise -->
com.google.code.kaptcha.impl.DefaultNoise
</param-value>
</init-param>
<init-param>
<description>干扰颜色,合法值:r,g,b或者white,black,blue</description>
<param-name>kaptcha.noise.color</param-name>
<param-value>white</param-value>
</init-param>
<init-param>
<description>
图片样式:
水纹com.google.code.kaptcha.impl.WaterRipple
鱼眼com.google.code.kaptcha.impl.FishEyeGimpy
阴影com.google.code.kaptcha.impl.ShadowGimpy
</description>
<param-name>kaptcha.obscurificator.impl</param-name>
<param-value>com.google.code.kaptcha.impl.ShadowGimpy</param-value>
</init-param>
<init-param>
<description>背景颜色渐变,开始颜色</description>
<param-name>kaptcha.background.clear.from</param-name>
<param-value>blue</param-value>
</init-param>
<init-param>
<description>背景渐变颜色,结束颜色</description>
<param-name>kaptcha.background.clear.to</param-name>
<param-value>white</param-value>
</init-param>
<init-param>
<description>文字渲染器</description>
<param-name>kaptcha.word.impl</param-name>
<param-value>
com.google.code.kaptcha.text.impl.DefaultWordRenderer
</param-value>
</init-param>
<init-param>
<description>session中存放验证码的key键</description>
<param-name>kaptcha.session.key</param-name>
<param-value>KAPTCHA_SESSION_KEY</param-value>
</init-param>

</servlet>

<servlet-mapping>
<servlet-name>kaptcha</servlet-name>
<url-pattern>/randomcode.jpg</url-pattern>
</servlet-mapping>

<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

页面

<form action="check.jsp" method="post">
<img src="randomcode.jpg">
<input type="text" name="r"/>
<input type="submit"/>
</form>

check.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<%
//检查是否是正确的验证码
String k = (String)session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
String str = request.getParameter("r");
str = new String(str.getBytes("iso-8859-1"),"utf-8");
if(k.equals(str))
out.println("true");
out.print("验证码是:" + k + " " +"输入的是:"+ str);

%>

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP

热门评论

https://coding.net/u/codinghjy/p/java-project/git

我的Kaptcha代码传上去了,你先看看,可以运行http://img.mukewang.com/58cbb241000175b205910275.jpg

查看全部评论