Spring mvc captcha verification code implementation (with Servlet implementation)

Source: Internet
Author: User

Note that spring MVC is implemented through annotation. If you want to know how to use spring annotation, go somewhere else, or I will write a new article later!

Prerequisites: the jar package kaptcha-2.3.2.jar is required as follows:

Http://code.google.com/p/kaptcha/downloads/list

1. Servlet implementation method

1.1web.xml is as follows:

<! -- Kaptcha verification code --> <servlet> <! -- Generate the verification code and refresh the servlet --> <servlet-Name> kaptcha </servlet-Name> <servlet-class> COM. google. code. kaptcha. servlet. kaptchaservlet </servlet-class> <init-param> <! -- The border of the Verification Code image is yes and no --> <param-Name> kaptcha. border </param-Name> <param-value> Yes </param-value> </init-param> <! -- Color of the border of the Verification Code image --> <param-Name> kaptcha. border. color </param-Name> <param-value> 105,179, 90 </param-value> </init-param> <! -- Verification code color --> <param-Name> kaptcha. textproducer. font. color </param-Name> <param-value> Red </param-value> </init-param> <! -- The width of the entire verification code in the image --> <param-Name> kaptcha. image. width </param-Name> <param-value> 250 </param-value> </init-param> <! -- Height of the entire verification code in the image --> <param-Name> kaptcha. image. height </param-Name> <param-value> 90 </param-value> </init-param> <! -- Size of the verification code in the image --> <param-Name> kaptcha. textproducer. font. size </param-Name> <param-value> 70 </param-value> </init-param> <! -- Get the verification code name session --> <param-Name> kaptcha. session. key </param-Name> <param-value> Code </param-value> </init-param> <! -- Display several verification codes --> <param-Name> kaptcha. textproducer. char. length </param-Name> <param-value> 4 </param-value> </init-param> <! -- Font and style of the verification code --> <param-Name> kaptcha. textproducer. font. names </param-Name> <param-value>,, </param-value> </init-param> </servlet> <servlet-mapping> <servlet-Name> kaptcha </servlet-Name> <URL-pattern> /kaptcha/kaptcha.jpg </url-pattern> </servlet-mapping>

1.2 The JSP code is as follows:

<table><tr><td></td><td valign="top"><form method="POST"><br>sec code:<input type="text" name="kaptchafield"><br /><input type="submit" name="submit"></form></td></tr></table><br /><br /><br /><br /><%String c = (String)session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);String parm = (String) request.getParameter("kaptchafield");out.println("Parameter: " + parm + " ? Session Key: " + c + " : ");if (c != null && parm != null) {if (c.equals(parm)) {out.println("<b>true</b>");} else {out.println("<b>false</b>");}%>

II. Implementation of spring MVC

2.1 spring configuration file

<Bean id = "captchaproducer" class = "com. google. code. kaptcha. impl. defaultkaptcha "> <property name =" Config "> <Bean class =" com. google. code. kaptcha. util. config "> <constructor-Arg> <props> <prop key =" kaptcha. border "> Yes </prop> <prop key =" kaptcha. border. color "> 105,179, 90 </prop> <prop key =" kaptcha. textproducer. font. color "> blue </prop> <prop key =" kaptcha. image. width "> 125 </prop> <prop key =" kaptcha. image. height "> 45 </prop> <prop key =" kaptcha. textproducer. font. size "> 45 </prop> <prop key =" kaptcha. session. key "> Code </prop> <prop key =" kaptcha. textproducer. char. length "> 4 </prop> <prop key =" kaptcha. textproducer. font. names ">,, </prop> </props> </constructor-Arg> </bean> </property> </bean>


2.2 controller implementation

Package COM. vopzoon. app. base. CAPTCHA; import Java. AWT. image. bufferedimage; import javax. imageIO. imageIO; import javax. servlet. servletoutputstream; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; import javax. servlet. HTTP. httpsession; import Org. springframework. beans. factory. annotation. autowired; import Org. springframework. stereotype. controller; import Org. springframework. web. BIND. annotation. requestmapping; import Org. springframework. web. servlet. modelandview; import COM. google. code. kaptcha. constants; import COM. google. code. kaptcha. producer;/*** prevent CAPTCHA bot from logging in * @ author liuwang **/@ controller @ requestmapping ("/kaptcha/*") public class captchacontroller {@ autowiredprivate producer captchaproducer = NULL; @ requestmappingpublic modelandview getkaptchaimage (httpservletrequest request, httpservletresponse response) throws exception {httpsession session = request. getsession (); string code = (string) session. getattribute (constants. kaptcha_session_key); system. out. println ("******************** the verification code is: "+ code +" ******************* "); response. setdateheader ("expires", 0); // set standard HTTP/1.1 no-Cache headers. response. setheader ("cache-control", "No-store, no-cache, must-revalidate"); // set ie extended HTTP/1.1 no-Cache headers (use addheader ). response. addheader ("cache-control", "post-check = 0, pre-check = 0"); // set standard HTTP/1.0 no-Cache header. response. setheader ("Pragma", "No-Cache"); // return a response. setcontenttype ("image/JPEG"); // create the text for the imagestring captext = captchaproducer. createtext (); // store the text in the Sessionsession. setattribute (constants. kaptcha_session_key, captext); // create the image with the textbufferedimage Bi = captchaproducer. createimage (captext); servletoutputstream out = response. getoutputstream (); // write the data outimageio. write (Bi, "jpg", out); try {out. flush ();} finally {out. close () ;}return null ;}}


2.3 JSP code

<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8" pageencoding = "UTF-8" %> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd"> <HTML> 

Oh, I almost forgot it. Sorry, I copied it from him. The address is as follows:

Http://ttaale.iteye.com/blog/808719


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.