JavaWeb之Response对象的基本操作和验证码

该对象和request都是由Tomcat创建

package study.web.response;

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 java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

@WebServlet("/response/demo1")
public class Demo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("demo1 ... get ...");
        /**
         * 重定向的两种方式:
         * 1、
         *  resp.setStatus(302);
         *  resp.setHeader("location","/WebServletDemo_war_exploded/response/demo2");
         *
         *  2、
         *  resp.sendRedirect("/WebServletDemo_war_exploded/response/demo2");
         */

        /**
         * 设置响应编码的两种方式:
         *
         * 1、
         * resp.setHeader("Content-Type","text/html;charset=utf-8");
         * 2、
         * resp.setContentType("text/html;charset=utf-8");
         */


        /**
         * 输出字符数据:
         * resp.getWriter().write("<h1>Hello Response! 你好,Response!</h1>");;
         */

        /**
         * 输出字节数据:
         * ServletOutputStream sos = resp.getOutputStream();
         * sos.write("你好".getBytes());
         */

        //验证码
        verification(resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }


    /**
     * 验证码
     * @param resp
     */
    private static void verification(HttpServletResponse resp)
    {
        //创建图像
        int width = 100;
        int height = 50;
        BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

        //美化图片
        Graphics g = image.getGraphics();//画笔对象
        g.setColor(Color.PINK);//设置画笔颜色
        g.fillRect(0,0,width,height);//填充

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

        //验证码随机数
        String str = "AAZWSXEDCRFVTGBYHNUJMIKLOPqazwsxedcrfvtgbyhnujmiklop1234567890";
        Random ran = new Random();

        String code = "";
        for (int i = 1; i < 5;i++) {
            //获取字符串
            int index = ran.nextInt(str.length());
            char ch = str.charAt(index);//随机字符串

            //写验证码
            g.drawString(ch + "",width / 5 * i,height / 2);
            code = code + ch;
        }
        //画干扰线
        g.setColor(Color.GREEN);

        for (int i = 0; i< 10; i++) {
            //生成随机坐标点
            int x1 = ran.nextInt(width);
            int x2 = ran.nextInt(width);

            int y1 = ran.nextInt(height);
            int y2 = ran.nextInt(height);

            g.drawLine(x1,y1,x2,y2);
        }


        //输出图像
        try {
            System.out.println("验证码是:"+ code);
            ImageIO.write(image,"jpg",resp.getOutputStream());//ImageIO 输出文件对象
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

验证码:
屏幕截图 2021-01-19 155739.png

仅有一条评论

添加新评论