栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

HTML 验证码 前后台全过程

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

HTML 验证码 前后台全过程

目录
  • 演示
    • 定义图片对象
    • 设置画笔
    • 画边框
    • 写文字
    • 验证码随机
    • 随机干扰线
    • 让图片在指定的位置显示
    • 点击换图
    • 让文字变大
  • 实战

演示

演示代码字节流

定义图片对象

设置画笔

x y 设置位置

画边框

注意 右 和 下 没有边框 ,原因是矩形打了,变小一点就可以了

写文字

验证码随机

随机干扰线

让图片在指定的位置显示

点击换图

让文字变大

实战

这个是验证码的servlet

package servlet;

import javax.imageio.ImageIO;
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;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

@WebServlet("/verifyCode")
public class VerifyCode extends HttpServlet {
    private Random random = new Random();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取字节输出流的对象
        ServletOutputStream outputStream = resp.getOutputStream();

        //获得一个图片对象
        int width = 100;
        int height = 50;
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);


        //图片背景
        Graphics graphics = bufferedImage.getGraphics();
        graphics.setColor(Color.BLUE);
        graphics.fillRect(0, 0, width, height);

        // 验证码内边框
        graphics.setColor(Color.RED);
        graphics.drawRect(0, 0, width - 1, height - 1);

        // 获取验证码内容
        String code = getCode();
        // 存到 session
        HttpSession session = req.getSession();
        session.setAttribute("code",code);

        // 文字变大了,y 坐标就要变
        int y = 30;
        graphics.setColor(Color.YELLOW);
        // 让文字变大
        graphics.setFont(new Font("simhei", Font.PLAIN, 24));
        for (int i = 0; i < code.length(); i++) {
            String c = code.charAt(i) + "";
            int n = i + 1;
            int x = n * 20;
            graphics.drawString(c, x, y);

        }

        // 干扰线
        graphics.setColor(Color.RED);
        for (int i = 0; i < 20; i++) {
            //生成坐标
            // 横坐标
            int x1 = random.nextInt(width);
            int x2 = random.nextInt(width);
            // 纵坐标
            int y1 = random.nextInt(height);
            int y2 = random.nextInt(height);
            //画线
            graphics.drawLine(x1, y1, x2, y2);
        }

        // 返回给游览器
        ImageIO.write(bufferedImage, "jpg", outputStream);
    }

    private String getCode() {
        // 内容池
        String content = "abcdABCDEFG123456789";


        String code = "";
        for (int i = 0; i < 4; i++) {
            int idx = random.nextInt(content.length());
            char c = content.charAt(idx);
            code += c;
        }

        return code;
    }
}

下面是页面

<%--
  Created by IntelliJ IDEA.
  User: SSOA
  Date: 2022/6/9
  Time: 13:48
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    
    
    
    
    

    
    

    

    

    
    
    
    
    
    
    

    

    
    

    尤洪







HTML 验证码 前后台全过程
  注册 已有商城账号,我要登录
* 用户名  
* 密码  
* 确认密码  
* 邮箱  
* 手机  
昵称  
性别   男 女
* 验证码   验证码 换一张
 
 
备案/许可证编号:蜀ICP备12009302号-1-www.dingguagua.com Copyright © 2015-2018 尤洪商城网 All Rights Reserved. 复制必究 , Technical Support: Dgg Group

HTML 验证码 前后台全过程
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/967423.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号