验证码的生成

生成验证码

这里我们使用一个jar包。以前我也想着直接自己整个原生练手,但是,宝贝,社会发展最重要的是什么,懒,企业中不会让你浪费时间写一个本可以很快解决的问题的

1.在项目中导入ValidateCode.jar

@WebServlet(name = "ValidateServlet", value = "/ValidateServlet")
public class ValidateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //生成验证码
        ValidateCode vc=new ValidateCode(200, 30, 4, 10);
        //获取验证码对应的文字
        String code=vc.getCode();
        System.out.println(code);
        
        // 将图片保存到本地
        //FileOutputStream out = new FileOutputStream("D:/code_img.jpg");
        //vc.write(out);

        //将验证码响应给用户,用户访问即刻看到验证码
        vc.write(response.getOutputStream());
    }
}

访问链接即可得到二维码 http://localhost:8080/01__war_exploded/ValidateServlet样式的图片

在这里插入图片描述

posted @ 2022-04-02 09:46  coderwcb  阅读(2)  评论(0编辑  收藏  举报