zoukankan      html  css  js  c++  java
  • 简单的验证码点击切换

    html:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>验证码</title>
        <script>
            var code
            window.onload=function () {
                code=document.getElementById("checkCode");
                code.onclick=change();
            }
            function change() {
                var time=new Date().getTime();
                code.src="/day07/ServletCheckCode?"+time;
            }
    
        </script>
    </head>
    <body>
        <img id="checkCode" src="/day07/ServletCheckCode">
        <a onclick="change() " href="javascript:void(0)">看不清楚?点击换一张</a>
    </body>
    </html>

    servlet

    @WebServlet("/ServletCheckCode")
    public class ServletCheckCode extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //1创建对象
            int width=100;
            int height=50;
            BufferedImage img=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
            //2.美化图片
            //填充背景色
            Graphics g=img.getGraphics();
            g.setColor(Color.pink);
            g.fillRect(0,0,width,height);
    
            //画边框
            g.setColor(Color.blue);
            g.drawRect(0,0,width-1,height-1);
    
            //写验证码
            String str="QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm0123456789";
            Random ran=new Random();
            for (int i = 1; i <= 4; i++) {
                int index = ran.nextInt(str.length());
                char c = str.charAt(index);
                g.drawString(c+"",width/5*i,height/2);
            }
            //画干扰线
            g.setColor(Color.green);
            int x1;
            int y1;
            int x2;
            int y2;
            for (int i = 0; i <10; i++) {
                x1=ran.nextInt(width);
                x2=ran.nextInt(width);
                y1=ran.nextInt(height);
                y2=ran.nextInt(height);
                g.drawLine(x1,y1,x2,y2);
            }
            //3.展示图片
            ImageIO.write(img,"jpg",response.getOutputStream());
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request,response);
        }
    }
  • 相关阅读:
    如果你也时常想要上进,我们可以相互鼓励,相互促进
    (转)Math.round(11.5)等于多少?Math.round(-11.5)等于多少?
    乐观锁和悲观锁(Version:0.1)
    redis数据丢失及解决【转】
    Spring的IOC原理[通俗解释一下]
    Java中Error与Exception的区别
    WebService
    JDBC详解
    Cookie与Session
    java的pojo规范
  • 原文地址:https://www.cnblogs.com/findlisa/p/10873291.html
Copyright © 2011-2022 走看看