javaWeb生成验证码

这是我的第一篇java学习,主要是为了记录我学习Java的心路历程,为了就业做准备。

javaWeb应用产生验证码主要分为三个板块,即:1,产生验证码字符串,字符串写入到输出流中。2,在code.jsp中进行中间处理,主要是让浏览器清空缓存。 3,在index.jsp页面生成验证码

1,产生验证码字符串都工具类

package com.imooc.skr;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.Random;

public class CaptchaCode {
    //这是生成单个随机字符的方法
    public static char randomChar(){
        String str = "asdfghjkl123456";
        Random random = new Random();
        return str.charAt(random.nextInt(str.length()));
    }

    //这是生成验证码的方法
    public static String drawImage(HttpServletResponse response){
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            stringBuilder.append(randomChar());
        }
        String code = stringBuilder.toString();
        int width = 120;
        int height = 25;

        BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR);
        //上面是一个缓冲流
        Graphics2D graphics2D = bi.createGraphics();
        graphics2D.setBackground(new Color(226,226,200));
        graphics2D.setColor(new Color(255,255,255));
        //Font类的构造函数由Font(字体名称,style,size)
        Font font = new Font("微软雅黑",Font.PLAIN,20);
        graphics2D.setFont(font);
        graphics2D.clearRect(0,0,width,height);//这条是使用当前会图表的背景色来填充矩形区域
        //好像用graphics2D.fillRect()也可以
        FontRenderContext context = graphics2D.getFontRenderContext();
        Rectangle2D bounds = font.getStringBounds(code,context);
        double x = (width-bounds.getWidth())/2;
        double y = (height-bounds.getHeight())/2;

        double ascent = bounds.getY();//这条语句是我最懵逼的呢,我没搞懂他是什么意思

        double baseY = y-ascent;

        graphics2D.drawString(code,(int)x,(int)baseY);
        //结束绘制
        graphics2D.dispose();

        try{
            ImageIO.write(bi,"jpg",response.getOutputStream());
        }catch (Exception e){
            e.printStackTrace();
        }
        return code;
    }
}

2.在code.jsp中通过工具类生成验证码

<%@ page import="com.imooc.skr.CaptchaCode" %><%--
  Created by IntelliJ IDEA.
  User: finajoy
  Date: 2019/12/30
  Time: 11:09
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    /**
     * 以下三条代码都是用来清空浏览器缓存的,我也不知道是什么意思
     */
    response.setHeader("pragma","no-cache");
    response.setHeader("cache-control","no-cache");
    response.setHeader("expires","no-cache");

    String code = CaptchaCode.drawImage(response);
    session.setAttribute("code",code);

    //下面两条代码用来解决getOutputStream异常,如果没有这两行代码,验证码无法显示
    //然而我也不知道是什么意思
    out.clear();
    out = pageContext.pushBody();
%>

3,在index.jsp中显示验证码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>

  <img src="code.jsp" >
  </body>
</html>

 

发布了13 篇原创文章 · 获赞 147 · 访问量 9万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章