简单输出随机验证码图片

Posted 灌水乐园

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单输出随机验证码图片相关的知识,希望对你有一定的参考价值。

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>

 

以上是关于简单输出随机验证码图片的主要内容,如果未能解决你的问题,请参考以下文章

java生成简单验证码图片

随机验证码图片验证码和邮箱发送用户验证码

使用response输出随机验证码图片。

使用response输出随机验证码图片。

原创干货 | Java代码审计之图形验证码模块

servlet输出随机图片(验证码)