zoukankan      html  css  js  c++  java
  • Response实现验证码功能

    Servlet代码:

        public static final int WIDTH=120;
        public static final int HEIGHT=35;
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
    
            BufferedImage image=new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
            Graphics  g=image.getGraphics();
            
            //1 设置背景颜色
            setBackGround(g);
            
            //2 设置边框
            setBorder(g);
            
            //3 画出干扰线
            drawRandomLine(g);
            
            //4 写随机数
            drawRandomNum((Graphics2D)g);
            
            //5 图形写给浏览器
            response.setContentType("image/jpeg");
            response.setHeader("Expires", "-1");  
            response.setHeader("Cache-Control", "no-cache");  
            response.setHeader("Pragma", "no-cache");
            ImageIO.write(image, "jpg", response.getOutputStream());
        }
    
    
        private void drawRandomNum(Graphics2D g) {
            g.setColor(Color.RED);
            g.setFont(new Font("宋体",Font.BOLD,20));
            String base = "u7684u4e00u4e86u662fu6211u4e0du5728u4ebau4eecu6709u6765u4ed6u8fd9u4e0au7740u4e2au5730u5230u5927u91ccu8bf4u5c31u53bbu5b50u5f97u4e5fu548cu90a3u8981u4e0bu770bu5929u65f6u8fc7u51fau5c0fu4e48u8d77u4f60u90fdu628au597du8fd8u591au6ca1u4e3a";
            int x=5;    
            for(int i=0;i<4;i++){
                int degree=new Random().nextInt()%30;
                String ch=base.charAt(new Random().nextInt(base.length()))+"";
                g.rotate(degree*Math.PI/180, x, 20);
                g.drawString(ch, x, 20);
                g.rotate(-degree*Math.PI/180, x, 20);
                x+=30;
            }
        }
    
    
        private void drawRandomLine(Graphics g) {
            g.setColor(Color.GREEN);
            for(int i=0;i<5;i++){
                int x1=new Random().nextInt(WIDTH);
                int y1=new Random().nextInt(HEIGHT);
        
                int x2=new Random().nextInt(WIDTH);
                int y2=new Random().nextInt(HEIGHT);
                g.drawLine(x1, y1, x2, y2);
            }
            
        }
    
    
        private void setBorder(Graphics g) {
            g.setColor(Color.BLUE);
            g.drawRect(1, 1, WIDTH-2, HEIGHT-2);
        }
    
    
        private void setBackGround(Graphics g) {
            g.setColor(Color.WHITE);
            g.fillRect(0, 0, WIDTH, HEIGHT);
        }

    jsp代码:

    <html>
      <head>
        <script>
            function changeImage(img){
                img.src=img.src+"?"+new Date().getTime(); //改变地址
            }
        </script>
      </head>
      
      <body>
        <form>
            用户名:<input type="text" name="username"> <br/>
            密码:<input type="text" name="password">  <br/>
            认证码:<input type="text" name="checkcode">
            <img src="/day07/servlet/ResponseDemo4" onclick="changeImage(this)"  alt="换一张" style="cursor:hand">  <br/>
            <input type="submit" value="注册">
        </form>
      </body>
    </html>

    onclick调用的方法能不能改成下边的这行:

    onclick="this.src=this.src"

    实践是错误的。第一次你请求的地址是

    http://localhost:8080/day07/servlet/ResponseDemo4

    因为你再次请求得地址和上边是一样的,浏览器里边有缓存,所有页面并没有刷新,没有得到我们想要的效果。

    因此我们使用下边的方法

     img.src=img.src+"?"+new Date().getTime(); //改变地址

    我们的传送方式是 get()方法,因此我们后边可以传送一个没有用的数据,只是为了改变这个请求的网址。就会得到下边的地址:

    http://localhost:8080/day07/servlet/ResponseDemo4?1444884854019

    所以这次就会得到我们想要的效果。

  • 相关阅读:
    关联A850刷机包 高级电源 时间中心 优化 ROOT 动力 美化 简化
    CodeForces 425E Sereja and Sets
    int有符号和无符号类型内存 -- C
    软件体系结构————防御性编程
    Hibernate各保存方法之间的差 (save,persist,update,saveOrUpdte,merge,flush,lock)等一下
    椭圆识别
    UVa 10223
    照片详细解释YUV420数据格式
    LeetCode:Reverse Integer
    看了此文你还不懂傅里叶变换,那就来掐我吧
  • 原文地址:https://www.cnblogs.com/lyjs/p/4878709.html
Copyright © 2011-2022 走看看