Use the Open Source component jcaptcha for JSP color Verification Code

Source: Internet
Author: User
Use the Open Source component jcaptcha for JSP color Verification Code Key Words: JSP Verification Code jcaptcha

For more information, see

Install

Add jcaptcha-all.jar (provided inBin-distribution) And ehcache. Jar (not provided seeEhcache site) To your application class path, ie in you WEB-INF/lib folder.

A jcaptcha service for the instance. Note that the service must be in singleton mode. ImportCom. Octo. CAPTCHA. Service. image. imagecaptchaservice;
ImportCom. Octo. CAPTCHA. Service. image. defamanagmanageableimagecaptchaservice;

Public   Class Captchaservicesingleton {

Private   Static Imagecaptchaservice instance =   New Defaultmanageableimagecaptchaservice ();

Public   Static Imagecaptchaservice getinstance (){
Return Instance;
}
}

Note: The above is a default implementation, and the following are more implementations.

    • Simplelistsoundcaptchaengine // You can also use the sound. It's amazing.
    • Spellersoundcaptchaengine
    • Spellersoundcaptchaengine
    • Defaultgimpyenginecaptcha
    • Bafflelistgimpyenginecaptcha
    • Basiclistgimpyenginecaptcha
    • Deformedbafflelistgimpyenginecaptcha
    • Doublerandomlistgimpyenginecaptcha
    • Simplelistimagecaptchaenginecaptcha
    • Simplefisheyeenginecaptcha

For details, refer to official instructions.

Compile a servlet for generating images

ImportCom. Octo. CAPTCHA. Service. captchaserviceexception;
ImportCom.sun.image.codec.jpeg. Unzip codec;
ImportCom.sun.image.codec.jpeg. Specify imageencoder;

Import Javax. servlet. servletconfig;
Import Javax. servlet. servletexception;
Import Javax. servlet. servletoutputstream;
Import Javax. servlet. http. httpservlet;
Import Javax. servlet. http. httpservletrequest;
Import Javax. servlet. http. httpservletresponse;
Import Java. AWT. image. bufferedimage;
Import Java. Io. bytearrayoutputstream;
Import Java. Io. ioexception;

Public ClassImagecaptchaservletExtendsHttpservlet {

Public VoidInit (servletconfig)ThrowsServletexception {

Super. INIT (servletconfig );

}

Protected   Void Doget (httpservletrequest, httpservletresponse) Throws Servletexception, ioexception {

Byte [] Captchachallengeasjpeg =   Null ;
// The output stream to render the CAPTCHA image as JPEG
Bytearrayoutputstream extends outputstream =   New Bytearrayoutputstream ();
Try {
// Get the session ID that will identify the generated CAPTCHA.
// The same ID must be used to validate the response, the session ID is a good candidate!
String captchaid = Httpservletrequest. getsession (). GETID ();
// Call the imagecaptchaservice getchallenge Method
Bufferedimage challenge =
Captchaservicesingleton. getinstance (). getimagechallengeforid (captchaid,
Httpservletrequest. getlocale ());

// A jpeg Encoder
Incluimageencoder incluencoder =
Using codec. createjpegencoder (using outputstream );
Incluencoder. encode (Challenge );
} Catch (Illegalargumentexception e ){
Httpservletresponse. senderror (httpservletresponse. SC _not_found );
Return ;
} Catch (Captchaserviceexception e ){
Httpservletresponse. senderror (httpservletresponse. SC _internal_server_error );
Return ;
}

Captchachallengeasjpeg=Using outputstream. tobytearray ();

// Flush it in the response
Httpservletresponse. setheader ( " Cache-control " , " No-store " );
Httpservletresponse. setheader ( " Pragma " , " No-Cache " );
Httpservletresponse. setdateheader ( " Expires " , 0 );
Httpservletresponse. setcontenttype ( " Image/JPEG " );
Servletoutputstream responseoutputstream =
Httpservletresponse. getoutputstream ();
Responseoutputstream. Write (captchachallengeasjpeg );
Responseoutputstream. Flush ();
Responseoutputstream. Close ();
}
}

Modify the Web. xml configuration file for Servlet < Servlet >
< Servlet-name > Jcaptcha </ Servlet-name >
< Servlet-class > Imagecaptchaservlet </ Servlet-class >
< Load-on-startup > 0 </ Load-on-startup >
</ Servlet >

< Servlet-Mapping >
< Servlet-name > Jcaptcha </ Servlet-name >
< URL-Pattern > /Jcaptcha </ URL-Pattern >
</ Servlet-Mapping >

Compile your client presentation <IMGSRC= "Jcaptcha">
<InputType= 'Text'Name= 'J _ captcha_response'Value=''>

The above src = "jcaptcha" indicates that the above servlet is called, and the text contains the verification code entered by the user.

Background logic verification Boolean isresponsecorrect = Boolean. False;
// Remenber that we need an ID to validate!
String captchaid = Httpservletrequest. getsession (). GETID ();
// Retrieve the response
String response = Httpservletrequest. getparameter ( " J_captcha_response " );
// Call the service method
Try {
Isresponsecorrect = Captchaservicesingleton. getinstance (). validateresponseforid (captchaid,
Response );
} Catch (Captchaserviceexception e ){
// Shocould not happen, may be thrown if the ID is not valid
}

OK. You are done.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.