码迷,mamicode.com
首页 > 其他好文 > 详细

简单输出随机验证码图片

时间:2016-09-29 01:22:46      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

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>

 

简单输出随机验证码图片

标签:

原文地址:http://www.cnblogs.com/zhangtianq/p/5918152.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!