Response_案例4_验证码_分析与Response_案例4_验证码_代码实现与Response_案例4_验证码_点击切换

Response_案例4_验证码_分析

          验证码分析

              1.本质:图片

              2.目的:防止恶意表单注册 

package qh.xueqiang.servlet;

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

@WebServlet("/checkCodeServlet")
public class CheckCodeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

            //1.创建一对象,在内存中图片(验证码对象)

            //2.美化图片

            //3.将图片输出到页面展示
    }
}

 

Response_案例4_验证码_代码实现

      

package qh.xueqiang.servlet;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

@WebServlet("/checkCodeServlet")
public class CheckCodeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                 int width = 130;
                 int heigth = 50;


            //1.创建一对象,在内存中图片(验证码对象)
        BufferedImage image = new BufferedImage(width,heigth,BufferedImage.TYPE_INT_RGB);
            //2.美化图片
            //2.1填充背景色
        Graphics graphics = image.getGraphics();//画笔对象
        graphics.setColor(Color.BLUE);//设置画笔颜色
        graphics.fillRect(0,0,width,heigth);

            //2.2画边框
        graphics.setColor(Color.cyan);
        graphics.drawRect(0,0,width-1,heigth-1);

        String str ="QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890";
        //生成随机角标
        Random ran = new Random();

        for (int i = 0;i<4;i++){
            int index = ran.nextInt(str.length());

        //获取字符
        char ch = str.charAt(index); //随机字符


        //2.3写验证码
        graphics.drawString(ch+"",width/5*i,25);
        }
        //2.4画干扰线
        graphics.setColor(Color.MAGENTA);
        //随机生成坐标点
        for (int i = 0; i <10; i++) {
            int x1 = ran.nextInt(width);
            int x2 = ran.nextInt(width);
            int y1 = ran.nextInt(heigth);
            int y2 = ran.nextInt(heigth);
            graphics.drawLine(x1,x2,y1,y2  );
        }
        //3.将图片输出到页面展示
        ImageIO.write(image,"jpg",response.getOutputStream());
    }
}

 

Response_案例4_验证码_点击切换

  

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="favicon.ico" rel="shortcut icon">
        <script>
            /*
                分析:
                    点击超链接或者图片,需要换一张
                    1.给超链接和图片绑定单机事件

                    2.重新设置图片的src属性值
             */
            window.onload = function (){
                //1.获取图片对象
                var img = document.getElementById("checkCode");
                //2.绑定单价事件
               img.onclick = function (){
                    //加时间戳
                        var date = new Date().getTime();
                   img.src = "/day15/checkCodeServlet?1"
               }
            }
        </script>



</head>
<body>
        <img id="checkCode" src="/day15/checkCodeServlet">

        <a id="change" href="">看不清,换一张</a>
</body>
</html>

 

posted @ 2023-02-02 15:39  zj勇敢飞,xx永相随  阅读(16)  评论(0编辑  收藏  举报