Tutorials : Smoothing Out Graphics Functioning Wrinkles in Linux and Unix :

SimpleCaptcha Integration: Step-by-step

Following these basic steps to integrate SimpleCaptcha with your own application:
  1. Copy the newly created lsimplecaptcha-20060421.jar file, (found in the $PROJECT_ROOT$/dist/lib/ directory) into WEB-INF/lib of your Web application.
  2. Modify the Web application's web.xml file:
    • Create the servlet mapping for the captcha servlet like:
      <servlet>
      <servlet-name>Captcha</servlet-name>
      <display-name>Captcha</display-name>
      <servlet-class>nl.captcha.servlet.CaptchaServlet</servlet-class>
      </servlet>
      
    • Define the mapping:
      <servlet-mapping>
      <servlet-name>Captcha</servlet-name>
      <url-pattern>/Captcha.jpg</url-pattern>
      </servlet-mapping>
      
  3. Create an image tag: <img src="Captcha.jpg"> in your JSP and create a form text input to hold user input. Please see detail in the following example test page called captcha_validation.jsp.
  4. Add the logic in your controller (for simplification, the same JSP page is used as the contoller for demo purpose.) to check the value from the text field against the captcha key stored in the session:
    (String)session.getAttribute(nl.captcha.servlet.Constants.SIMPLE_CAPCHA_SESSION_KEY);
    
    The captcha servlet will put a key in the user session that can be picked up from any controller (with each request, the value will be overridden). Without additional configuration, the Java captcha will render as shown in Figure 5.


    Figure 5: Default image text in black color and black & white background.

    The example test page captcha_validation.jsp is shown in Listing 1.

  5. All image configuration is done through web.xml. The final web.xml file for the LearnAbout application can be seen in Listing 2. Click here to find out more about image configuration in SimpleCaptcha.

    The generated image text will be rendered in red color and light blue background color within no-border box (see Figure 6).


    Figure 6: Customized image text in light purple colour and light blue background.

Figures 7 and 8 show how the random image texts are generated and validated.


Figure 7: The customized image text with the valid input text.


Figure 8: The customized image text with the invalid input text.

For demo purposes, the logic has been added into the captcha_validation.jsp page to demonstrate the system configuration for IBM's WAS 5.1 server app, used in the examples from Figures 9 to 11.


Figure 9: The system environment setting for intergrating SimpleCaptCha with PJA Toolkit.


Figure 10: The system environment setting for intergrating SimpleCaptCha with PJA Toolkit.


Figure 11: The system environment setting for intergrating SimpleCaptCha with PJA Toolkit.

Please note that after you've made the above configuration changes, you need to restart the "app" WAS server and re-deploy the application through the WAS admin console. If you deploy it through the binary deployment, or just change the configuration manually (not through the WAS console), then you will also need to update web.xml manually in the server site:

/www/websphere/AppServer/config/cells/toruxptl2/applications/LearnAbout.ear/deployments
	/LearnAbout/LearnAbout.war/WEB-INF/web.xml
However, this is NOT the recommended deployment approach.

The Best Solution

There are many ways to deal with graphic element generation in the headless environment using the Java platform. In this application's specific environment (IBM WAS 5.1 hosted in the Sun Solaris 5.8 Unix server with a Sun JDK1.4.2 installation), there is no better way than using the Pure Java AWT Toolkit in conjunction with SimpleCaptcha to render the dynamic generated text image. This approach incurs no system dependencies for the X-Server installation on the Unix server and also eliminates any concerns over varying versions of the JDK.

Related Resources


James You specializes in Java software and J2EE application development as a senior programmer and analyst in Canada. He is a Sun Certified Programmer for the Java 2 Platform 1.4 and a Sun Certified Web Component Developer for the J2EE 1.4.

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.