com.darwinsys.servlet
Class HumanResponseServlet

java.lang.Object
  extended by javax.servlet.GenericServlet
      extended by javax.servlet.http.HttpServlet
          extended by com.darwinsys.servlet.HumanResponseServlet
All Implemented Interfaces:
java.io.Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig

public class HumanResponseServlet
extends javax.servlet.http.HttpServlet

Also known to some as "CAPTCHA", this servlet will generate one of those annoying but necessary images that the user has to read and verify to confirm that she's a human being not a spambot. The complication is that we can't generate the image back to the middle of a JSP, so we create it in a temp file, and write the <IMG> tag back to the user

Because of this, there must be a writable Temporary Directory /tmp inside the web app directory; this is unusual from a security point of view but would be quite hard to subvert since the servlet does not accept any parameters from the user that are used in creating the file.

Typical usage of this servlet (in, e.g, contact.jsp):

<jsp:include page="/servlet/HumanResponseServlet">

Typical code in response servlet, e.g., ContactServlet:

 final String actualChallenge = request.getParameter("challenge");
 final String expectedChallenge =
     (String)request.getSession().getAttribute(HumanResponseServlet.SESSION_KEY_RESPONSE);
 if (actualChallenge == null) {
     out.println("You must provide a value for the challenge string");
           giveTryAgainLink(out);
     return;
 }
 if (!actualChallenge.equals(expectedChallenge)) {
     out.println("Sorry, you didn't pass the anti-Turing test :-)");
     giveTryAgainLink(out);
     return;
 }
 

See Also:
Serialized Form

Field Summary
static java.lang.String SESSION_KEY_RESPONSE
           
static java.lang.String SESSION_KEY_TIMESTAMP
           
 
Constructor Summary
HumanResponseServlet()
           
 
Method Summary
protected  void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
           
 void init(javax.servlet.ServletConfig arg0)
           
 boolean isValidString(javax.servlet.http.HttpSession session, java.lang.String input)
          Return true iff the user entered the correct string.
 
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
 

Field Detail

SESSION_KEY_RESPONSE

public static final java.lang.String SESSION_KEY_RESPONSE
See Also:
Constant Field Values

SESSION_KEY_TIMESTAMP

public static final java.lang.String SESSION_KEY_TIMESTAMP
See Also:
Constant Field Values
Constructor Detail

HumanResponseServlet

public HumanResponseServlet()
Method Detail

init

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

doGet

protected void doGet(javax.servlet.http.HttpServletRequest request,
                     javax.servlet.http.HttpServletResponse response)
              throws javax.servlet.ServletException,
                     java.io.IOException
Overrides:
doGet in class javax.servlet.http.HttpServlet
Throws:
javax.servlet.ServletException
java.io.IOException

isValidString

public boolean isValidString(javax.servlet.http.HttpSession session,
                             java.lang.String input)
Return true iff the user entered the correct string. Designed to be called from the target servlet, just to encapsulate the logic for this all in one place.

Parameters:
session -
input -
Returns:
True if the user input matches what's in the session.


Copyright © 1996-2004 Ian F. Darwin. See license.html for usage license.