@WebServlet("/getCode")
public class GetCodeServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// resp.setContentType(“image/jpeg”);
// 获取一个图片验证码
CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 20);
// 获取图片中的验证码内容
String code = captcha.getCode();
// 把验证码存在session中
req.getSession().setAttribute(“code”,code);
// 将图片验证码发送给前端
captcha.write(resp.getOutputStream());
}
}