Integration of simple Captcha 1.1.1
===================================
Step 1:
========
in jsp where you required, Copy the below table .
Note
====
1)You should have all the format tag keys in your message properties
2)modify the src tag as required
3)make sure that reloadCaptcha , images are available to the page.
<table align="center" width="98%" border="0" cellpadding="2" cellspacing="2">
<tr>
<td colspan="2" style="font-size: 11px;font-weight: bold"><fmt:message key="captcha.label.captchaInfo"/></td>
</tr>
<tr><td class="ClsLabel" width="35%"><fmt:message key="captcha.label.picture" />:</td><td>
<iframe id="iframeCaptch" zindex="999" frameborder="0" vspace="0" hspace="0" marginwidth="0" marginheight="0" width="260px" scrolling="no" height="50px" src="<c:out value="${contextRoot}"/>/captcha/getSimpleCaptchaImage.do"></iframe>
<img src="<c:out value="${contextRoot}"/>/images/refresh_icon.gif" title="Click here to refresh the image(picture)" onclick="javascript:reloadCapctha()">
</td></tr>
<tr><td> </td><td><fmt:message key="captcha.label.characters"/></td></tr>
<tr><td class="ClsLabel"><span class="ClsRequiredFields">*</span><fmt:message key="captcha.label.typeCharacters"/>:</td><td><input type="text" maxlength="8" name="txtCaptchaAnswer" id="txtCaptchaAnswer" value="" style="width:250px" class="validate[required,minSize[8]]"></td></tr>
</table>
Step 2
======
copy the below function, such that it is available to the JSP page.
function reloadCapctha() {
document.getElementById('iframeCaptch').src="<c:out value="${contextRoot}"/>/captcha/getSimpleCaptchaImage.do";
}
step 3
=======
Captcha Controller
===================
package com.jlr.onlinereservation.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import nl.captcha.Captcha;
import nl.captcha.backgrounds.GradiatedBackgroundProducer;
import nl.captcha.gimpy.DropShadowGimpyRenderer;
import nl.captcha.servlet.CaptchaServletUtil;
import nl.captcha.text.producer.DefaultTextProducer;
import nl.captcha.text.renderer.DefaultWordRenderer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@Controller
public class CaptchaController
{
private static Log log = LogFactory.getLog(CaptchaController.class);
@RequestMapping("/captcha/getSimpleCaptchaImage.do")
public void getSimpleCaptchaImage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
if (log.isDebugEnabled())
{
log.debug("Invoking getSimpleCaptchaImage");
}
try
{
Captcha captcha = new Captcha.Builder(250, 50).addText(new DefaultTextProducer(8), new DefaultWordRenderer()).addBackground(new GradiatedBackgroundProducer())
.gimp(new DropShadowGimpyRenderer()).addNoise().addBorder().build();
CaptchaServletUtil.writeImage(response, captcha.getImage());
request.getSession().setAttribute(Captcha.NAME, captcha);
} catch (Exception ex)
{
log.error("Error getSimpleCaptchaImage" + ex.getMessage());
}
}
public static boolean validateCaptcha( HttpServletRequest request ) {
Captcha captcha = (Captcha) request.getSession().getAttribute( Captcha.NAME );
String answer = request.getParameter( "txtCaptchaAnswer" );
if( captcha.isCorrect( answer ) ) {
return true;
}
return false;
}
}
Step 4
======
Use the Static method validateCaptcha(request).
the method output will be a boolean value , if the entered letters matched, it will return true and page will be saved
else you can handle as required.
sample code
============
if (CaptchaController.validateCaptcha(request))
{
// save logic
}
else
{
request.setAttribute("message", "The text you entered for the verfication text box(CAPTCHA) was not correct.");
}
Note if you are using simle Cattcha 1.2.1
the for the above code to work in getSimpleCaptchaImage method , the below changes required
// code for 1.1.1
Captcha captcha = new Captcha.Builder(250, 50).addText(new DefaultTextProducer(8),
new DefaultWordRenderer()).addBackground(new GradiatedBackgroundProducer())
// code for 1.2.1
private static final char[] DEFAULT_CHARS = new char[] { 'a', 'b',
'c', 'd', 'e', 'f', 'g', 'h', 'k', 'm', 'n', 'p', 'r', 'w',
'x', 'y', '2', '3', '4', '5', '6', '7', '8', };
Captcha captcha = new Captcha.Builder(250, 50).addText(new DefaultTextProducer(8,DEFAULT_CHARS),
new DefaultWordRenderer()).addBackground(new GradiatedBackgroundProducer());
you can download jars from the below links
=============================================
http://sourceforge.net/projects/simplecaptcha/files/
note: download files for JDK 1.5