1. 程式人生 > >驗證碼的製作(二):使用開源元件jcaptcha來實現驗證碼

驗證碼的製作(二):使用開源元件jcaptcha來實現驗證碼

package com.octo.captcha.module.servlet.image.sample;

import com.octo.captcha.module.servlet.image.SimpleImageCaptchaServlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SubmitActionServlet extends HttpServlet
{
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
    String userCaptchaResponse = request.getParameter("japtcha");
    boolean captchaPassed = SimpleImageCaptchaServlet.validateResponse(request, userCaptchaResponse);
    response.setCharacterEncoding("utf-8");
    if (captchaPassed)
      response.getWriter().write("驗證通過");
    else {
      response.getWriter().write("驗證失敗");
    }
  }
}

下面看一下它的web.xml中的配置和index.jsp: