kaptcha验证码配置和使用教程

kaptcha-2.3.2.jar,一个java开源的验证码工具包,kaptcha是一个非常实用的验证码生成工具。

有了它,你可以生成各种样式的验证码,因为它是可配置的。


使用步骤

(0)导包 kaptcha-2.3.2.jar。

(1)在web.xml中配置kaptcha验证码的属性常量。


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
version="3.0">
  <display-name>display-name>
  <welcome-file-list>
    <welcome-file>index.jspwelcome-file>
  welcome-file-list>
    
    <servlet>
        
        <servlet-name>kaptchaCodeservlet-name>
        <servlet-class>com.google.code.kaptcha.servlet.KaptchaServletservlet-class>
        
        <init-param>
            <param-name>kaptcha.borderparam-name>
            <param-value>noparam-value>
        init-param>
        
        <init-param>
            <param-name>kaptcha.textproducer.font.colorparam-name>
            <param-value>redparam-value>
        init-param>
        
        <init-param>
            <param-name>kaptcha.image.widthparam-name>
            <param-value>135param-value>
        init-param>
        
        <init-param>
            <param-name>kaptcha.image.heightparam-name>
            <param-value>50param-value>
        init-param>
        
        <init-param>
            <param-name>kaptcha.textproducer.char.stringparam-name>
            <param-value>ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345679
            param-value>
        init-param>
        
        <init-param>
            <param-name>kaptcha.textproducer.char.lengthparam-name>
            <param-value>4param-value>
        init-param>
        
        <init-param>
            <param-name>kaptcha.textproducer.font.sizeparam-name>
            <param-value>43param-value>
        init-param>
        
        <init-param>
            <param-name>kaptcha.noise.colorparam-name>
            <param-value>blackparam-value>
        init-param>
        
        <init-param>
            <param-name>kaptcha.textproducer.font.namesparam-name>
            <param-value>Arialparam-value>
        init-param>
    servlet>

    
    <servlet-mapping>
        <servlet-name>kaptchaCodeservlet-name>
        <url-pattern>/verifyCode.jpgurl-pattern>
    servlet-mapping>
web-app>

(2)实现点击更换kaptcha验证码(jsp文件)。

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


<html>
 <head>
   <base href="<%=basePath%>"> 
   <title>登录页面title>
 head>

 <body>

   <div>
    <form action="<%=basePath %>user.do?opr=login" method="post" >
        <h2>用户登录h2>

        <p>
            验证码:<input type="text" name="verifyCode" />
            <img alt="" id="verifyCode" src="<%=path %>/verifyCode.jpg?r=<%=Math.random() %>"
                 onclick="changeVerifyCode()" style="cursor: pointer;">
            <a href="javascript:void(0)" onclick="changeVerifyCode()">看不清,换一张?a>
        p>
        <p>
            <input type="submit" value="登录" />
        p>
    form>
   div>

   <script type="text/javascript" src="<%=path%>/js/jquery-1.12.4.js">script>
   <script type="text/javascript">

    function changeVerifyCode(){        
        $("#verifyCode").attr("src","<%=path %>/verifyCode.jpg?r="+Math.random());
    }

   script>

 body>
html>

(3)java程序获取kaptcha生成存放在session中的验证码。

String kaptchaVerifyCode = (String) req.getSession()
            .getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);

你可能感兴趣的:(Java代码)