Springmvc uses cx-common to implement the verification code Function
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 "%>
Insert title here<Script type = "text/javascript" src = "js/jquery-1.7.2.min.js"> </script> <script type = "text/javascript"> function refresh (obj) {var w = "$ {w}"; obj. src = w + "/im.html? "+ Math. random (); // Add Math. random (); to prevent the browser from caching $ ("# validatecode "). attr ("value", "") ;}</script>
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 ;}}