The Kaptcha verification code is Google's verification code plugin, which is relatively simple to use, the interference lines set and the font distortion is not easy for others to read the hack.
Here we need to import a Kaptcha-2.3.jar
: Http://pan.baidu.com/s/1dDzw7Bj
There is a kaptchaservlet servlet in the jar package, and each access is saved in the session scope
The name saved by default is Kaptcha_session_key you can also set your own saved name
Look at the comments in the XML "key key for verifying code in Session" Configuration, and you can also set the parameter properties of other verification codes according to individual requirements.
Web. XML configuration
<servlet>
<servlet-name>kaptcha</servlet-name>
<servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>
<!--<init-param>
<description> picture border, legal value: Yes, no</description>
<param-name>kaptcha.border</param-name>
<param-value>yes</param-value>
</init-param>
<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>
<init-param>
<description> border thickness, legal value:>0</description>
<param-name>kaptcha.border.thickness</param-name>
<param-value>1</param-value>
</init-param>
<init-param>
<description> Picture Width 200</description>
<param-name>kaptcha.image.width</param-name>
<param-value>200</param-value>
</init-param>
<init-param>
<description> Pictures High 50</description>
<param-name>kaptcha.image.height</param-name>
<param-value>50</param-value>
</init-param>
<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>
<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>
<init-param>
<description> text Collection, validation code values get from this collection </description>
<param-name>kaptcha.textproducer.char.string</param-name>
<param-value>QWERTYUIOPASDFGHJKLZXCVBNM1234567890</param-value>
</init-param>
<init-param>
<description> Verification Code Length 5</description>
<param-name>kaptcha.textproducer.char.length</param-name>
<param-value>5</param-value>
</init-param>
<init-param>
<description> Font Arial, courier</description>
<param-name>kaptcha.textproducer.font.names</param-name>
<param-value>arial, courier</param-value>
</init-param>
<init-param>
<description> Font Size 40px.</description>
<param-name>kaptcha.textproducer.font.size</param-name>
<param-value>40</param-value>
</init-param>
<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>
<init-param>
<description> text Interval 2</description>
<param-name>kaptcha.textproducer.char.space</param-name>
<param-value>2</param-value>
</init-param>
<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>
<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>
<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.ShadowGimpy
</description>
<param-name>kaptcha.obscurificator.impl</param-name>
<param-value>
Com.google.code.kaptcha.impl.WaterRipple
</param-value>
</init-param>
<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>
<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>
<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>
<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>
<init-param>
<description>
The date the Kaptcha is generated was put into the
HttpSession. This is the key value for the. Item in the
Session.
</description>
<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>/randomcode.jpg</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>com.test.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/login.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
index.jsp Code
<form action= "<%=request.getcontextpath ()%>/login.do" method= "Get" >
<center>
<div>
CAPTCHA: <input type= "text" name= "Code2"/>
/randomcode.jpg" id= "Code2" onclick= "Relodcode (This, ' Randomcode.jpg ') "/>
</div>
<input type= "Submit" value= "Submission"/>
</center>
</form>
<script type= "Text/javascript" >
function Relodcode (obj,url) {
var time=new Date ();
Obj.src= "<%=request.getcontextpath ()%>/" +url+ "? time=" +time;
}
</script>
Submit Validation Servlet
Import java.io.IOException;
Import Java.io.PrintWriter;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
public class Loginservlet extends HttpServlet {
@Override
protected void Doget (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException {
Get Kaptchaservlet Save generated verification code
String code= (String) req.getsession (). getattribute (Com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
String code2=req.getparameter ("Code2");
System.out.println ("code=" +code);
System.out.println ();
PrintWriter Out=null;
String msg= "Incorrect input!";
try {
Resp.setcontenttype ("Text/html;charset=utf-8");
Out=resp.getwriter ();
if (code2.equals (code)) {
msg= "input correct!";
}
Out.print (msg+ "\tcode=" +code+ "\tcode2=" +code2);
} catch (Exception e) {
E.printstacktrace ();
}finally{
if (null!=out) out.close ();
}
Super.doget (req, resp);
}
@Override
protected void DoPost (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException {
Doget (req, resp);
}
}
(Click Verification Code Refresh)
Java implementation Verification code making one of the Kaptcha verification code