JCaptcha servlet example

In this example, a simple Java servlet is used to generate a captcha image that is displayed alongside input boxes for username and password on a simulated login page (see Figure 1 below). The user must enter his credentials and the letters appearing in the captcha image. Once the user submits the form, the same servlet validates the user input and sets a flag in the user's session. An appropriate response is shown on the results page indicating whether or not they passed the captcha challenge. Note: the browser must support session cookies. Also, the username and password inputs are ignored in this example (something you might not want to do in a real-life scenario!).



The source for the login jsp page is presented below. Note that the src attribute of the captcha image is "/captcha-demos/simple". This is a GET call to our servlet, which does the work necessary to generate the captcha image. The login form submits via POST method to the same servlet.

 &lt;%@ page language="java" %&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt; &lt;head&gt; &lt;title&gt;Using JCaptcha - A Simple Captcha Servlet&lt;/title&gt; &lt;style type="text/css"&gt; td { font-family: Verdana, Tahoma, Arial, Helvetica; font-size: 10pt; background-color: #FFFFFF; margin-left: 0pt } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;h2&gt;Using JCaptcha: A Simple Captcha Servlet&lt;/h2&gt; &lt;form name="SimpleServletForm" action="/captcha-demos/simple" method="post"&gt; &lt;input type="hidden" name="hidCaptchaID" value="&lt;%= session.getId %&gt;"/&gt; &lt;table border="0" cellspacing="0" cellpadding="3"&gt; &lt;tr&gt; &lt;td&gt;Username:&lt;/td&gt; &lt;td align="center"&gt;&lt;input type="text" name="inUsername" value="joeuser"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Password:&lt;/td&gt; &lt;td align="center"&gt;&lt;input type="password" name="inPassword" value="somepassword"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td valign="middle"&gt;Enter these letters:&lt;br/&gt; &lt;img src="/captcha-demos/simple" align="middle" alt="Enter the    characters appearing in this image" border="1"/&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="inCaptchaChars"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="2" align="right"&gt;&lt;input type="submit" value="Submit"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="2" align="center"&gt;&lt;br/&gt;&lt;br/&gt;(refresh    page to generate a new image)&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; 

Source code for the SimpleCaptchaServlet is shown below. The doGet method handles the captcha image generation. Each captcha image must have an associated ID that is used as the key. The session ID is a convenient captcha ID in this case. The "MyCaptchaService" class (shown later) is a singleton that utilizes a JCaptcha built-in class called DefaultManageableImageCaptchaService. This class provides a default Gimpy-based captcha image, management of associated ID's, and validation of user input.

 package org.owasp.captcha; import com.octo.captcha.service.CaptchaServiceException; import javax.servlet.*; import javax.servlet.http.*; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Map; import javax.imageio.ImageIO; public class SimpleCaptchaServlet extends HttpServlet {  String sImgType = null; public void init( ServletConfig servletConfig ) throws ServletException {    super.init( servletConfig ); // For this servlet, supported image types are PNG and JPG. sImgType = servletConfig.getInitParameter( "ImageType" ); sImgType = sImgType==null ? "png" : sImgType.trim.toLowerCase; if ( !sImgType.equalsIgnoreCase("png") && !sImgType.equalsIgnoreCase("jpg") &&    !sImgType.equalsIgnoreCase("jpeg") ) {      sImgType = "png"; }  }   protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {    ByteArrayOutputStream imgOutputStream = new ByteArrayOutputStream; byte[] captchaBytes; if ( request.getQueryString!=null ) {      response.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "GET request should have no query string." ); return; }    try {      // Session ID is used to identify the particular captcha. String captchaId = request.getSession.getId; // Generate the captcha image. BufferedImage challengeImage = MyCaptchaService.getInstance.getImageChallengeForID(      captchaId, request.getLocale ); ImageIO.write( challengeImage, sImgType, imgOutputStream ); captchaBytes = imgOutputStream.toByteArray; // Clear any existing flag. request.getSession.removeAttribute( "PassedCaptcha" ); }    catch( CaptchaServiceException cse ) {       System.out.println( "CaptchaServiceException - " + cse.getMessage ); response.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Problem generating captcha image." ); return; }    catch( IOException ioe ) {       System.out.println( "IOException - " + ioe.getMessage ); response.sendError( response.SC_INTERNAL_SERVER_ERROR, "Problem generating captcha image." ); return; }    // Set appropriate http headers. response.setHeader( "Cache-Control", "no-store" ); response.setHeader( "Pragma", "no-cache" ); response.setDateHeader( "Expires", 0 ); response.setContentType( "image/" + (sImgType.equalsIgnoreCase("png") ? "png" : "jpeg") ); // Write the image to the client. ServletOutputStream outStream = response.getOutputStream; outStream.write( captchaBytes ); outStream.flush; outStream.close; }  protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {    // Get the request params. Map paramMap = request.getParameterMap; if ( paramMap.isEmpty ) {      response.sendError( HttpServletResponse.SC_METHOD_NOT_ALLOWED, "Post method not allowed without parameters." ); return; }    String[] arr1 = (String[])paramMap.get( "hidCaptchaID" ); String[] arr2 = (String[])paramMap.get( "inCaptchaChars" ); if ( arr1==null || arr2==null ) {      response.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Expected parameters were not found." ); return; }    String sessId = request.getSession.getId; String incomingCaptchaId = arr1.length>0 ? arr1[0] : ""; String inputChars = arr2.length>0 ? arr2[0] : ""; // Check validity and consistency of the data. if ( sessId==null || incomingCaptchaId==null || !sessId.equals(incomingCaptchaId) ) {      response.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Browser must support session cookies." ); return; }    // Validate whether input from user is correct. System.out.println( "Validating - inputChars are: " + inputChars ); boolean passedCaptchaTest = validateCaptcha( incomingCaptchaId, inputChars ); // Set flag into session. request.getSession.setAttribute( "PassedCaptcha", new Boolean(passedCaptchaTest) ); // Forward request to results page. RequestDispatcher rd = getServletContext.getRequestDispatcher( "/results.jsp" ); rd.forward( request, response ); }  private boolean validateCaptcha( String captchaId, String inputChars ) {    boolean bValidated = false; try {      bValidated = MyCaptchaService.getInstance.validateResponseForID( captchaId, inputChars ); }    catch( CaptchaServiceException cse ) {}    return bValidated; } } 

Depending on the outcome of the captcha validation, either a Boolean "true" or Boolean "false" object is set into the user's session in the form of an attribute. The request is then forwarded on to the results.jsp page. Assuming the user entered the correct letters, the page below is displayed.



Here is the source for results.jsp:

 &lt;%@ page language="java" %&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt; &lt;head&gt; &lt;title&gt;Captcha Demo - Servlet&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;% Boolean B = (Boolean)session.getAttribute( "PassedCaptcha" ); if ( B!=null && B.booleanValue ) { %&gt; Congrats! You passed the Captcha test! &lt;% } else { %&gt; Sorry, you failed the Captcha test. &lt;% } %&gt; &lt;p&gt;Please &lt;a href="login.jsp"&gt;Try Again&lt;/a&gt;&lt;/p&gt; &lt;/body&gt; &lt;/html&gt; 