逛奔的蜗牛

我不聪明,但我会很努力

   ::  :: 新随笔 ::  ::  :: 管理 ::

1. 需要的jar包: patchca.jar: http://code.google.com/p/patchca/

   patchca使用LGPL协议: GNU Lesser General Public License

2. 请求验证码的 JSP: validate.jsp

3. 产生验证码的 Servlet: ValidationCode.java

4. 验证验证码的 Servlet: ValidateCode.java


验证过程: validate.jsp请求验证码 -> ValidationCode.java生成验证码图片和相应的字符给validate.jsp -> 用户输入验证码 -> ValidateCode.java验证用户的验证码是否输入正确.

================================== validate.jsp ==================================

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>


<body>

    <img src="ValidationCode">

    <form action="ValidateCode" method="post">

        <input type="text" name="validateCode"/>

        <input type="submit" value="Submit"/>

    </form>

</body>

</html>


 

================================== ValidateCode.java ==================================


import java.io.IOException;


import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;


/**

 * Servlet implementation class Validate

 */

public class ValidateCode extends HttpServlet {

    private static final long serialVersionUID = 1L;


    public ValidateCode() {

        super();

    }


    @Override

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,

                                                                                  IOException {

        String validateCode = request.getParameter("validateCode");

        HttpSession session = request.getSession();

        

        // 从session中取得验证码与用户输入的验证码进行比较

        String storedValidateCode = (String) session.getAttribute(session.getId());

        

        response.setHeader("Content-type", "text/html; charset=UTF-8");

        if (validateCode.equalsIgnoreCase(storedValidateCode)) {

            response.getWriter().println("验证码输入正确: " + validateCode);

        } else {

            response.getWriter().println("验证码输入错误: " + validateCode);

        }

    }


    @Override

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,

                                                                                   IOException {

        doGet(request, response);

    }

}

 


================================== ValidationCode.java ==================================

import java.awt.image.BufferedImage;

import java.io.IOException;


import javax.imageio.ImageIO;

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;


import org.patchca.color.GradientColorFactory;

import org.patchca.filter.predefined.CurvesRippleFilterFactory;

import org.patchca.filter.predefined.DiffuseRippleFilterFactory;

import org.patchca.filter.predefined.DoubleRippleFilterFactory;

import org.patchca.filter.predefined.MarbleRippleFilterFactory;

import org.patchca.filter.predefined.RippleFilterFactory;

import org.patchca.filter.predefined.WobbleRippleFilterFactory;

import org.patchca.service.Captcha;

import org.patchca.service.ConfigurableCaptchaService;


/**

 * Servlet implementation class ValidateCode

 */

public class ValidationCode extends HttpServlet {

    private static final long serialVersionUID = 1L;


    public ValidationCode() {

        super();

    }


    // 关键之处,产生图片


    @Override

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,

                                                                                  IOException {

        response.setContentType("image/png");

        ValidateEntry ve = new ValidateEntry();


        HttpSession session = request.getSession();

        session.setAttribute(session.getId(), ve.getCode()); // 验证码保存在session中


        ServletOutputStream out = response.getOutputStream();

        BufferedImage img = ve.getImage();

        ImageIO.write(img, "png", out);

        out.close();

    }


    @Override

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,

                                                                                   IOException {

        doGet(request, response);

    }


    protected class ValidateEntry {

        String code;

        BufferedImage image;


        ValidateEntry() {

            createEntry();

        }


        public String getCode() {

            return code;

        }


        public BufferedImage getImage() {

            return image;

        }


        private void createEntry() {

            ConfigurableCaptchaService cs = new ConfigurableCaptchaService();

            cs.setColorFactory(new GradientColorFactory());

            switch ((int) (System.currentTimeMillis() % 6)) {

            case 0:

                cs.setFilterFactory(new CurvesRippleFilterFactory(cs.getColorFactory()));

                break;

            case 1:

                cs.setFilterFactory(new MarbleRippleFilterFactory());

                break;

            case 2:

                cs.setFilterFactory(new DoubleRippleFilterFactory());

                break;

            case 3:

                cs.setFilterFactory(new WobbleRippleFilterFactory());

                break;

            case 4:

                cs.setFilterFactory(new DiffuseRippleFilterFactory());

                break;

            case 5:

                cs.setFilterFactory(new RippleFilterFactory());

                break;

            }


            Captcha captcha = cs.getCaptcha();

            code = captcha.getChallenge();

            image = captcha.getImage();

        }

    }

}

 

 

posted on 2010-12-28 06:30 逛奔的蜗牛 阅读(2921) 评论(4)  编辑 收藏 引用 所属分类: Java

评论

# re: Java:Servlet生成验证码 2010-12-28 07:02 逛奔的蜗牛
<%!
public String getCookieValue(Cookie[] cookies, String cookieName, String defaultValue) {
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
if (cookieName.equals(cookie.getName())) return (cookie.getValue());
}
return (defaultValue);
}
%>  回复  更多评论
  

# re: Java:Servlet生成验证码 2011-05-25 12:19 sophy
web。xml怎么配置的  回复  更多评论
  

# re: Java:Servlet生成验证码 2014-01-21 11:22 wk
import org.patchca.color.GradientColorFactory;

import org.patchca.filter.predefined.CurvesRippleFilterFactory;

import org.patchca.filter.predefined.DiffuseRippleFilterFactory;

import org.patchca.filter.predefined.DoubleRippleFilterFactory;

import org.patchca.filter.predefined.MarbleRippleFilterFactory;

import org.patchca.filter.predefined.RippleFilterFactory;

import org.patchca.filter.predefined.WobbleRippleFilterFactory;

import org.patchca.service.Captcha;

import org.patchca.service.ConfigurableCaptchaService;

这是需要手动导入的包么???  回复  更多评论
  

# re: Java:Servlet生成验证码 2014-02-05 13:27 邝晶武
网页生成版的怎么用?  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理