每日一得

不求多得,只求一得 about java,hibernate,spring,design,database,Ror,ruby,快速开发
最近关心的内容:SSH,seam,flex,敏捷,TDD
本站的官方站点是:颠覆软件

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  220 随笔 :: 9 文章 :: 421 评论 :: 0 Trackbacks
key words: jsp验证码 jcaptcha

原文参考 这里

安装

Add jcaptcha-all.jar (provided in bin-distribution) and ehcache.jar (not provided see ehcache site) to your application class path, ie in you WEB-INF/lib folder.

实例一个jcaptcha服务,注意,必须是单例模式的
import com.octo.captcha.service.image.ImageCaptchaService;
import com.octo.captcha.service.image.DefaultManageableImageCaptchaService;

public class CaptchaServiceSingleton {
    
    
private static ImageCaptchaService instance = new DefaultManageableImageCaptchaService();
    
    
public static ImageCaptchaService getInstance(){
        
return instance;
    }
}

注:以上是默认的一个实现,下面是其他更多的实现
  • SimpleListSoundCaptchaEngine   //还可以用声音,真爽哦
  • SpellerSoundCaptchaEngine
  • SpellerSoundCaptchaEngine
  • DefaultGimpyEngineCaptcha           
  • BaffleListGimpyEngineCaptcha           
  • BasicListGimpyEngineCaptcha           
  • DeformedBaffleListGimpyEngineCaptcha           
  • DoubleRandomListGimpyEngineCaptcha           
  • SimpleListImageCaptchaEngineCaptcha           
  • SimpleFishEyeEngineCaptcha
具体请参考官方说明

编写一个产生图片的servlet


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;
       
// the output stream to render the captcha image as jpeg into
        ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
        
try {
        
// get the session id that will identify the generated captcha. 
        
//the same id must be used to validate the response, the session id is a good candidate!
        String captchaId = httpServletRequest.getSession().getId();
        
// call the ImageCaptchaService getChallenge method
            BufferedImage challenge =
                    CaptchaServiceSingleton.getInstance().getImageChallengeForID(captchaId,
                            httpServletRequest.getLocale());
            
            
// a jpeg encoder
            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();

        
// flush it in the response
        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();
    }
}


为servlet修改web.xml配置文件
<servlet>
        
<servlet-name>jcaptcha</servlet-name>
        
<servlet-class>ImageCaptchaServlet</servlet-class>
        
<load-on-startup>0</load-on-startup>
    
</servlet>


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


编写你的客户端的展示
<img src="jcaptcha">
<input type='text' name='j_captcha_response' value=''>

上面的src="jcaptcha"  就是调用了上面的servlet,text里是用户填写的确认验证码

后台逻辑验证
Boolean isResponseCorrect =Boolean.FALSE;
           
//remenber that we need an id to validate!
           String captchaId = httpServletRequest.getSession().getId();
           
//retrieve the response
           String response = httpServletRequest.getParameter("j_captcha_response");
           
// Call the Service method
            try {
                isResponseCorrect 
= CaptchaServiceSingleton.getInstance().validateResponseForID(captchaId,
                        response);
            } 
catch (CaptchaServiceException e) {
                 
//should not happen, may be thrown if the id is not valid 
            }


OK,大功告成了.

posted on 2006-06-11 13:11 Alex 阅读(21017) 评论(28)  编辑  收藏 所属分类: java

评论

# re: 用开源组件jcaptcha做jsp彩色验证码 2006-06-11 16:15 江南白衣
Good. 用一下先  回复  更多评论
  

# re: 用开源组件jcaptcha做jsp彩色验证码 2006-06-12 11:25 坏小子
好呀。。收藏起来

http://www.ihuna.com 精品FLASH观看  回复  更多评论
  

# re: 用开源组件jcaptcha做jsp彩色验证码 2006-08-04 11:32 野#草
感谢分享!!  回复  更多评论
  

# re: 用开源组件jcaptcha做jsp彩色验证码 2006-08-16 13:14 xquan
好东西  回复  更多评论
  

# re: 用开源组件jcaptcha做jsp彩色验证码 2006-09-07 17:50 haya
我是新手,
请问我用的tomcat1.5,怎么会提示
java.lang.NoClassDefFoundError: org/apache/commons/collections/FastHashMap  回复  更多评论
  

# re: 用开源组件jcaptcha做jsp彩色验证码 2006-09-09 01:14 Alex
检查一下相应的commons包是否存在,还有就是是否是已经有此包而冲突  回复  更多评论
  

# re: 用开源组件jcaptcha做jsp彩色验证码 2006-10-17 11:14 sdfsdf
@江南白衣sadfsdfasdsdf
  回复  更多评论
  

# re: 用开源组件jcaptcha做jsp彩色验证码 2006-12-25 16:30 fy
为什么刷新一次图片就不在显示了 要想在显示 就要重新启动tomcat 这个问题怎么解决
6:29:22,390 INFO GlobalFilter:97 - accessType:0
16:29:22,531 ERROR StandardWrapper[/back:jcaptcha]:276 - Servlet.service() for servlet jcaptcha threw exception
java.lang.ClassCastException: java.lang.String
at net.sf.ehcache.Element.equals(Element.java:202)
at java.util.HashMap.eq(Unknown Source)
at java.util.HashMap.removeEntryForKey(Unknown Source)
at java.util.HashMap.remove(Unknown Source)
at net.sf.ehcache.store.MemoryStore.remove(MemoryStore.java:172)
at net.sf.ehcache.Cache.remove(Cache.java:883)
at net.sf.ehcache.Cache.remove(Cache.java:832)
at net.sf.ehcache.Cache.remove(Cache.java:796)
at net.sf.ehcache.Cache.remove(Cache.java:780)
at com.octo.captcha.service.EhcacheManageableCaptchaService.generateAndStoreCaptcha(EhcacheManageableCaptchaService.java:826)
at com.octo.captcha.service.AbstractCaptchaService.getChallengeForID(AbstractCaptchaService.java:538)
at com.octo.captcha.service.image.EhcacheManageableImageCaptchaService.getImageChallengeForID(EhcacheManageableImageCaptchaService.java:505)
at com.tf.sevenp.service.code.ImageCaptchaServlet.doGet(ImageCaptchaServlet.java:39)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:825)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:731)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:526)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Unknown Source)
  回复  更多评论
  

# re: 用开源组件jcaptcha做jsp彩色验证码 2007-01-13 14:40 Lazybones
@fy
你的帮本有可能是RC2的,要换RC3   回复  更多评论
  

# re: 用开源组件jcaptcha做jsp彩色验证码 2007-03-14 13:27 樂日天
赞一个!!谢谢!!  回复  更多评论
  

# re: 用开源组件jcaptcha做jsp彩色验证码 2007-03-23 19:06 黑马_2046
我把验证码长度设为4,但是显示出来后,有时候长度是4,有时候却很长,不知道什么原因  回复  更多评论
  

# re: 用开源组件jcaptcha做jsp彩色验证码 2007-04-11 12:04 ybyb14
如何改变图片的大小,你知道吗?如果知道,请您告诉我!谢谢!
Email:ybyb14@163.com
  回复  更多评论
  

# re: 用开源组件jcaptcha做jsp彩色验证码 2007-04-26 16:50 ..狂.编程..
谁能教教我怎么用这个组件啊. 我用不来. 能不能把组件发给我...谢谢....  回复  更多评论
  

# re: 用开源组件jcaptcha做jsp彩色验证码[未登录] 2007-06-28 20:39 Tony
CaptchaServicePlugi类怎么提示不存在
请问在哪个包中.
谢谢.  回复  更多评论
  

# re: 用开源组件jcaptcha做jsp彩色验证码 2007-07-09 14:49 不好
就不是彩色的 是纯黑色 丑死啦  回复  更多评论
  

# re: 用开源组件jcaptcha做jsp彩色验证码 2007-08-05 16:15 superhanliu@gmail.com
看来它是根据sessionid来生成验证码的,那么如果我同时请求了多个页面,这些页面都有验证码,然后我提交的顺序与请求的顺序还不一致,它是怎么处理的哦?呵呵  回复  更多评论
  

# re: 用开源组件jcaptcha做jsp彩色验证码 2007-08-28 16:26 ola
怎么下那两个jar包啊 下了半天下下来4个 闷  回复  更多评论
  

# re: 用开源组件jcaptcha做jsp彩色验证码 2007-08-28 18:27 ola
怎么用嘛~ 处处异常 伤了~
web.xml里是什么啊 没有叫jcaptcha的servlet 5555~~
<servlet>
<servlet-name>jcaptcha</servlet-name>
<servlet-class>ImageCaptchaServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>


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


  回复  更多评论
  

# re: 用开源组件jcaptcha做jsp彩色验证码 2007-08-29 14:18 ola
能发个工程源码到我邮箱里吗?我搞不出来~ ~~
ola_pianpian@hotmail.com
谢谢了~  回复  更多评论
  

# re: 用开源组件jcaptcha做jsp彩色验证码 2007-09-14 17:38 hello
您好,弄了一天了,没弄不出来,能给我发一份吗?
zhdq_j2ee@126.com
谢谢你.  回复  更多评论
  

# re: 用开源组件jcaptcha做jsp彩色验证码 2007-10-31 23:42 less sleep
我有一个例子 jcaptcha 的,可以换图片。在
www.91athome.com/show  回复  更多评论
  

# re: 用开源组件jcaptcha做jsp彩色验证码 2007-12-20 16:17 xx
图片的长、宽能换控制吗?  回复  更多评论
  

# re: 用开源组件jcaptcha做jsp彩色验证码 2008-05-20 21:19 Angus Zhu
能不能给一份详细的说明给我啊,
我不太懂了,
也就是能不能,给个更详细的步骤给我啊,让我一步一步的做下去,
最好是把你的实现代码给发给我一份,谢谢!
我的邮箱是
zhm6422107@126.com  回复  更多评论
  

# re: 用开源组件jcaptcha做jsp彩色验证码 2009-07-13 20:49 游客
根本就没出现验证码啊  回复  更多评论
  

# re: 用开源组件jcaptcha做jsp彩色验证码 2009-11-16 23:37 add
very nice  回复  更多评论
  

# re: 用开源组件jcaptcha做jsp彩色验证码 2010-03-22 13:44 tt
麻烦发一份给我 始终没搞出来
78997312@qq.com 谢谢  回复  更多评论
  

# re: 用开源组件jcaptcha做jsp彩色验证码 2010-07-29 15:39 张鑫
我的验证码图像也没出来,能发我下邮箱吗
247834994@qq.com 谢谢  回复  更多评论
  

# re: 用开源组件jcaptcha做jsp彩色验证码[未登录] 2012-05-25 10:20 蜗牛
我的验证图像一直没出来 能发一份详细的给我吗 或者给我实现源码吗 谢谢 570690564@163.com  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: