JSP uses Kaptcha plug-in to generate digital operation graphics verification code

Source: Internet
Author: User
Tags character set html page

1. Download Kaptcha compressed files from the official website https://code.google.com/p/kaptcha/, after extracting the file there is a war file, open the Eclipse/myeclipse import it into, and then deploy to the server, In the browser to enter the URL to see the Kaptcha officially provided by the operation of the basic demo, now change it to addition calculation verification.

2. First look at the Web.xml file found that the servlet used to generate the CAPTCHA is Kaptchaservlet


3. Locate the Kaptchaservlet.class file, and then decompile it.

4. Create a new own verification code Mykaptchaservlet, will be back to compile the source code copy in.

5. Make the following modifications to the Mykaptchaservlet.

After the modified Mykaptchaservlet.java code is as follows:


Package com.xhc.kaptchaServlet;

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.Date;
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 Mykaptchaservlet extends HttpServlet implements Servlet {
Private Properties props;
Private Producer Kaptchaproducer;
Private String Sessionkeyvalue;
Private String Sessionkeydatevalue;

Public Mykaptchaservlet () {
This.props = new Properties ();

This.kaptchaproducer = null;

This.sessionkeyvalue = null;

This.sessionkeydatevalue = 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 ();
This.sessionkeydatevalue = Config.getsessiondate ();
}

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 str1 = captext.substring (0, 2);
String str2 = captext.substring (2, 4);
int result = Integer.valueof (str1) + integer.valueof (STR2);
Req.getsession (). setattribute (This.sessionkeyvalue, result+ "");

Req.getsession (). setattribute (This.sessionkeydatevalue, New Date ());

BufferedImage bi = this.kaptchaProducer.createImage (str1 + "+" + str2
+ "=?");

Servletoutputstream out = Resp.getoutputstream ();

Imageio.write (BI, "JPG", out);
}
}


6. Modify the Web.xml configuration file and assign Servlet-class to Mykaptchaservlet


Refer to official documentation

Https://code.google.com/p/kaptcha/wiki/ConfigParameters Other properties are also configured, after the modified Web.xml file is as follows:

<?xml version= "1.0" encoding= "UTF-8"?>

<web-app xmlns= "Http://java.sun.com/xml/ns/j2ee" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version= "2.4" >

<servlet>
<servlet-name>Kaptcha</servlet-name>
<!--<servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>-->
<servlet-class>com.xhc.kaptchaServlet.MyKaptchaServlet</servlet-class>
<!--for a complete list of Init Parameters, See:http://code.google.com/p/kaptcha/wiki/configparameters-->
<!--verification code boundary, valid value Yes or no-->
<init-param>
<param-name>kaptcha.border</param-name>
<param-value>yes</param-value>
</init-param>
<!--boundary color, legal value r,g,b (and optional alpha) or White,black,blue-->
<init-param>
<param-name>kaptcha.border.color</param-name>
<param-value>black</param-value>
</init-param>
<!--boundary thickness, the legal value is greater than 0-->
<init-param>
<param-name>kaptcha.border.thickness</param-name>
<param-value>1</param-value>
</init-param>
<!--verification code picture width-->
<init-param>
<param-name>kaptcha.image.width</param-name>
<param-value>200</param-value>
</init-param>
<!--verification code picture height-->
<init-param>
<param-name>kaptcha.image.height</param-name>
<param-value>50</param-value>
</init-param>
<!--validation code picture generation class, by default generated by the Defaultkaptcha class, you can write a picture generation class, you need to implement producer interface and inherit configurable
Class-->
<init-param>
<param-name>kaptcha.producer.impl</param-name>
<param-value>com.google.code.kaptcha.impl.DefaultKaptcha</param-value>
</init-param>
<!--validation code generation class, by default generated by the Defaulttextcreator class, you can write a validation code generation class, you need to implement Textproducer interface and inherit configurable
Class-->
<init-param>
<param-name>kaptcha.textproducer.impl</param-name>
<param-value>com.google.code.kaptcha.text.impl.DefaultTextCreator</param-value>
</init-param>
<!--character set used to generate Authenticode-->
<init-param>
<param-name>kaptcha.textproducer.char.string</param-name>
<param-value>01234565789</param-value>
</init-param>
<!--the length of the generated verification code-->
<init-param>
<param-name>kaptcha.textproducer.char.length</param-name>
<param-value>4</param-value>
</init-param>
<!--authentication code font, multiple values separated by commas-->
<init-param>
<param-name>kaptcha.textproducer.font.names</param-name>
<param-value>Arial,Courier</param-value>
</init-param>
<!--font size-->
<init-param>
<param-name>kaptcha.textproducer.font.size</param-name>
<param-value>40</param-value>
</init-param>
<!--font Color-->
<init-param>
<param-name>kaptcha.textproducer.font.color</param-name>
<param-value>blue</param-value>
</init-param>
<!--verification code character interval-->
<init-param>
<param-name>kaptcha.textproducer.char.space</param-name>
<param-value>5</param-value>
</init-param>
<!--image interference generation class, default by the Defaultnoise class generation, you can write a picture of the interference class, you need to implement Noiseproducer interface and inherit configurable
Class-->
<init-param>
<param-name>kaptcha.noise.impl</param-name>
<param-value>com.google.code.kaptcha.impl.DefaultNoise</param-value>
</init-param>
<!--interference color-->
<init-param>
<param-name>kaptcha.noise.color</param-name>
<param-value>yellow</param-value>
</init-param>
<!--get a distorted, different style of verification code, the official implementation of the Shadowgimpy,fisheyegimpy,waterripple, which used to fisheyegimpy after the above set interference line does not work-->
<init-param>
<param-name>kaptcha.obscurificator.impl</param-name>
<param-value>com.google.code.kaptcha.impl.FishEyeGimpy</param-value>
</init-param>
<!--gradient background color, initial color-->
<init-param>
<param-name>kaptcha.background.clear.from</param-name>
<param-value>gray</param-value>
</init-param>
<!--gradient in Beijing, final color-->
<init-param>
<param-name>kaptcha.background.clear.to</param-name>
<param-value>white</param-value>
</init-param>
<!--text renderer-->
<init-param>
<param-name>kaptcha.word.impl</param-name>
<param-value>com.google.code.kaptcha.text.impl.DefaultWordRenderer</param-value>
</init-param>
<init-param>
<param-name>kaptcha.session.key</param-name>
<param-value>KAPTCHA_SESSION_KEY</param-value>
</init-param>
<init-param>
<param-name>kaptcha.session.date</param-name>
<param-value>KAPTCHA_SESSION_DATE</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>Kaptcha</servlet-name>
<url-pattern>/Kaptcha.jpg</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>KaptchaExample.jsp</welcome-file>
</welcome-file-list>
</web-app>


7. Effect drawing

Java Auto-generate verification code plug-in-kaptcha


Kaptcha a very useful authentication code plug-in, Java version, a very good plug-in, only with a simple set of several properties in the Web.xml, a very beautiful verification code came out. Other parameters can be set themselves, the most bull is to provide the interface, you can define OH.

HTML page code

<form action= "Submit.action" >
<input type= "text" name= "Kaptcha" value= ""/>
</form>

Web.xml Configuration Code

<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>/kaptcha.jpg</url-pattern>
</servlet-mapping>

Java code

String kaptchaexpected = (string) request.getsession ()
. getattribute (Com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
String kaptchareceived = Request.getparameter ("Kaptcha");

if (kaptchareceived = null | |!kaptchareceived.equalsignorecase (kaptchaexpected))
{
SetError ("Kaptcha", "Invalid validation code.");
}

Click to display code Refresh method JS method

<script type= "Text/javascript" >
$ (function () {
$ (' #kaptchaImage '). Click (function () {
$ (this). attr (' src ', '/kaptcha.jpg ' + Math.floor (math.random () *100));
})
});
</script>
&LT;BR/><small>can ' t read the image? Click it to get a new one.</small>

Other parameter settings

<init-param>
<param-name>kaptcha.border</param-name>
<param-value>yes</param-value>
</init-param>
<init-param>
<param-name>kaptcha.border.color</param-name>
<param-value>105,179,90</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.font.color</param-name>
<param-value>black</param-value>
</init-param>
<init-param>
<param-name>kaptcha.image.width</param-name>
<param-value>500</param-value>
</init-param>
<init-param>
<param-name>kaptcha.image.height</param-name>
<param-value>300</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.font.size</param-name>
<param-value>90</param-value>
</init-param>
<init-param>
<param-name>kaptcha.session.key</param-name>
<param-value>code</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.char.length</param-name>
<param-value>4</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.font.names</param-name>
<param-value> Arial, italics, Microsoft Ya-Black </param-value>
</init-param>


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.