GOOGLE Kaptcha Verification Code generation plug-in servlet/jsp project use steps

Source: Internet
Author: User
Tags border color

Step One:

Add Kaptchar jar package to the project: http://code.google.com/p/kaptcha/(Thunder download is recommended)


Step Two:
Configure Web. XML, detailed configuration (not all configurations here, there are many parameters can be reduced as appropriate):

 <!--Configuring the CAPTCHA plug-in servlet class-<servlet> <servlet-name>kaptcha</servlet-name> <servlet-class&  Gt  Com.google.code.kaptcha.servlet.KaptchaServlet </servlet-class> <!--to the CAPTCHA plug-in servlet class configuration Parameters--<!--border settings Yes no-<init-param> <param-name>kaptcha.border</param-name> <param-value>no& Lt;/param-value> </init-param> <!--border color name for example: Red Blue Black can also be rgb:105,179,90--and <init-    Param> <param-name>kaptcha.border.color</param-name> <param-value>red</param-value> </init-param> <!--Border Width--<init-param> <param-name>kaptcha.border.thickness</par Am-name> <param-value>5</param-value> </init-param> <!--the character source that generated the verification code is compatible with the Chinese--&lt ;init-param> <param-name>kaptcha.textproducer.char.string</param-name> <param-value>2345 Total 23 54 Al asdf Tiffin 2345 Climbing Wall 2345Ewry empty 1254 shares Fktiul part of y7u12354o Private 254 recognized Ry13254iu's identity 123 Happy </param-value> </init-param> <!-- Number of characters to generate results-<init-param> <param-name>kaptcha.textproducer.char.length</param-name> &lt ;p aram-value>5</param-value> </init-param> <!--character font of the resulting results--<init-param> &LT;PA ram-name>kaptcha.textproducer.font.names</param-name> <param-value> Microsoft Ya hei, arial, italic </param-value > </init-param> <!--the character size of the resulting result--<init-param> <param-name>kaptcha.textproducer. Font.size</param-name> <param-value>40</param-value> </init-param> <!--the character color of the resulting result- <init-param> <param-name>kaptcha.textproducer.font.color</param-name> <param-value >blue</param-value> </init-param> <!--character spacing of generated results-<init-param> <param-nam e>kaptcha.textproducer.char.space</Param-name> <param-value>2</param-value> </init-param> <!--interference Line Generator--<init- Param> <param-name>kaptcha.noise.impl</param-name> <param-value>com.google.code.kaptcha.imp L.defaultnoise</param-value> </init-param> <!--interference Line Color--<init-param> <param-na Me>kaptcha.noise.color</param-name> <param-value>BLACK</param-value> </init-param> & lt;! --Interference line background gradient color gradient from left to right-<init-param> &LT;PARAM-NAME&GT;KAPTCHA.BACKGROUND.CLEAR.FROM&LT;/PARAM-NAME&G      T <param-value>green</param-value> </init-param> <!--interference line background gradient color from right to left gradient-<init-par Am> <param-name>kaptcha.background.clear.to</param-name> <param-value>yellow</param-valu E> </init-param> <!--verification code picture width--<init-param> <param-name>kaptcha.image.width&lt ;/param-name> <param-value>200</param-value> </init-param> <!--verification code picture height-<init-param&gt      ; <param-name>kaptcha.image.height</param-name> <param-value>50</param-value> </ Init-param> </servlet> <!--verification Code request name Mapping--<servlet-mapping> <servlet-name>kaptcha</se Rvlet-name> <url-pattern>/yzm</url-pattern> </servlet-mapping>

The above is for the use of Google plug-in generated verification code parameter configuration, do not write the code generated by the tool class.


Step Three:
To add a picture label to the JSP page, the SRC static resource request address for the tag is the configured request name Yzm, as follows:

<br>    

Step Four:
Create a servlet in response to request processing for form submissions, and configure the servlet with the following code:

Package Com.kaptcha.action;import Java.io.ioexception;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public class Kaptchaservlet extends HttpServlet {private static final long Serialversionuid = 1l;protected void Service (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {//Set Request encoding Method Request.setcharacterencoding ("Utf-8");//Get form submission parameters String Yzm = Request.getparameter ("Yzm"); System.out.println (YZM);//Gets the verification code generated by the Google Kaptcha plugin from the session Googlecode = Request.getsession (). getattribute ( Com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY). toString (); System.out.println (Googlecode);//Compare Verification Code correctness SYSTEM.OUT.PRINTLN (Googlecode.equalsignorecase (YZM));  True}}

********************************

  <servlet>    <description></description>    <servlet-name>kaptchaservlet</ servlet-name>    <servlet-class>com.kaptcha.action.KaptchaServlet</servlet-class>  </ servlet>  <servlet-mapping>    <servlet-name>KaptchaServlet</servlet-name>    < Url-pattern>/kaptchatest</url-pattern>  </servlet-mapping>

Step Five:
Restart the server, the Address bar input JSP address, you can test the results

The parameters that can be set are for reference only: *************************

Parameters that can be set are for reference only:
Kaptcha.border whether there is a border default to True we can set it ourselves yes,no
Kaptcha.border.color border color defaults to Color.Black
kaptcha.border.thickness Border thickness defaults to 1
Kaptcha.producer.impl Verification Code generator defaults to Defaultkaptcha
Kaptcha.textproducer.impl captcha Text Generator defaults to Defaulttextcreator
kaptcha.textproducer.char.string Captcha text character content range defaults to ABCDE2345678GFYNMNPWX
Kaptcha.textproducer.char.length Verification Code Text character length default is 5
Kaptcha.textproducer.font.names captcha text font style default to New font ("Arial", 1, fontSize), New Font ("Courier", 1, FontSize)
Kaptcha.textproducer.font.size captcha text character size defaults to 40
Kaptcha.textproducer.font.color captcha text character color defaults to Color.Black
Kaptcha.textproducer.char.space Verification Code text character spacing defaults to 2
Kaptcha.noise.impl Verification Code Noise generation object defaults to Defaultnoise
Kaptcha.noise.color captcha Noise color defaults to Color.Black
Kaptcha.obscurificator.impl Verification Code style engine defaults to Waterripple
Kaptcha.word.impl captcha text character rendering defaults to Defaultwordrenderer
Kaptcha.background.impl Verification Code Background generator defaults to Defaultbackground
Kaptcha.background.clear.from Verification Code Background color progressive defaults to Color.light_gray
Kaptcha.background.clear.to Verification Code Background color progressive defaults to Color.White
Kaptcha.image.width Verification Code picture width defaults to 200
Kaptcha.image.height Verification Code Picture height defaults to 50



GOOGLE Kaptcha Verification Code generation plug-in servlet/jsp project use steps

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.