每日一得

          不求多得,只求一得 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 閱讀(21033) 評論(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  回復  更多評論
            

          主站蜘蛛池模板: 繁昌县| 崇明县| 扎兰屯市| 吉水县| 宁城县| 双江| 九江县| 白河县| 抚远县| 项城市| 哈密市| 武邑县| 元江| 托里县| 伊金霍洛旗| 和田市| 武功县| 石屏县| 广宁县| 龙川县| 邵武市| 建平县| 绥德县| 安义县| 新晃| 新野县| 北宁市| 托克逊县| 阳东县| 潞城市| 临沂市| 克山县| 汝城县| 开鲁县| 襄樊市| 江油市| 察雅县| 马公市| 河津市| 通辽市| 邢台县|