zoukankan      html  css  js  c++  java
  • 随机生成登录时的验证码图片

    生成随机验证码的代码:

     1 package servlet;
     2 
     3 import java.awt.Color;
     4 import java.awt.Font;
     5 import java.awt.Graphics;
     6 import java.awt.Graphics2D;
     7 import java.awt.image.BufferedImage;
     8 import java.io.IOException;
     9 import java.io.OutputStream;
    10 import java.io.PrintWriter;
    11 import java.util.Random;
    12 
    13 import javax.imageio.ImageIO;
    14 import javax.servlet.ServletException;
    15 import javax.servlet.http.HttpServlet;
    16 import javax.servlet.http.HttpServletRequest;
    17 import javax.servlet.http.HttpServletResponse;
    18 
    19 public class servletDemo2 extends HttpServlet {
    20 
    21     public static final int WIDTH=120;
    22     public static final int HEIGHT=50;
    23     
    24     public void doGet(HttpServletRequest request, HttpServletResponse response)
    25             throws ServletException, IOException {
    26      
    27         BufferedImage image=new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    28         Graphics g=image.getGraphics();
    29         //1、设置背景颜色
    30         setBackGround(g);
    31         //2、设置边框
    32         setBorder(g);
    33         //3、画干扰线
    34         drawRandomLine(g);
    35         //4、写随机数
    36         drawRandomNum((Graphics2D)g);
    37         //5、图形写给浏览器
    38         response.setContentType("image/jpeg");
    39         //发头控制浏览器不要缓存
    40         response.setDateHeader("expries", -1);
    41         response.setHeader("Cache-Control", "no-cache");
    42         response.setHeader("Pragma", "no-cache");
    43         //图形写给浏览器
    44         ImageIO.write(image, "jpg", response.getOutputStream());
    45         
    46         
    47         
    48     }
    49 
    50     private void setBackGround(Graphics g) {
    51         g.setColor(Color.WHITE);
    52         g.fillRect(0, 0, WIDTH, HEIGHT);
    53         
    54         
    55     }
    56     private void setBorder(Graphics g) {
    57         g.setColor(Color.BLUE);
    58         g.drawRect(1, 1, WIDTH-2, HEIGHT-2);
    59         
    60     }
    61     private void drawRandomLine(Graphics g) {
    62         g.setColor(Color.GREEN);
    63         for(int i=0;i<10;i++)
    64         {
    65             int x1=new Random().nextInt(WIDTH);
    66             int y1=new Random().nextInt(HEIGHT);
    67             int x2=new Random().nextInt(WIDTH);
    68             int y2=new Random().nextInt(HEIGHT);
    69             g.drawLine(x1, y1, x2, y2);
    70             
    71         }
    72 
    73     }
    74     private void drawRandomNum(Graphics2D g) {
    75         
    76         g.setColor(Color.RED);
    77         g.setFont(new Font("楷体",Font.BOLD,20));
    78         String base="u7684u4e00u662fu4e86u6211u4e0du4ebau5728u4ed6u6709u8fd9u4e2au4e0au4eecu6765";
    79         int x=5;
    80         for(int i=0;i<4;i++)
    81         {
    82             int degree=new Random().nextInt()%30;
    83             String ch=base.charAt(new Random().nextInt(base.length()))+"";
    84             g.rotate(degree*3.14/180, x, 25);
    85             g.drawString(ch, x, 25);
    86             g.rotate(-degree*3.14/180, x, 25);
    87             x=x+30;
    88         }
    89     }
    90 
    91 
    92     public void doPost(HttpServletRequest request, HttpServletResponse response)
    93             throws ServletException, IOException {
    94 
    95         doGet(request,response);
    96     }
    97 
    98 }

    在下面的html页面中可以调用此验证码图片:

     1 <!DOCTYPE html>
     2 <html>
     3   <head>
     4     <title>regist.html</title>
     5     <script type="text/javascript">
     6     function changeImage(img)
     7     {
     8       img.src=img.src+"?"+new Date().getTime();
     9     }
    10     </script>
    11 
    12   </head>
    13   
    14   <body>
    15     <form action="">
    16              用户名:<input type="text" name="username"></br>
    17              密码:    <input type="password" name="password"></br>
    18              验证码:<input type="text" name="checkcode"></br>
    19              <img src="/day_05/servlet/servletDemo2" onclick="changeImage(this)" alt="换一张" style="cursor:hand"></br>
    20              <input type="submit" value="注册">
    21                
    22         
    23     </form>
    24   </body>
    25 </html>
  • 相关阅读:
    PAT A1094 The Largest Generation (25 分)——树的bfs遍历
    PAT A1055 The World's Richest (25 分)——排序
    PAT A1052 Linked List Sorting (25 分)——链表,排序
    PAT A1076 Forwards on Weibo (30 分)——图的bfs
    辅导员
    辅导员面试
    C程序设计
    Excel VBA 基本概念
    Excel函数
    导入excel表的数据到数据库ssh
  • 原文地址:https://www.cnblogs.com/jjlovemm/p/4337948.html
Copyright © 2011-2022 走看看