zoukankan      html  css  js  c++  java
  • Response验证码案例 January 27,2020

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>验证码点击切换</title>
        <script>
            /*
            分析:
                点击超链接或者图片,需要换一张
                1.给超链接和图片绑定单击事件
                2.重新设置图片的src属性值
         */
            window.onload=function () {//加载完毕后
                var img = document.getElementById("checkCode");
                img.onclick = function () {//绑定单击事件
                    //加时间戳 欺骗服务器
                    var date = new Date().getTime();
                    img.src="/order7_Request_Response_war_exploded/checkCodeServlet?"+date;
                }
    
                var a = document.getElementById("change");
                a.onclick = function () {
                    //加时间戳 欺骗服务器
                    var date = new Date().getTime();
                    img.src="/order7_Request_Response_war_exploded/checkCodeServlet?"+date;
                }
            }
    
        </script>
    </head>
    <body>
    
        <img id="checkCode" src="/order7_Request_Response_war_exploded/checkCodeServlet">
        <a id="change" href="">看不清?换一张</a>
    </body>
    </html>
    package web.response;
    
    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 {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //1.创建一个对象,在内存中 存放图片
            int width = 100;
            int heigth = 50;
            BufferedImage image = new BufferedImage(width,heigth,BufferedImage.TYPE_INT_RGB);
            //2.美化图片
                //2.1填充背景色
            Graphics graphics = image.getGraphics();//画笔对象
            graphics.setColor(Color.PINK);//设置画笔颜色
            graphics.fillRect(0,0,width,heigth);//填充矩形
                //2.2画边框
            graphics.setColor(Color.BLUE);
            graphics.drawRect(0,0,width-1,heigth-1);//因为画笔有1px  所以要减一
                //2.3填写验证码
            String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
            Random random = new Random();
            for (int i = 1; i <=4 ; i++) {
                int index = random.nextInt(str.length());
                graphics.drawString(str.charAt(index)+"",width/5*i,heigth/2);
            }
                //2.4画干扰线
            graphics.setColor(Color.green);
            for (int i = 0; i <10 ; i++) {
                int x1 = random.nextInt(width);
                int x2 = random.nextInt(width);
                int y1 = random.nextInt(heigth);
                int y2 = random.nextInt(heigth);
                graphics.drawLine(x1,y1,x2,y2);
            }
            //3.将图片输出至页面展示
            ImageIO.write(image,"jpg",response.getOutputStream());
    
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request,response);
        }
    }
  • 相关阅读:
    javaSE基础(六)--IO流
    javaSE基础(五)--JDBC
    javaSE基础(四)--Map集合
    javaSE基础(三)--List集合
    javaSE基础(二)
    javaSE基础(一)
    eclipse快捷键大全
    mybatis学习-基础
    工厂模式
    GC日志和jvm内存的分代
  • 原文地址:https://www.cnblogs.com/yyanghang/p/12236027.html
Copyright © 2011-2022 走看看