Spring MVC page Validation code sample

Source: Internet
Author: User


Verification Code class:

Import java.io.IOException;

Import javax.servlet.ServletException;

Import Javax.servlet.http.HttpServlet;

Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;

Import javax.servlet.http.HttpSession;

public class Verifycodeservlet extends HttpServlet {

Private static final long serialversionuid = 1L;

Public Verifycodeservlet () {

Super ();

}

protected void doget (HttpServletRequest request, httpservletresponse response)

Throws Servletexception, IOException {

DoPost (request, response);

}

protected void DoPost (HttpServletRequest request, HttpServletResponse resp) throws Servletexception, IOException {

Disable image caching so that you can click the captcha to refresh the captcha picture

String randsource[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "Q", "W", "E", "R", "T", "Y", "U",

"I", "O", "P", "a", "s", "D", "F", "G", "H", "J", "K", "Z", "X", "C", "V", "B", "N", "M"};

String randstr = "";

HttpSession session = Request.getsession ();//Get a Session object

Resp.setheader ("Pragma", "NoCache");

Resp.setheader ("Cache-control", "No-cache");

Resp.setdateheader ("Expires", 0);

Resp.setcontenttype ("Image/jpeg");

Createimagecode Vcode = new Createimagecode (100, 30, 5, 10);

Session.setattribute ("Verifycodefind", Vcode.getcode ());

Vcode.write (Resp.getoutputstream ());

}

}


JSP page:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<title>sf</title>

<form id= "FormData" method= "post" action= "${pagecontext.request.contextpath}/save" >

<label><span> name </span>

<input id= "UName" type= "text" name= "UserName"/>

</label>

<label><span> Verification Code </span>

<input type= "text" style= "width:100px;height:30px" name= "Yzm" id= "Yzm" tabindex= "3" datatype= "Limit" min= "1" msg= " Verification code cannot be empty "class=" I-text "/>

style=" vertical-align:middle; margin:0px; cursor:pointer "title=" Click Change Verification Code border= 0/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<input type= "button" value= "Confirm Commit" class= "tj-btn" id= "TJ" >

</form>

<script type= "Text/javascript" >

$ (' #tj '). Click (function () {

$.ajax ({

Type: ' POST ',

URL: ' ${pagecontext.request.contextpath}/save ',

Data: $ (' #formData '). Serialize (),

DataType: ' JSON ',

Success:function (data) {

if (data.result==1) {

$ (' #formData ') [0].reset ();

$ (' #changecode '). attr (' src ', '/sf/verifycodeservlet?. Tmp= ' + math.random ());

Alert (' Submit success ');

}else{

$ (' #changecode '). Click ();

alert (data.errormessage);

    }

},

DataType: ' JSON '

            })

});

$ (' #changecode '). Click (function () {

$ (this). attr (' src ', '/sf/verifycodeservlet?. Tmp= ' + math.random ());

$ (' #yzm '). Focus (). Val (');

});

</script>

</body>


Control layer:

@Controller

@RequestMapping ("/SF")

public class Sfcontroller {

@RequestMapping (value = "/save", method = Requestmethod.post)

@ResponseBody

Public Baseajaxresponse Save (DTO dto,httpservletrequest request, String Yzm) {

Baseajaxresponse res = new Baseajaxresponse ();

if (Stringutils.isblank (Yzm)) {

Res.seterrormessage ("Verification code cannot be empty");

return res;

}else if (!yzm.tolowercase (). Equals (Request.getsession (). getattribute ("Verifycodefind"))) {

Res.seterrormessage ("Captcha error");

return res;

}else{

Request.getsession (). SetAttribute ("Verifycodefind", "" ");

try {

This.checkedBaggageDamageService.saveOrUpdate (DTO, request);

Res.setresult (1);

} catch (Exception e) {

E.printstacktrace ();

Res.seterrormessage (E.getmessage ());

}

}

return res;

}

}


Spring MVC page Validation code sample

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.