Jcaptcha uses jsp color Verification Code

Source: Internet
Author: User
I used jcaptcha for the verification code when I made an opencms form. After a brief look, I thought it was pretty good. I 've collected it. Today I 've improved a little bit. Hey hey :)

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 in bin-distribution) and ehcache. jar (not provided see ehcache 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. Import com. octo. captcha. service. image. ImageCaptchaService;
Import com. octo. captcha. service. image. DefaultManageableImageCaptchaService;

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

Import com. octo. captcha. service. CaptchaServiceException;
Import com.sun.image.codec.jpeg. Unzip codec;
Import com.sun.image.codec.jpeg. encode 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 class ImageCaptchaServlet extends HttpServlet {

Public void init (ServletConfig servletConfig) throws ServletException {

Super. init (servletConfig );

}

Protected void doGet (HttpServletRequest httpServletRequest, HttpServletResponse 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
Required imageencoder required encoder =
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 = paioutputstream. 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
<Input type = '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.