ECM
ECM
总版主
总版主
  • 社区居民
  • 忠实会员
  • 原创写手
阅读:1111回复:0

JCaptcha 实例

楼主#
更多 发布于:2016-01-01 20:44
What's JCaptcha?
如果你登录csdn的话,需要输入验证码, JCaptcha就是显示和效验那张图片上的文字的东西.

Get more detail?
上官网吧,其它地方不可信,包括本贴. http://jcaptcha.sourceforge.net/

以下是我的例子.


当然首先你要加入 JCaptcha jar 包吧
jcaptcha-1.0-bin.zip
http://nchc.dl.sourceforge.net/sourceforge/jcaptcha/jcaptcha-1.0-bin.zip



配置文件:
web.xml

add fellow in to web.xml


<servlet>
       <servlet-name>jcaptcha</servlet-name>
       <servlet-class>*****servlet.ImageCaptchaServlet</servlet-class>  
<!--the stars means privacy 我的隐私 -->
       <load-on-startup>0</load-on-startup>
   </servlet>

   <servlet-mapping>
       <servlet-name>jcaptcha</servlet-name>
       <url-pattern>/jcaptcha</url-pattern>
   </servlet-mapping>

1-----------------------------------
CaptchaService.java


import com.octo.captcha.service.image.ImageCaptchaService;
import com.octo.captcha.service.image.DefaultManageableImageCaptchaService;

public class CaptchaService {

   private static ImageCaptchaService instance = new DefaultManageableImageCaptchaService();

   public static ImageCaptchaService getInstance(){
       return instance;
   }
}


2---------------------------------------

import com.octo.captcha.service.CaptchaServiceException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

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 java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;


public class ImageCaptchaServlet extends HttpServlet {


   public void init(ServletConfig servletConfig) throws ServletException {

       super.init(servletConfig);

   }


   protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {

      byte[] captchaChallengeAsJpeg = null;
       ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
       try {
       String captchaId = httpServletRequest.getSession().getId();
           BufferedImage challenge =
                   CaptchaService
                           .getInstance().getImageChallengeForID(captchaId,
                           httpServletRequest.getLocale());

           JPEGImageEncoder jpegEncoder =
                   JPEGCodec.createJPEGEncoder(jpegOutputStream);
           jpegEncoder.encode(challenge);
       } catch (IllegalArgumentException e) {
           httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
           return;
       } catch (CaptchaServiceException e) {
           httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
           return;
       }

       captchaChallengeAsJpeg = jpegOutputStream.toByteArray();

       httpServletResponse.setHeader("Cache-Control", "no-store");
       httpServletResponse.setHeader("Pragma", "no-cache");
       httpServletResponse.setDateHeader("Expires", 0);
       httpServletResponse.setContentType("image/jpeg");
       ServletOutputStream responseOutputStream =
       httpServletResponse.getOutputStream();
       responseOutputStream.write(captchaChallengeAsJpeg);
       responseOutputStream.flush();
       responseOutputStream.close();
   }
}


3--------------------------------------------------------

<tr>
   <td>请输入验证码/please enter what you saw in the picture</td>
   <td>
       <img src="/jcaptcha">
 

<!-- 上面对应 是的 web.xml 的<servlet>与<servlet-mapping>-->
           <input type='text' name='j_captcha_response' value=''>
   </td>
</tr>



总结:
1,看官方文档,不会英语就别学计算机了,除非有一天,中国人重撑世界的陀.
2,照里面的小例子, ctrl + c / ctrl +v .
3,以后再慢慢调试font, color

 

 

 

 

 

 

 

异常中心网是一家专门收集整理程序员编程过程中遇到的常见异常(exception)以及各种异常问答中心的网站。异常中心网旨在,减少程序员在编码遇到异常,处理各种异常时间和痛苦,让程序员能更愉快的、快速的定位异常并查找对应的异常解决方案。异常中心网诚心打造最完美的编程社区为程序员用户服务,努力成为最好的程序员乐园程序员社区程序异常中心程序bug中心异常问答中心

 

喜欢0 评分0
游客

返回顶部