zoukankan      html  css  js  c++  java
  • 简单输出随机验证码图片

    1. 创建一个servlet

     1 package ztq.servlet.study;
     2 
     3 import java.awt.Color;
     4 import java.awt.Font;
     5 import java.awt.Graphics;
     6 import java.awt.image.BufferedImage;
     7 import java.io.IOException;
     8 import java.util.Random;
     9 
    10 import javax.imageio.ImageIO;
    11 import javax.servlet.ServletException;
    12 import javax.servlet.http.HttpServlet;
    13 import javax.servlet.http.HttpServletRequest;
    14 import javax.servlet.http.HttpServletResponse;
    15 
    16 public class ServletDemo1 extends HttpServlet {
    17     public void doGet(HttpServletRequest request, HttpServletResponse response)
    18             throws ServletException, IOException {
    19         //通知浏览器不要缓存
    20         response.setHeader("Expires", "-1");
    21         response.setHeader("Cache-Control", "no-cache");
    22         response.setHeader("Pragma", "no-cache");
    23         
    24         int width = 120;
    25         int height = 25;
    26         //创建一副内存图像
    27         BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    28         
    29         //得到属于该图像的画笔
    30         Graphics g = bi.getGraphics();
    31         
    32         //画边框
    33         g.setColor(Color.BLACK);
    34         g.drawRect(0, 0, width, height);
    35         
    36         //填充背景色
    37         g.setColor(Color.GRAY);
    38         g.fillRect(1, 1, width - 2, height - 2);
    39         
    40         //画随机干扰线
    41         g.setColor(Color.GREEN);
    42         Random r = new Random();
    43         for(int i = 0; i < 10; i++){
    44             g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height));
    45         }
    46         
    47         //画随机数字
    48         g.setColor(Color.RED);
    49         g.setFont(new Font("宋体", Font.BOLD|Font.ITALIC, 20));
    50         int x = 23;
    51         for(int i = 0; i < 4; i++){
    52             g.drawString(r.nextInt(10) + "", x, 20);
    53             x += 20;
    54         }
    55         
    56         //输出到浏览器的页面上
    57         ImageIO.write(bi, "jpg", response.getOutputStream());
    58         
    59     }
    60     public void doPost(HttpServletRequest request, HttpServletResponse response)
    61             throws ServletException, IOException {
    62         doGet(request, response);
    63     }
    64 }

    2. 创建一个Html页面

     1 <!DOCTYPE html>
     2 <html>
     3   <head>
     4     <title>登录</title>
     5     
     6     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
     7     <meta http-equiv="description" content="this is my page">
     8     <meta http-equiv="content-type" content="text/html; charset=UTF-8">
     9     
    10     <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
    11 
    12   </head>
    13   
    14   <body>
    15       <form action = "" method = "post">
    16         用户名:<input type = "text" name = "username" /><br /><br />
    17         密码: <input type = "password" name = "password" /><br /><br />
    18         验证码:<input type = "text" name = "code" />
    19         <img src = "/JavaWeb_Servlet_Study/servlet/ServletDemo1">
    20         <a href = "javascript:toRefresh()">看不清</a><br /><br />
    21         <input type = "submit" value = "登录" />
    22     </form>
    23     <script type = "text/javascript">
    24         function toRefresh(){
    25             document.location.href = "login.html";
    26         }
    27     </script>
    28   </body>
    29 </html>
  • 相关阅读:
    【linux基础】linux系统日志设置相关记录
    【linux基础】mount: unknown filesystem type 'exfat'
    [c++]float assign
    第6章 移动语义和enable_if:6.1 完美转发
    第5章 技巧性基础:5.7 模板模板参数
    第5章 技巧性基础:5.6 变量模板
    第5章 技巧性基础:5.4 原生数组和字符串字面量的模板
    第5章 技巧性基础:5.3 this->的使用
    第5章 技巧性基础:5.2 零初始化
    第5章 技巧性基础:5.1 关键字typename
  • 原文地址:https://www.cnblogs.com/zhangtianq/p/5918152.html
Copyright © 2011-2022 走看看