验证码

验证码的验证需要三个类
public class ImgFontByte {
    public Font getFont(int fontHeight){
        try {
            Font baseFont = Font.createFont(Font.TRUETYPE_FONT, new ByteArrayInputStream(hex2byte(getFontByteStr())));
            return baseFont.deriveFont(Font.PLAIN, fontHeight);
        } catch (Exception e) {
            return new Font("Arial",Font.PLAIN, fontHeight);
        }
    }
    
    private  byte[] hex2byte(String str) {
        if (str == null)
            return null;
        str = str.trim();
        int len = str.length();
        if (len == 0 || len % 2 == 1)
            return null;

        byte[] b = new byte[len / 2];
        try {
            for (int i = 0; i < str.length(); i += 2) {
                b[i / 2] = (byte) Integer
                        .decode("0x" + str.substring(i, i + 2)).intValue();
            }
            return b;
        } catch (Exception e) {
            return null;
        }
    }

 

public class ValidateCode {
    // 宽
    private int width = 160;
    // 高
    private int height = 40;
    // 字符数
    private int codeCount = 5;
    // 干扰线条数
    private int lineCount = 150;
    // 验证码
    private String code = null;
    // 图片
    private BufferedImage buffImg=null;

    private char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
            'K', 'L', 'M', 'N',  'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
            'X', 'Y', 'Z',  '1', '2', '3', '4', '5', '6', '7', '8', '9' };

 

@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public UserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        Object vCode = session.getAttribute("code");
        String code = request.getParameter("vCode");
        //验证码是否正确s
        if(code.equalsIgnoreCase((String)vCode)) {
            //移除
            session.removeAttribute("code");
            String name = request.getParameter("name");
            String age = request.getParameter("age");
            //处理业务
            System.out.println(name+":"+age);
            request.setAttribute("msg", "处理成功");
            request.getRequestDispatcher("/info.jsp").forward(request, response);
        }else {
            request.setAttribute("msg", "验证码不正确");
            request.getRequestDispatcher("/info.jsp").forward(request, response);
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值