Class StickyCaptchaServlet

  extended by javax.servlet.GenericServlet
      extended by javax.servlet.http.HttpServlet
          extended by nl.captcha.servlet.StickyCaptchaServlet
All Implemented Interfaces:, javax.servlet.Servlet, javax.servlet.ServletConfig

public class StickyCaptchaServlet
extends javax.servlet.http.HttpServlet

Builds a CAPTCHA and stores it in the session. This is intended to prevent bots from simply reloading the page and getting new images until one is generated which they can successfully parse. Removal of the session attribute CaptchaServletUtil.NAME will force a new Captcha to be added to the session. (Obviously, this is not a perfect solution as session IDs can simply not be passed.) The size of the image is by default 200x50. This can be customized using the captcha-height and captcha-width init parameters in web.xml. By default the CAPTCHA will expire after 600000ms. This can be overridden using the ttl init parameter, in milliseconds. An example showing all parameters:

Since the constructed image is a PNG the servlet mapping should be defined something like this:

James Childers
See Also:
Serialized Form

Constructor Summary
Method Summary
 void doGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)
          Write out the CAPTCHA image stored in the session.
 void init(javax.servlet.ServletConfig config)
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service, service
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Detail


public StickyCaptchaServlet()
Method Detail


public void init(javax.servlet.ServletConfig config)
          throws javax.servlet.ServletException
Specified by:
init in interface javax.servlet.Servlet
init in class javax.servlet.GenericServlet


public void doGet(javax.servlet.http.HttpServletRequest req,
                  javax.servlet.http.HttpServletResponse resp)
           throws javax.servlet.ServletException,
Write out the CAPTCHA image stored in the session. If not present, generate a new Captcha and write out its image.

doGet in class javax.servlet.http.HttpServlet

Copyright © 2010-2011 James Childers. All Rights Reserved.