09、手写验证码

login.html

复制代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
    function refeshCode(){
        var imgTag = document.getElementById("img");
        imgTag.src = "/servletContext/VerificationCode?" +new Date().getTime();//更新验证码
    }
</script>
</head>
<body>    
    <div>
        用户名: <input type='text'><br>
        密码: <input type='text'>
        <img src = '/servletContext/VerificationCode' id='img'>
        <a href='javascript:refeshCode()'>看不清?换一张</a>
    </div>
</body>
</html>
复制代码

 

 

VerificationCode.java

复制代码
package com.ivan.web;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Properties;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 验证码Servlet
 * @author ivan
 *
 */
@WebServlet("/VerificationCode")
public class VerificationCode extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
//        BufferedImage:内存图像
//        Graphics:画笔
//        ImageIO:输出图像
        
        //1.创建一个图片对象【画布】
        int width = 120;
        int height = 30;
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        
        //2.拿到画笔
        Graphics graphics = bi.getGraphics();
        
        //3.图个背景颜色
        graphics.setColor(Color.yellow);
        //Rectangle
        graphics.fillRect(0, 0, width, height);
        
        //4.画干扰线(4条)
        //重新设置画笔颜色
        graphics.setColor(Color.gray);
        Random random = new Random();
        for(int i=0;i<4;i++){
            
            //第一个点
            int x1 = random.nextInt(width);
            int y1 = random.nextInt(height);
            
            //第二个点
            int x2 = random.nextInt(width);;
            int y2 = random.nextInt(height);
            graphics.drawLine(x1, y1, x2, y2);
        }
        
        //5.画验证码(4个随机数)
        graphics.setColor(Color.black);
        int left = 5;
        for(int i=0;i < 4;i++){
            int r = random.nextInt(10);
            System.out.println("x=" + (left + i * 20));
            graphics.drawString(r + "", left + i * 20 ,20);
        }
        
        //设置响应的类型
        response.setContentType("image/jpeg");
        //把图片输出给客户端
        ImageIO.write(bi, "JPEG", response.getOutputStream());
        
    }

}
复制代码

 

posted @   expworld  阅读(91)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示