Development tools: eclipse, kaptcha-2.3.jar package.
1. Create a Web project;
2. Create a new Jsp page (the content includes a text box, an image container, and a submit button)
<body> <img alt="random" src="randomcode.jpg" onclick="changeR(this)" style="cursor: pointer;"> <form action="check.jsp"> <input type="text" name="r"> <input type="submit" value="s"> </form> </body>
3. Yes It can be seen that the source of the image verification code (src="randomcode.jpg") needs to be configured with the Web.xml file. (Leave it 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>
4. Since the kaptcha-2.3.jar package is required, import the downloaded jar package into lib. (Just copy and paste)
Others:
1. Attributes of web page verification code
(1) Add border
<servlet> <init-param> <description>图片边框,合法值:yes , no</description> <param-name>kaptcha.border</param-name> <param-value>yes</param-value> <!-- yes 或者 no--> </init-param> </servlet>
(2) Border Color
<init-param> <description> 边框颜色,合法值: r,g,b (and optional alpha) 或者white,black,blue. </description> <param-name>kaptcha.border.color</param-name> <param-value>black</param-value> </init-param>
(3) Border thickness
<init-param> <description>边框厚度,合法值:>大于0 </description> <param-name>kaptcha.border.thickness</param-name> <param-value>1</param-value> </init-param>
(4) Image width
<init-param> <description>图片宽 200</description> <param-name>kaptcha.image.width</param-name> <param-value>200</param-value> </init-param>
(5) Image height
<init-param> <description>图片高 50</description> <param-name>kaptcha.image.height</param-name> <param-value>50</param-value> </init-param>
(6) Verification code collection
<init-param> <description>文本集合,验证码值从此集合中获取</description> <param-name>kaptcha.textproducer.char.string</param-name> <param-value>1234567890</param-value> <!--纯数字 --> //<param-value>abcde2345678gfynmnpwx</param-value> <!-- 文字加英文--> </init-param>
(7) Verification code length
<init-param> <description>验证码长度 默认是5 </description> <param-name>kaptcha.textproducer.char.length</param-name> <param-value>2</param-value> </init-param>
(8) Font
<init-param> <description>字体 Arial, Courier</description> <param-name>kaptcha.textproducer.font.names</param-name> <param-value>Arial, Courier</param-value> </init-param>
(9) Font size
<init-param> <description>字体大小 40px.</description> <param-name>kaptcha.textproducer.font.size</param-name> <param-value>40</param-value> </init-param>
(10) Font color
<init-param> <description> 字体颜色,合法值: r,g,b 或者 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>文字间隔 2</description> <param-name>kaptcha.textproducer.char.space</param-name> <param-value>2</param-value> </init-param>
(Twelve) Interference Realization
<init-param> <description>干扰实现类</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>
(Thirteen) Interference Color
<init-param> <description> 干扰颜色,合法值: r,g,b 或者 white,black,blue. </description> <param-name>kaptcha.noise.color</param-name> <param-value>black</param-value> </init-param>
(Fourteen) Background style
<init-param> <description> 图片样式: 水纹com.google.code.kaptcha.impl.WaterRipple 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy 阴影com.google.code.kaptcha.impl.ShadowGimpy </description> <param-name>kaptcha.obscurificator.impl</param-name> <param-value> com.google.code.kaptcha.impl.WaterRipple </param-value> </init-param>
(Fifteen) Background implementation class
<init-param> <description>背景实现类</description> <param-name>kaptcha.background.impl</param-name> <param-value> com.google.code.kaptcha.impl.DefaultBackground </param-value> </init-param>
(Sixteen) Background gradient color
<init-param> <description>背景颜色渐变,开始颜色</description> <param-name>kaptcha.background.clear.from</param-name> <param-value>green</param-value> </init-param> <init-param> <description>背景颜色渐变,结束颜色</description> <param-name>kaptcha.background.clear.to</param-name> <param-value>white</param-value> </init-param>
(17) Text renderer
<init-param> <description> 文字渲染器 </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, and the value is
<init-param> <description> session中存放验证码的key键 </description> <param-name>kaptcha.session.key</param-name> <param-value>KAPTCHA_SESSION_KEY</param-value> </init-param>
(19) Image implementation category
<init-param> <description>图片实现类</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 rewrite this class to implement the verification code in Chinese)
<init-param> <description>文本实现类</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, inherit Configurable and implement 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 length = 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(26) + 65; firstWord = String.valueOf((char) tempInt); break; case 2: int r1, r2, r3, r4; String strH, strL;// high&low r1 = rand.nextInt(3) + 11; // 前闭后开[11,14) if (r1 == 13) { r2 = rand.nextInt(7); } else { r2 = rand.nextInt(16); } r3 = rand.nextInt(6) + 10; if (r3 == 10) { r4 = rand.nextInt(15) + 1; } else if (r3 == 15) { r4 = rand.nextInt(15); } 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, 16)); bytes[1] = (byte) (Integer.parseInt(strL, 16)); firstWord = new String(bytes); break; default: tempInt = rand.nextInt(10) + 48; firstWord = String.valueOf((char) tempInt); break; } finalWord += firstWord; } return finalWord; } }
2. Modify the Web.xml configuration
<init-param> <description>文本实现类</description> <param-name>kaptcha.textproducer.impl</param-name> <param-value> ChineseText </param-value> </init-param> 五、验证码的校验(本文是利用check.jsp来校验的)保存在Session中,其中的键值为(第十八个属性) [html] view plain copy <body> <% // 检查是否是正确的验证码 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>
6. Extension (implementation of addition verification code)
1. Rewrite 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 the method introduced by the editor to use open source tools to create web page verification codes. , I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank you all for your support of the PHP Chinese website!
For more related articles on how to use open source tools to create web page verification codes, please pay attention to the PHP Chinese website!