SimpleCaptcha
The process for installing SimpleCaptcha is no different than installing most other libraries for a J2EE container: a jar is deployed to WEB-INF/lib and web.xml is updated. These steps are described for reference below.
StickyCaptchaServlet
, SimpleCaptchaServlet
, ChineseCaptchaServlet
, and AudioCaptchaServlet
. All generate CAPTCHA challenge/answer pairs, but StickyCaptchaServlet
and ChineseCaptchaServlet
are “sticky” to the user’s session: page reloads will render the same CAPTCHA instead of generating a new one. An example mapping for StickyCaptchaServlet:<servlet> <servlet-name>StickyCaptcha</servlet-name> <servlet-class>nl.captcha.servlet.StickyCaptchaServlet</servlet-class> <!-- The width and height params are optional; if unspecified they default to 200 and 50 respectively. --!> <init-param> <param-name>width</param-name> <param-value>250</param-value> </init-param> <init-param> <param-name>height</param-name> <param-value>75</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>StickyCaptcha</servlet-name> <url-pattern>/stickyImg</url-pattern> </servlet-mapping>
And for AudioCaptchaServlet
:
<servlet> <servlet-name>AudioCaptcha</servlet-name> <servlet-class>nl.captcha.servlet.AudioCaptchaServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>AudioCaptcha</servlet-name> <url-pattern>/audio.wav</url-pattern> </servlet-mapping>
url-pattern
s defined in web.xml
, e.g., http://localhost:8080/stickyImg
. If everything has been set up correctly you should see/hear a CAPTCHA challenge.captcha.jsp
. For an image CAPTCHA, add the following code inside the <body>
element:<img src="/stickyImg" /> <form action="/captchaSubmit.jsp" method="post"> <input name="answer" /> </form>
Or for an audio CAPTCHA:
<audio controls autoplay src="/audio.wav"></audio> <form action="/captchaSubmit.jsp" method="post"> <input name="answer" /> </form>
captchaSubmit.jsp
. Add the following:<%@ page import="nl.captcha.Captcha" %> ... <% // We're doing this in a JSP here, but in your own app you'll want to put // this logic in your MVC framework of choice. Captcha captcha = (Captcha) session.getAttribute(Captcha.NAME); // Or, for an AudioCaptcha: // AudioCaptcha captcha = (AudioCaptcha) session.getAttribute(Captcha.NAME); request.setCharacterEncoding("UTF-8"); // Do this so we can capture non-Latin chars String answer = request.getParameter("answer"); if (captcha.isCorrect(answer)) { %> <b>Correct!</b> <% } %>
/captcha.jsp
. You should get your CAPTCHA image, as well as a form for entering your answer. Submit the form and see what happens.