servlet响应一个随机四位验证码图片给客户端

众所周知,在html中对于图片资源的加载,是通过访问其src的值进行一次请求。

我们可以在Java中使用BufferedImage类创建一张图片,通过Graphics2D类进行图片的绘制,从而动态制作验证码图片,再使用工具类ImagIO把图片写入到 resp.getOutputStream() 响应流中,即实现动态生成的图片验证码。

 

 


jsp页码代码:

  图片资源来自别名位validcode的servlet类

<form method="post" action="login">
    账户:<input type="text" name="username" id="uname"><br>
    密码:<input type="password" name="password" id="pwd"><br>
    验证码:<input type="text" size="1" name="code" id="validCode"><img src="validcode" width="80" height="40"><a href="">看不清啊</a>
    <input type="submit" value="登录"><input type="button" value="重置">
    <br>
    ${error}
</form>

生成验证码的servlet代码:

  validcode服务类获取到请求,调用实现方法

   @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        getValidCode(req, resp);
    }

具体实现随机验证码方法:

   生成验证码,把四位数字存入session中,把图片写入响应流中

    private void getValidCode(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        //创建一张图片
        BufferedImage image = new BufferedImage(200,100,BufferedImage.TYPE_INT_BGR);

        //获取一支画笔
        Graphics2D gra = image.createGraphics();
        gra.setColor(Color.WHITE);
        //画一个矩形:使用白色填充照片
        gra.fillRect(0,0,200,100);

        //生成四个随机数,放到集合中
        List<Integer> randList = new ArrayList<>();
        Random random = new Random();
        for(int i = 0;i<4;i++){
            randList.add(random.nextInt(10));
        }

        //设置字体
        gra.setFont(new Font("宋体",Font.ITALIC|Font.BOLD,40));
        //随机颜色
        Color[] colors = new Color[]{Color.RED,Color.YELLOW,Color.BLUE,Color.GREEN};
        for (int i =0;i<4;i++){
            gra.setColor(colors[random.nextInt(colors.length)]);
            //画字符,参数:String,x,y
            gra.drawString(randList.get(i)+"",i*40,70+(random.nextInt(21)-10));
        }
        //画第一根直线,起点为:左边【0,(60-90)】,终点为:右边【200,(0-100)】
        gra.setColor(colors[random.nextInt(colors.length)]);
        gra.drawLine(0,70+(random.nextInt(21)-10),200,random.nextInt(101));
        //画第二根直线
        gra.setColor(colors[random.nextInt(colors.length)]);
        gra.drawLine(0,random.nextInt(101),200,70+(random.nextInt(21)-10));

        //获取输出流
        ServletOutputStream outputStream  = resp.getOutputStream();
        //使用工具类把图片写到流中
        ImageIO.write(image,"jpg",outputStream);


        //随机数写入session中
        HttpSession session = req.getSession();
        String code = ""+randList.get(0)+randList.get(1)+randList.get(2)+randList.get(3);
        session.setAttribute("validCode",code);
        System.out.println("验证码:"+code);
    }

程序入口servlet:

  web应用入口

   @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        //获取请求数据
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String code = req.getParameter("code");

        //第一次发起请求
        if(code==null){
            req.getRequestDispatcher("index.jsp").forward(req,resp);
            return;
        }

        //比较验证码
        if(code.equals(req.getSession().getAttribute("validCode"))){
            //进行业务操作
            User user = service.selectByUnamePwd(username, password);

            System.out.println(user);
            //登录成功
            if(user!=null){
                resp.sendRedirect("main.jsp");
            }else {
                req.setAttribute("error","账户密码错误");
                req.getRequestDispatcher("index.jsp").forward(req,resp);
                System.out.println("账户密码错误");
            }
        }else {
            req.setAttribute("error","验证码错误");
            req.getRequestDispatcher("index.jsp").forward(req,resp);
            System.out.println("验证码错误");
        }
    }

 


程序地址:

lurenjia/服务器动态生成验证码响应给浏览器 (gitee.com)

 

posted @ 2023-02-27 18:41  在博客做笔记的路人甲  阅读(70)  评论(0编辑  收藏  举报