案例4:验证码
- 1.本质:图片
- 2.目的:防止恶意表单注册
- 步骤:
- 1.创建一个web工程。
- 2.在src目录下创建cn.itcast.web.servlet包,在该包下创建一个CheckCodeServlet类,实现验证码在网页展示
package cn.itcast.web.servlet; import javax.imageio.ImageIO; import javax.servlet.ServletException; 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.IOException; import java.util.Random; @WebServlet("/checkCodeServlet") public class CheckCodeServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int width = 100; int height = 50; //1.