nl.captcha.servlet
Class StickyCaptchaServlet

java.lang.Object
  extended by javax.servlet.GenericServlet
      extended by javax.servlet.http.HttpServlet
          extended by nl.captcha.servlet.StickyCaptchaServlet
All Implemented Interfaces:
java.io.Serializable, 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:

    <servlet>
         <servlet-name>StickyCaptcha</servlet-name>
         <servlet-class>nl.captcha.servlet.StickyCaptchaServlet</servlet-class>
         <init-param>
             <param-name>captcha-width</param-name>
             <param-value>400</param-value>
         </init-param>
         <init-param>
             <param-name>captcha-height</param-name>
             <param-value>200</param-value>
         </init-param>
         <init-param>
             <param-name>ttl</param-name>
             <param-value>900000</param-value>
         </init-param>
       </servlet>
 
Since the constructed image is a PNG the servlet mapping should be defined something like this:
     <servlet-mapping>
         <servlet-name>StickyCaptcha</servlet-name>
         <url-pattern>/stickyCaptcha.png</url-pattern>
     </servlet-mapping>

Author:
James Childers
See Also:
Serialized Form

Constructor Summary
StickyCaptchaServlet()
           
 
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

StickyCaptchaServlet

public StickyCaptchaServlet()
Method Detail

init

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

doGet

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

Overrides:
doGet in class javax.servlet.http.HttpServlet
Throws:
javax.servlet.ServletException
java.io.IOException


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