首页 > 其他 > 详细

Response实现验证码功能

时间:2015-10-14 21:31:10      阅读:280      评论:0      收藏:0      [点我收藏+]

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 = "\u7684\u4e00\u4e86\u662f\u6211\u4e0d\u5728\u4eba\u4eec\u6709\u6765\u4ed6\u8fd9\u4e0a\u7740\u4e2a\u5730\u5230\u5927\u91cc\u8bf4\u5c31\u53bb\u5b50\u5f97\u4e5f\u548c\u90a3\u8981\u4e0b\u770b\u5929\u65f6\u8fc7\u51fa\u5c0f\u4e48\u8d77\u4f60\u90fd\u628a\u597d\u8fd8\u591a\u6ca1\u4e3a";
        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>

 

Response实现验证码功能

原文:http://www.cnblogs.com/lyjs/p/4878709.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!