输出随机数字验证码图片

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


// 输出随机验证码图片
public class RandomPic extends HttpServlet {


        public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
          
                    // 刷新验证码,设置浏览器不需要缓存
                    response.setHeader("Expires", "-1");
                    response.setHeader("Cache-Control", "no-cache");
                    response.setHeader("Pragma", "no-cache");
                    
                    // 设置图片的宽高
                    int width = 120;
                    int height = 25;
                    // 创建一副内存图像:BufferedImage 最后一个参数为: 红绿蓝色素方式
                    BufferedImage image = new BufferedImage(width, height,
                        BufferedImage.TYPE_INT_RGB);
                    // 得到属于该图片的画笔: Graphics
                    Graphics g = image.getGraphics();


                    // 画边框
                    g.setColor(Color.RED);
                    g.drawRect(0, 0, width, height);


                    // 填充背景色
                    g.setColor(Color.YELLOW);
                    g.fillRect(1, 1, width - 2, height - 2);


                    // 画10条干扰线
                    g.setColor(Color.GRAY);
                    Random r = new Random();
                    for (int i = 0; i < 10; i++) {
                            g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), 
                                r.nextInt(height));
                    }


                    // 随机数字
                    g.setColor(Color.RED);
                    g.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, 20));
                    int x = 23; // 初始数字的横坐标
                    for (int i = 0; i < 4; i++) {
                            g.drawString(r.nextInt(10) + "", x, 20);
                            x += 20;
                    }


                    // 输出到浏览器的页面上: ImageIO 参数依次为: 内存图像,图像格式,输出方式
                    ImageIO.write(image, "jpg", response.getOutputStream());
        }


        public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
                    doGet(request, response);
        }

}



//  创建一个login.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
        <head>
                <title>用户登录</title>
                <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
                <meta http-equiv="description" content="this is my page">
                <meta http-equiv="content-type" content="text/html; charset=UTF-8">
                <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->


              <script type="text/javascript">
                function toRefresh(){
                  document.location.href="Login.html"
                }
              </script>


        </head>
        <body>
                <form action="/day06/servlet/ResponseDemo4" method="post">
                  用户名: <input type="text" name="username"/> <br/><br/>
                  密码   : <input type="password" name="password"/> <br/><br/>
                  验证码: <input type="text" name="code" size="3"/>
                  <img src="/day06/servlet/RandomPic"/>
                  <a href="javascript:toRefresh()">刷新</a><br/><br/>
                  <input type="submit" value="登录"/>
                </form>         
        </body>
</html>












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值