当前位置 : 主页 > 编程语言 > c++ >

使用谷歌验证码框架(kaptcha)

来源:互联网 收集:自由互联 发布时间:2021-06-30
web 文件 index.jsp Kaptcha com.google.code.kaptcha.servlet.KaptchaServlet kaptcha.border no kaptcha.textproducer.font.color red kaptcha.image.width 135 kaptcha.textproducer.char.string ACDEFHKPRSTWX345679 kaptcha.image.height 50 kaptcha.t
web 文件
 

 
  
   
   
    index.jsp
    
  
  
   
  
    
   
    
   
    Kaptcha
    
   
    com.google.code.kaptcha.servlet.KaptchaServlet
    
    
    
    
     kaptcha.border
     
    
     no
     
    
    
    
    
     kaptcha.textproducer.font.color
     
    
     red
     
    
    
    
    
     kaptcha.image.width
     
    
     135
     
    
    
    
    
     kaptcha.textproducer.char.string
     
    
     ACDEFHKPRSTWX345679
     
    
    
    
    
     kaptcha.image.height
     
    
     50
     
    
    
    
    
     kaptcha.textproducer.font.size
     
    
     43
     
    
    
    
    
     kaptcha.noise.color
     
    
     black
     
    
    
    
    
     kaptcha.textproducer.char.length
     
    
     4
     
    
    
    
    
     kaptcha.textproducer.font.names
     
    
     Arial
     
    
  
    
  
    
   
   
    Kaptcha
    
   
    /Kaptcha.jpg
    
  
   
    
  
    
   
   
    VerifyServlet
    
   
    me.gacl.web.controller.VerifyServlet
    
  
    
   
   
    VerifyServlet
    
   
    /servlet/VerifyServlet
    
  
    

 
html 界面
<%@ page language="java" pageEncoding="UTF-8"%>


  
    显示KaptchaServlet生成的验证码
    
    
  
  
  
       
 
请输入验证码: 看不清,换一张  
后台验证
/**
 * 
 */
package me.gacl.web.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class VerifyServlet extends HttpServlet {

    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charaset=utf-8");
        response.setHeader("pragma", "no-cache");
        response.setHeader("cache-control", "no-cache");
        PrintWriter out = null;
        try {
            //响应数据
            String resultData;
            //获取传过来的验证码
            String verifyCode = request.getParameter("verifyCode");
            System.out.println("verifyCode----"+verifyCode);
            if(verifyCode=="") {
                resultData = "N";
            }else {
                //获取kaptcha生成存放在session中的验证码
                String kaptchaValue = (String) request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
                //比较输入的验证码和实际生成的验证码是否相同
                if(kaptchaValue == null || kaptchaValue == ""||!verifyCode.equalsIgnoreCase(kaptchaValue)) {
                    resultData = "N";
                }else {
                    resultData = "Y";
                }
            }
            out = response.getWriter();
            out.write(resultData);
            out.flush();
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            if(out != null) {
                out.close();
            }
        }
    }
}
网友评论