Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I found a solution for this, is not the optimal solution but it works, here it goes:</p> <p>captcha.xhtml</p> <pre><code>&lt;table border="0"&gt; &lt;tr&gt; &lt;td&gt; &lt;h:graphicImage url="#{request.contextPath}/../jcaptcha"/&gt; &lt;/td&gt; &lt;td&gt; &lt;input type='text' name='j_captcha_response' value='' /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; </code></pre> <p>CaptchaServlet doGet method:</p> <pre><code> 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.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-cache, no-store, must-revalidate"); // HTTP 1.1. httpServletResponse.setHeader("Pragma", "no-cache"); httpServletResponse.setDateHeader("Expires", 0); httpServletResponse.setContentType("image/jpeg"); ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream(); responseOutputStream.write(captchaChallengeAsJpeg); responseOutputStream.flush(); responseOutputStream.close(); } </code></pre> <p>created CaptchaServiceRequestSingleton.java</p> <pre><code> package com.myapp.web.common.listener; import java.awt.image.BufferedImage; import java.util.HashMap; import java.util.Locale; import com.octo.captcha.service.image.DefaultManageableImageCaptchaService; import com.octo.captcha.service.image.ImageCaptchaService; public class CaptchaServiceSingleton { private static ImageCaptchaService instance = new DefaultManageableImageCaptchaService(); private static final int MAX_CACHE_SIZE = 200; private static HashMap&lt;String, BufferedImage&gt; captchaImgCache = new HashMap&lt;String, BufferedImage&gt;(); public static ImageCaptchaService getInstance(){ return instance; } public static BufferedImage getImageChallengeForID(String id, Locale locale) { if (captchaImgCache.containsKey(id)) { return captchaImgCache.get(id); } else { BufferedImage bImage = instance.getImageChallengeForID(id, locale); // if limit reached reset captcha cache if (captchaImgCache.size() &gt; MAX_CACHE_SIZE) { captchaImgCache = new HashMap&lt;String, BufferedImage&gt;(); } captchaImgCache.put(id, bImage); return bImage; } } public static void resetImageChallengeForID(String id) { if (captchaImgCache.containsKey(id)) { captchaImgCache.remove(id); } } } </code></pre> <p>when clicking on "Create Account" button Captcha is reset:</p> <p>CustomerMB.openCreateCustomerAccount():</p> <pre><code>public String openCreateCustomerAccount() { customerAccountEditVO = new CustomerAccountVO(); screenComponent.setPageName(NameConstants.CREATE); getUserMB().resetCaptcha(); return null; } </code></pre> <p>in UserMB.resetCaptcha():</p> <pre><code>public String resetCaptcha() { CaptchaServiceSingleton.resetImageChallengeForID(JSFUtil.getRequest().getRequestedSessionId()); return null; } </code></pre> <p>Perhaps it's not the perfect solution but at least it's working for all Browsers.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload