<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    每日一得

    不求多得,只求一得 about java,hibernate,spring,design,database,Ror,ruby,快速開發(fā)
    最近關(guān)心的內(nèi)容:SSH,seam,flex,敏捷,TDD
    本站的官方站點(diǎn)是:顛覆軟件

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      220 隨筆 :: 9 文章 :: 421 評(píng)論 :: 0 Trackbacks
    key words: jsp驗(yàn)證碼 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.

    實(shí)例一個(gè)jcaptcha服務(wù),注意,必須是單例模式的
    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;
    ????}
    }

    注:以上是默認(rèn)的一個(gè)實(shí)現(xiàn),下面是其他更多的實(shí)現(xiàn)
    • SimpleListSoundCaptchaEngine?? //還可以用聲音,真爽哦
    • SpellerSoundCaptchaEngine
    • SpellerSoundCaptchaEngine
    • DefaultGimpyEngineCaptcha??? ??? ???
    • BaffleListGimpyEngineCaptcha??? ??? ???
    • BasicListGimpyEngineCaptcha??? ??? ???
    • DeformedBaffleListGimpyEngineCaptcha??? ??? ???
    • DoubleRandomListGimpyEngineCaptcha??? ??? ???
    • SimpleListImageCaptchaEngineCaptcha??? ??? ???
    • SimpleFishEyeEngineCaptcha
    具體請(qǐng)參考官方說明

    編寫一個(gè)產(chǎn)生圖片的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"? 就是調(diào)用了上面的servlet,text里是用戶填寫的確認(rèn)驗(yàn)證碼

    后臺(tái)邏輯驗(yàn)證
    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 閱讀(21029) 評(píng)論(28)  編輯  收藏 所屬分類: java

    評(píng)論

    # re: 用開源組件jcaptcha做jsp彩色驗(yàn)證碼 2006-06-11 16:15 江南白衣
    Good. 用一下先  回復(fù)  更多評(píng)論
      

    # re: 用開源組件jcaptcha做jsp彩色驗(yàn)證碼 2006-06-12 11:25 壞小子
    好呀。。收藏起來

    http://www.ihuna.com 精品FLASH觀看  回復(fù)  更多評(píng)論
      

    # re: 用開源組件jcaptcha做jsp彩色驗(yàn)證碼 2006-08-04 11:32 野#草
    感謝分享!!  回復(fù)  更多評(píng)論
      

    # re: 用開源組件jcaptcha做jsp彩色驗(yàn)證碼 2006-08-16 13:14 xquan
    好東西  回復(fù)  更多評(píng)論
      

    # re: 用開源組件jcaptcha做jsp彩色驗(yàn)證碼 2006-09-07 17:50 haya
    我是新手,
    請(qǐng)問我用的tomcat1.5,怎么會(huì)提示
    java.lang.NoClassDefFoundError: org/apache/commons/collections/FastHashMap  回復(fù)  更多評(píng)論
      

    # re: 用開源組件jcaptcha做jsp彩色驗(yàn)證碼 2006-09-09 01:14 Alex
    檢查一下相應(yīng)的commons包是否存在,還有就是是否是已經(jīng)有此包而沖突  回復(fù)  更多評(píng)論
      

    # re: 用開源組件jcaptcha做jsp彩色驗(yàn)證碼 2006-10-17 11:14 sdfsdf
    @江南白衣sadfsdfasdsdf
      回復(fù)  更多評(píng)論
      

    # re: 用開源組件jcaptcha做jsp彩色驗(yàn)證碼 2006-12-25 16:30 fy
    為什么刷新一次圖片就不在顯示了 要想在顯示 就要重新啟動(dòng)tomcat 這個(gè)問題怎么解決
    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)
      回復(fù)  更多評(píng)論
      

    # re: 用開源組件jcaptcha做jsp彩色驗(yàn)證碼 2007-01-13 14:40 Lazybones
    @fy
    你的幫本有可能是RC2的,要換RC3   回復(fù)  更多評(píng)論
      

    # re: 用開源組件jcaptcha做jsp彩色驗(yàn)證碼 2007-03-14 13:27 樂日天
    贊一個(gè)!!謝謝!!  回復(fù)  更多評(píng)論
      

    # re: 用開源組件jcaptcha做jsp彩色驗(yàn)證碼 2007-03-23 19:06 黑馬_2046
    我把驗(yàn)證碼長度設(shè)為4,但是顯示出來后,有時(shí)候長度是4,有時(shí)候卻很長,不知道什么原因  回復(fù)  更多評(píng)論
      

    # re: 用開源組件jcaptcha做jsp彩色驗(yàn)證碼 2007-04-11 12:04 ybyb14
    如何改變圖片的大小,你知道嗎?如果知道,請(qǐng)您告訴我!謝謝!
    Email:ybyb14@163.com
      回復(fù)  更多評(píng)論
      

    # re: 用開源組件jcaptcha做jsp彩色驗(yàn)證碼 2007-04-26 16:50 ..狂.編程..
    誰能教教我怎么用這個(gè)組件啊. 我用不來. 能不能把組件發(fā)給我...謝謝....  回復(fù)  更多評(píng)論
      

    # re: 用開源組件jcaptcha做jsp彩色驗(yàn)證碼[未登錄] 2007-06-28 20:39 Tony
    CaptchaServicePlugi類怎么提示不存在
    請(qǐng)問在哪個(gè)包中.
    謝謝.  回復(fù)  更多評(píng)論
      

    # re: 用開源組件jcaptcha做jsp彩色驗(yàn)證碼 2007-07-09 14:49 不好
    就不是彩色的 是純黑色 丑死啦  回復(fù)  更多評(píng)論
      

    # re: 用開源組件jcaptcha做jsp彩色驗(yàn)證碼 2007-08-05 16:15 superhanliu@gmail.com
    看來它是根據(jù)sessionid來生成驗(yàn)證碼的,那么如果我同時(shí)請(qǐng)求了多個(gè)頁面,這些頁面都有驗(yàn)證碼,然后我提交的順序與請(qǐng)求的順序還不一致,它是怎么處理的哦?呵呵  回復(fù)  更多評(píng)論
      

    # re: 用開源組件jcaptcha做jsp彩色驗(yàn)證碼 2007-08-28 16:26 ola
    怎么下那兩個(gè)jar包啊 下了半天下下來4個(gè) 悶  回復(fù)  更多評(píng)論
      

    # re: 用開源組件jcaptcha做jsp彩色驗(yàn)證碼 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>


      回復(fù)  更多評(píng)論
      

    # re: 用開源組件jcaptcha做jsp彩色驗(yàn)證碼 2007-08-29 14:18 ola
    能發(fā)個(gè)工程源碼到我郵箱里嗎?我搞不出來~ ~~
    ola_pianpian@hotmail.com
    謝謝了~  回復(fù)  更多評(píng)論
      

    # re: 用開源組件jcaptcha做jsp彩色驗(yàn)證碼 2007-09-14 17:38 hello
    您好,弄了一天了,沒弄不出來,能給我發(fā)一份嗎?
    zhdq_j2ee@126.com
    謝謝你.  回復(fù)  更多評(píng)論
      

    # re: 用開源組件jcaptcha做jsp彩色驗(yàn)證碼 2007-10-31 23:42 less sleep
    我有一個(gè)例子 jcaptcha 的,可以換圖片。在
    www.91athome.com/show  回復(fù)  更多評(píng)論
      

    # re: 用開源組件jcaptcha做jsp彩色驗(yàn)證碼 2007-12-20 16:17 xx
    圖片的長、寬能換控制嗎?  回復(fù)  更多評(píng)論
      

    # re: 用開源組件jcaptcha做jsp彩色驗(yàn)證碼 2008-05-20 21:19 Angus Zhu
    能不能給一份詳細(xì)的說明給我啊,
    我不太懂了,
    也就是能不能,給個(gè)更詳細(xì)的步驟給我啊,讓我一步一步的做下去,
    最好是把你的實(shí)現(xiàn)代碼給發(fā)給我一份,謝謝!
    我的郵箱是
    zhm6422107@126.com  回復(fù)  更多評(píng)論
      

    # re: 用開源組件jcaptcha做jsp彩色驗(yàn)證碼 2009-07-13 20:49 游客
    根本就沒出現(xiàn)驗(yàn)證碼啊  回復(fù)  更多評(píng)論
      

    # re: 用開源組件jcaptcha做jsp彩色驗(yàn)證碼 2009-11-16 23:37 add
    very nice  回復(fù)  更多評(píng)論
      

    # re: 用開源組件jcaptcha做jsp彩色驗(yàn)證碼 2010-03-22 13:44 tt
    麻煩發(fā)一份給我 始終沒搞出來
    78997312@qq.com 謝謝  回復(fù)  更多評(píng)論
      

    # re: 用開源組件jcaptcha做jsp彩色驗(yàn)證碼 2010-07-29 15:39 張鑫
    我的驗(yàn)證碼圖像也沒出來,能發(fā)我下郵箱嗎
    247834994@qq.com 謝謝  回復(fù)  更多評(píng)論
      

    # re: 用開源組件jcaptcha做jsp彩色驗(yàn)證碼[未登錄] 2012-05-25 10:20 蝸牛
    我的驗(yàn)證圖像一直沒出來 能發(fā)一份詳細(xì)的給我嗎 或者給我實(shí)現(xiàn)源碼嗎 謝謝 570690564@163.com  回復(fù)  更多評(píng)論
      

    主站蜘蛛池模板: 皇色在线免费视频| a级毛片视频免费观看| 亚洲一区二区三区在线视频| 免费久久人人爽人人爽av| 亚洲不卡在线观看| 亚洲精品一级无码鲁丝片| 99久久免费看国产精品| 美女18一级毛片免费看| 亚洲AV无码成人网站久久精品大| 蜜桃视频在线观看免费网址入口| 亚洲成av人片在线观看天堂无码| 亚洲免费闲人蜜桃| 一本色道久久88综合亚洲精品高清| 亚洲日韩国产欧美一区二区三区| 永久免费视频网站在线观看| 亚洲av日韩综合一区久热| 亚洲AV无码精品色午夜果冻不卡| a级毛片免费在线观看| 亚洲精品无码成人片久久不卡| 国产1000部成人免费视频| 一级毛片不卡免费看老司机| 亚洲国产精品综合久久网各| 久久久久噜噜噜亚洲熟女综合| 黄页网站在线视频免费| 亚洲91av视频| 亚洲国产中文v高清在线观看| 一级毛片在线完整免费观看| 亚洲综合区图片小说区| 亚洲中文字幕无码中文字在线 | 国产精品免费大片一区二区| 亚洲国产综合第一精品小说| 亚洲理论电影在线观看| 国产自产拍精品视频免费看| 99爱在线精品免费观看| 七色永久性tv网站免费看| 一级做a免费视频观看网站| 久久精品国产亚洲AV未满十八| 又爽又黄无遮挡高清免费视频| 日本精品久久久久久久久免费| 亚洲精品岛国片在线观看| 成人浮力影院免费看|