hooyantsing's Blog

验证码

字数统计: 158阅读时长: 1 min
2019/11/20

验证码

前端:

1
<img src="vcode.jsp"  style="height:50px;margin-left: 30px"  onclick="src='vcode.jsp?'+ new Date()">

后端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.yc.demo.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/login.s")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        // 获取验证码
        String sVcode = (String) request.getSession().getAttribute("vcode");
        String rVcode = request.getParameter("vcode");
        
        if(sVcode.equalsIgnoreCase(rVcode)){
            //验证成功
        } else {
            //验证失败
        }
        
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}

vcode

vcode.zip

CATALOG