How to embed a verification code in a website

Source: Internet
Author: User

Recently the blog Park Revision action is relatively large, see the Background Management page verification code, brought up my interest. Click the displayed verification code, you can go to a special verification code site: http://captcha.com/

It contains Java, PHP,. NET, and so on, almost full-platform support, as well as supporting tutorials and demos.

At the end of the text to provide downloaded Java version of the file, there is a jar package, how to use it? To get started quickly:

One: The simplest text verification code. Use JSP mode.

In the JSP page reference:

<% @page import="botdetect.web.Captcha"%>

Then add the following where you need the CAPTCHA:
// Adding botdetect Captcha to the page " Examplecaptcha " ); Captcha.rendercaptchamarkup (Pagecontext.getservletcontext (),     pagecontext.getout ()); %><input id="captchacodetextbox" type="text" Name="captchacodetextbox" />

Verify that the verification code entered by the user is correct

<%if("POST". Equalsignorecase (Request.getmethod ())) {  //Validate the Captcha to check if we ' re not dealing with a botBoolean Ishuman =captcha.validate (Request, Request.getparameter ("Captchacodetextbox")); if(Ishuman) {//Todo:captcha validation passed, perform protected action}Else {    //Todo:captcha validation failed, show error message  }}%>

Since the introduction of third-party jar packages, of course, it is configured in Web. Xml.

<servlet-name>botdetect captcha</servlet-name>    <servlet-class> botdetect.web.http.captchaservlet</servlet-class></servlet><servlet-mapping>    <servlet-name>botdetect captcha</servlet-name>    <url-pattern>/botdetectcaptcha</ Url-pattern></servlet-mapping>  

Two: Use Springmvc way.

The corresponding page:

<% @page contenttype= "text/html" pageencoding= "UTF-8"%><% @taglib prefix= "Botdetect" uri= "Botdetect"%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > for= "Captchacodetextbox"class= "Prompt" >Retype the code from the picture:</label> <!--Adding botdetect Captcha to the page-to <botdetect:captcha id= "Basicexamplecap Tcha "/> <divclass= "Validationdiv" > <input id= "captchacodetextbox" type= "text"name= "Captchacodetextbox"value= "${basicexample.captchacodetextbox}"/> <input type= "Submit" name= "submit" value= "Submit"/>&nbsp; <spanclass= "correct" >${basicExample.captchaCodeCorrect}</span> <spanclass= "Incorrect" >${basicExample.captchaCodeIncorrect}</span> </div> </fieldset> </form&  Gt </body>

Controller for the page:

 PackageBotdetect.examples.springmvc.basic.controller;Importbotdetect.examples.springmvc.basic.model.BasicExample;ImportBotdetect.web.Captcha;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importorg.springframework.validation.BindException;ImportOrg.springframework.web.servlet.ModelAndView;ImportOrg.springframework.web.servlet.mvc.SimpleFormController; Public classIndexcontrollerextendsSimpleformcontroller { PublicIndexcontroller () {Setcommandclass (basicexample.class); } @OverrideprotectedModelandview OnSubmit (httpservletrequest request, httpservletresponse response, Object command, bindexception E rrors) {basicexample basicexample=(basicexample) command; //Validate the Captcha to check if we ' re not dealing with a botCaptcha Captcha = captcha.load (Request, "Basicexamplecaptcha"); BooleanIshuman =captcha.validate (Request, Basicexample.getcaptchacodetextbox ()); if(Ishuman) {Basicexample.setcaptchacodecorrect ("Correct."); Basicexample.setcaptchacodeincorrect (""); } Else{basicexample.setcaptchacodecorrect (""); Basicexample.setcaptchacodeincorrect ("Incorrect!"); } basicexample.setcaptchacodetextbox (""); return NewModelandview ("index", "Basicexample", basicexample); }  }

The bean corresponding to the verification code

 PackageBotdetect.examples.springmvc.basic.model; Public classBasicexample {PrivateString Usercaptchacode, Captchacodecorrect, Captchacodeincorrect;  PublicString Getcaptchacodetextbox () {returnUsercaptchacode; }   Public voidSetcaptchacodetextbox (String usercaptchacode) { This. Usercaptchacode =Usercaptchacode; }   PublicString Getcaptchacodecorrect () {returnCaptchacodecorrect; }   Public voidSetcaptchacodecorrect (String captchacodecorrect) { This. Captchacodecorrect =Captchacodecorrect; }   PublicString Getcaptchacodeincorrect () {returnCaptchacodeincorrect; }   Public voidSetcaptchacodeincorrect (String captchacodeincorrect) { This. Captchacodeincorrect =Captchacodeincorrect; }}

Download the Captcha file:

Http://captcha.com/botdetect-java-captcha-component-free.zip

How to embed a verification code in a website

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.