使用ajax实现验证码

2021-02-12 02:16

阅读:461

标签:head   ssi   cep   extends   gets   let   ltm   else   password   

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 
 2 
 3 
 4     
"login.love" method="post"> 5 6 7 用户名:"nm" name="userName" type="text" value="${userName}" /> 8 密码:"userPass" type="password" /> 9 验证码:"vcode" name="vcode" type="text">"img1" alt="" src="image.do" onclick="flash(this)"> 10 11 12 13 14 15 16
17


评论


亲,登录后才可以留言!