How to create a Web page verification code using open source Tools

Source: Internet
Author: User
Tags border color
Development tools: Eclipse, Kaptcha-2.3.jar package.

First, create a Web project;

Second, create a new JSP page (content, a text box, a picture container, a submit button)

<body><form action= "check.jsp" ><input type= "text" name= "R" ><input type= "Submit" value= "s" ></form ></body>

Third, you can see the source of the Image verification code (src= "randomcode.jpg") need to configure the Web. xml file. (given to the servlet (the servlet is in Kaptcha-2.3.jar) for processing)

<servlet><servlet-name>Kaptcha</servlet-name><servlet-class> Com.google.code.kaptcha.servlet.kaptchaservlet</servlet-class></servlet><servlet-mapping> <servlet-name>kaptcha</servlet-name><url-pattern>/randomcode.jpg</url-pattern></ Servlet-mapping>

Iv. due to the need for Kaptcha-2.3.jar package, the downloaded jar package is imported into lib. (Copy and paste)

Other:

I. Properties of the Web verification Code

(i) Add a border

<servlet><init-param><description> picture border, legal value: Yes, no</description><param-name> Kaptcha.border</param-name><param-value>yes</param-value> <!--Yes or No--></init-param ></servlet>

(b) Border color

<init-param><description> border color, legal value: r,g,b (and optional alpha) or white,black,blue.</description> <param-name>kaptcha.border.color</param-name><param-value>black</param-value></ Init-param>

(c) Border thickness

<init-param><description> border thickness, legal value:> greater than 0 </description><param-name> Kaptcha.border.thickness</param-name><param-value>1</param-value></init-param>

(d) Picture width

<init-param><description> Picture Width 200</description><param-name>kaptcha.image.width</ Param-name><param-value>200</param-value></init-param>

(v) Picture height

<init-param><description> Pictures High 50</description><param-name>kaptcha.image.height</ Param-name><param-value>50</param-value></init-param>

(vi) Verification Code collection

<init-param><description> text Collection, validation code values get from this collection </description><param-name> Kaptcha.textproducer.char.string</param-name><param-value>1234567890</param-value> <!--Pure Digital -->//<param-value>abcde2345678gfynmnpwx</param-value> <!--text plus English--></init-param>

(vii) Verification code length

<init-param><description> Verification Code length default is 5 </description><param-name> Kaptcha.textproducer.char.length</param-name><param-value>2</param-value></init-param>

(eight) font

<init-param><description> Font Arial, courier</description><param-name> Kaptcha.textproducer.font.names</param-name><param-value>arial, courier</param-value></ Init-param>


(ix) Font size

<init-param><description> Font Size 40px.</description><param-name> Kaptcha.textproducer.font.size</param-name><param-value>40</param-value></init-param>


(10) Font Color

<init-param><description> font Color, legal value: R,g,b or white,black,blue.</description><param-name> Kaptcha.textproducer.font.color</param-name><param-value>black</param-value></init-param >


(11) The interval between each verification code

<init-param><description> text Interval 2</description><param-name> Kaptcha.textproducer.char.space</param-name><param-value>2</param-value></init-param>


(12) Interference realization

<init-param><description> interference Implementation Class </description><param-name>kaptcha.noise.impl</ param-name><param-value><!--Com.google.code.kaptcha.impl.NoNoise-- Com.google.code.kaptcha.impl.defaultnoise</param-value></init-param>


(13) Interference color

<init-param><description> interference color, legal value: R,g,b or white,black,blue.</description><param-name> Kaptcha.noise.color</param-name><param-value>black</param-value></init-param>


(14) Background style

<init-param><description> Picture Style: Watermark Com.google.code.kaptcha.impl.WaterRipple Fish Eye com.google.code.kaptcha.impl.FishEyeGimpy Shadow Com.google.code.kaptcha.impl.Shado Wgimpy</description><param-name>kaptcha.obscurificator.impl</param-name><param-value> Com.google.code.kaptcha.impl.waterripple</param-value></init-param>


(15) Background implementation class

<init-param><description> Background Implementation class </description><param-name>kaptcha.background.impl</ param-name><param-value>com.google.code.kaptcha.impl.defaultbackground</param-value></ Init-param>

(16) Background gradient color

<init-param><description> background color gradient, starting color </description><param-name> Kaptcha.background.clear.from</param-name><param-value>green</param-value></init-param ><init-param><description> background color gradient, end color </description><param-name> Kaptcha.background.clear.to</param-name><param-value>white</param-value></init-param>

(17) Text renderer

<init-param><description> Text Renderer </description><param-name>kaptcha.word.impl</ param-name><param-value>com.google.code.kaptcha.text.impl.defaultwordrenderer</param-value>< /init-param>

(18) The verification code of the image will be saved in the session, where the value is

<init-param><description> key key to store verification code in session </description><param-name> kaptcha.session.key</param-name><param-value>kaptcha_session_key</param-value></ Init-param>

(19) Image Realization Category

<init-param><description> Picture Implementation Class </description><param-name>kaptcha.producer.impl</ param-name><param-value>com.google.code.kaptcha.impl.defaultkaptcha</param-value></ Init-param>

(20) Text implementation class (You can override this class to implement a CAPTCHA in Chinese)

<init-param><description> Text Implementation class </description><param-name>kaptcha.textproducer.impl</ param-name><param-value>com.google.code.kaptcha.text.impl.defaulttextcreator</param-value></ Init-param>

Rewrite the text implementation class to implement the verification code in Chinese:

1. Create a category that inherits the configurable implementation textproducer (in the jar package)

Import Com.google.code.kaptcha.text.textproducer;import Com.google.code.kaptcha.util.configurable;import Java.util.random;public class Chinesetext extends configurable implements Textproducer {public String getText () {int Leng th = GetConfig (). Gettextproducercharlength (); String Finalword = "", FirstWord = ""; int tempint = 0; String[] Array = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "B", "C", "D", "E", "F"}; Random rand = new Random (), for (int i = 0, i < length; i++) {switch (Rand.nextint (array.length)) {Case 1:tempint = Rand . Nextint (+) + 65;firstword = string.valueof ((char) tempint); Break;case 2:int r1,r2,r3,r4; String strh,strl;//high&lowr1 = Rand.nextint (3) + 11; Front closed open [11,14] if (r1 = =) {r2 = Rand.nextint (7);} else {r2 = Rand.nextint (16);} R3 = Rand.nextint (6) + 10;if (R3 = =) {R4 = Rand.nextint (+ 1),} else if (r3 = =) {R4 = Rand.nextint);} else {R4 = Rand.nextint (16);} STRH = Array[r1] + Array[r2];strl = Array[r3] + array[r4];byte[] bytes = new byte[2];bytes[0] = (byte) (Integer.parseint (STRH, +)); bytes[1] = (byte) (Integer.parseint (StrL, +)); FirstWord = new String (b ytes); break;default:tempint = Rand.nextint (+ 48;firstword = string.valueof (char) tempint); Finalword + = FirstWord;} return Finalword;}}

2. Modify the Web. XML configuration

<init-param><description> Text Implementation class </description><param-name>kaptcha.textproducer.impl</ Param-name><param-value>chinesetext</param-value></init-param> Five, Validation code verification (This article is verified using check.jsp) is saved in the session, where the key value is (18th attribute) [HTML] View plain copy<body><%//check is the correct captcha string k = ( String) Session.getattribute (Com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY); String str = request.getparameter ("R"), if (K.equals (str)) Out.print ("true"), Out.print (k + "---" + str);%></body >

VI. extension (Implementation of the addition verification Code)

1. Overriding the Kaptchaservlet class

Import Com.google.code.kaptcha.producer;import Com.google.code.kaptcha.util.config;import Java.awt.image.bufferedimage;import Java.io.ioexception;import Java.util.enumeration;import java.util.Properties; Import Javax.imageio.imageio;import Javax.servlet.servlet;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 Javax.servlet.http.httpsession;public class Kaptchaservlet extends HttpServlet implements servlet {private Properties Props;private Producer kaptchaproducer;private String sessionkeyvalue;public kaptchaservlet () {this.props = new Properties (); this.kaptchaproducer = Null;this.sessionkeyvalue = null;} public void init (ServletConfig conf) throws Servletexception {super.init (conf); Imageio.setusecache (false); Enumeration initparams = Conf.getinitparameternames (); while (Initparams.hasmoreelemEnts ()) {string key = (string) initparams.nextelement (); String value = Conf.getinitparameter (key); This.props.put (key, value);} Config config = new config (this.props); this.kaptchaproducer = Config.getproducerimpl (); this.sessionkeyvalue = Config.getsessionkey ();} public void Doget (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException { Resp.setdateheader ("Expires", 0L), Resp.setheader ("Cache-control", "No-store, No-cache, must-revalidate"); Resp.addheader ("Cache-control", "post-check=0, pre-check=0"), Resp.setheader ("Pragma", "No-cache"); Resp.setcontenttype ("Image/jpeg"); String Captext = This.kaptchaProducer.createText (); String S1 = captext.substring (0, 1); String s2 = captext.substring (1, 2), int r = integer.valueof (S1). Intvalue () + integer.valueof (S2). Intvalue (); Req.getsession (). SetAttribute (This.sessionkeyvalue, string.valueof (R)); BufferedImage bi = this.kaptchaProducer.createImage (s1+ "+" +s2+ "=?"); Servletoutputstream out = Resp.getoutputstream (); Imageio.write (bI, "JPG", out); try {Out.flush ();} finally {out.close ();}}} 

2. Modify the configuration file

<servlet><servlet-name>kaptcha</servlet-name><servlet-class>kaptchaservlet</ Servlet-class></servlet>

The above is a small part of the introduction of the use of open source tools to create a Web page verification code method, I hope we have some help, if you have any questions please give me a message, small series will promptly reply to you. Thank you very much for your support for topic.alibabacloud.com!

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.