Servlet实现简单的验证码

HTML代码

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="/day16/ResualtServlet" method="post">
        验证码:<input type="text" name="code"><br>
        <img alt="图片无法显示" id="imgId" src="/day16/VerifyLoginServlet" onclick="changeImg()">
        <a href="javascript:void(0)" onclick="changeImg()">看不清,换一张</a><br>
        <input type="submit" value="提交">
    </form>
</body>
</html>
<script>
    function changeImg(){
        document.getElementById("imgId").src="/day16/VerifyLoginServlet?"+new Date();
    }
</script>

Java代码

生成验证码

public class VerifyLoginServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //生成验证码字符串和图片
        BufferedImage img = VerifyCodeUtils.paintImage(80, 30);
        String code = VerifyCodeUtils.getCode();

        //把验证码存放到session对象中
        HttpSession session = request.getSession();
        session.removeAttribute("code");
        session.setAttribute("code", code.toLowerCase());
        System.out.println(session.getAttribute("code"));

        //生成验证码
        ImageIO.write(img, "jpeg", response.getOutputStream());
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

验证输入验证码是否正确

public class ResualtServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String parameter = request.getParameter("code").toLowerCase();
        String code = (String) request.getSession().getAttribute("code");

        if (parameter.equals(code)) {
            response.getWriter().println("验证成功");
        } else {
            response.getWriter().println("验证失败");
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

验证码生成工具

public class VerifyCodeUtils{
    private static StringBuffer codesave = null;

    public static BufferedImage paintImage(int width, int height) {
        //核心对象,第一个参数是图片的宽度,第二个参数是高度,最后一个参数是颜色的模式
        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        //获得画笔
        Graphics graphics = img.getGraphics();
        //给画笔设置为黑色
        graphics.setColor(Color.BLACK);
        //绘制矩形
        graphics.fillRect(0, 0, width, height);
        //给画笔设置为白色
        graphics.setColor(Color.WHITE);
        graphics.fillRect(1, 1, width-2, height-2);

        //定义字符数据
        String code = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
        Random rd = new Random();
        codesave = new StringBuffer();
        //设置画笔的字体样式为楷体、加粗,行高
        graphics.setFont(new Font("楷体", Font.BOLD, height-4));
        for (int i = 0; i < 4; i++) {
            //设置颜色
            graphics.setColor(new Color(rd.nextInt(256), rd.nextInt(256), rd.nextInt(256)));
            //取出随机索引
            int index = rd.nextInt(code.length());
            codesave.append(code.substring(index, index+1));
            //画字符串
            graphics.drawString(code.substring(index, index+1), width/6*(i+1), height-4);
        }

        return img;
    }

    public static String getCode() {
        if (codesave==null) {
            return "";
        }
        return codesave.toString();
    }
}
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值