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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章