Springmvc uses cx-common to implement the verification code function. springmvccx-common
Please indicate the source of the post: http://blog.csdn.net/kouwoo/article/details/42675201
Springmvc configuration will not be detailed. Here we will post the code of the key points.
Code. jsp
<% @ Page language = "java" contentType = "text/html; charset = UTF-8 "pageEncoding =" UTF-8 "%> <% @ taglib uri =" http://www.springframework.org/tags/form "prefix =" form "%> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
CodeController. java
Package net. spring. controller; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import javax. servlet. http. httpSession; import org. springframework. stereotype. controller; import org. springframework. web. bind. annotation. requestMapping; import org. springframework. web. bind. annotation. requestMethod; import org. springframework. web. bind. annotation. requestParam; import com. Cx. web. common. util. randomValidateCode; @ Controllerpublic class CodeController {@ RequestMapping (value = "code", method = RequestMethod. GET) public String code (HttpSession session, HttpServletRequest request) {session. setAttribute ("w", request. getContextPath (); return "code";}/*** get the Verification code * @ param response * @ param request */@ RequestMapping ("im ") public void validationImg (HttpServletResponse response, H TtpServletRequest request) {response. setContentType ("image/jpeg"); // set the corresponding type to notify the browser that the output content is image response. setHeader ("Pragma", "No-cache"); // sets the response header to tell the browser not to cache the response. setHeader ("Cache-Control", "no-cache"); response. setDateHeader ("Expire", 0); RandomValidateCode randomValidateCode = new RandomValidateCode (); try {randomValidateCode. getRandcode (request, response); // method of output image} catch (Exception e) {}}/*** verify The verification code is correct * @ param validatecode * @ param session * @ return */@ RequestMapping (value = "validate", method = RequestMethod. POST) public String validate (@ RequestParam String validatecode, HttpSession session) {// obtain the codeString validateC = (String) session in the session. getAttribute (RandomValidateCode. RANDOMCODEKEY); if (validatecode = null | "". equals (validatecode) {// The verification code entered is null // TODO} if (! ValidateC. equals (validatecode. toUpperCase () {// The entered verification code is incorrect // TODO} return null ;}}