1.先导包,然后添加进制浏览器缓存代码
index.jsp
代码语言:javascript复制<%--
Created by IntelliJ IDEA.
User: 49841
Date: 2020/9/4
Time: 10:59
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>index</title>
</head>
<body>
<h2>欢迎登录</h2>
<h2>呱?!,当前登录用户:${user}</h2>
<img src="${pageContext.request.contextPath}/img">
</body>
</html>
ImageServlet
代码语言:javascript复制package servlet;
import cn.dsna.util.images.ValidateCode;
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.io.IOException;
/**
* zt
* 2020/9/5
* 17:53
*/
@WebServlet(name = "ImageServlet",value = "/img")
public class ImageServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Pragma", "no-cacahe");
ValidateCode validateCode = new ValidateCode(100, 30, 4, 30);
String code = validateCode.getCode();
request.getSession().setAttribute("code", code);
validateCode.write(response.getOutputStream());
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
运行效果图
此时按回车可以刷新验证码