使用ajax實現驗證碼


java后台的servlet:

 1 @WebServlet(value = "/login.love",name = "AjaxLoginServlet")
 2 public class AjaxLoginServlet extends HttpServlet {
 3 
 4     @Override
 5     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 6         super.doPost(req, resp);
 7     }
 8 
 9     @Override
10     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
11 
12         ResultMsg rm=new ResultMsg();
13         PrintWriter out=resp.getWriter();
14         Gson gson=new Gson();
15 
16         //1 獲取前台來的驗證碼
17         //2 獲取session里面的驗證碼
18         //3 校驗
19 
20         String vcode=req.getParameter("vcode");
21         String code= (String) req.getSession().getAttribute("code");
22 
23         if (null==vcode||null==code){
24             rm.setResult("0002");
25             rm.getMsg("驗證碼為空");
26 
27             out.println(gson.toJson(rm));
28             return;
29         }
30         if (vcode.equalsIgnoreCase(code)){
31             rm.setResult("0000");
32             out.println(gson.toJson(rm));
33         }else {
34             rm.setResult("0001");
35             rm.getMsg("驗證碼錯誤");
36             out.println(gson.toJson(rm));
37         }
38 
39 
40     }

前端的jsp:

 1 <!-- 提交的方式; get  post -->
 2 
 3 
 4     <form action="login.love" method="post">
 5 
 6         <!-- name : 對應我們servlet去獲取前台文本框的值的 key -->
 7         用戶名:<input id="nm" name="userName" type="text" value="${userName}" /> 
 8         密碼:<input name="userPass" type="password" />
 9         驗證碼:<input id="vcode" name="vcode" type="text"><img id="img1"  alt="" src="image.do" onclick="flash(this)">
10 
11         <!-- 默認是submit; -->
12         <button type="button" onclick="login()">登錄</button>
13 
14         <label id="lab1"></label>
15 
16     </form>
17 </body>
18 <script type="text/javascript">
19 
20     function login(){
21         var  xhr=XMLHttpRequest();
22         xhr.open("POST","login.love",true);
23         xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
24         xhr.send("vcode="+document.getElementById("vcode").value);
25         
26         xhr.onreadystatechange=function () {
27             
28             //請求成功
29             if (xhr.readyState==4&&xhr.status==200){
30                 var  obj=JSON.parse(xhr.responseText);
31                 
32                 if (obj.result=="0000"){
33                     alert("驗證碼正確");
34                 } else if(obj.result=="0002"){
35                     alert("驗證碼為空")
36                     flash(document.getElementById("img1"))
37                 }else {
38                     alert("驗證碼錯誤");
39                     flash(document.getElementById("img1"));
40                 }
41             } 
42         }
43     }
44     function flash(obj){
45         obj.src = "code.do?"+Math.random();
46         console.info(obj.src);
47     }
48 </script>

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM