Springmvc uses cx-common to implement the verification code Function

Source: Internet
Author: User

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 ;}}


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.