目录
1. 案例需求:
访问一个Servlet,如果是第一次访问,则提示:您好,欢迎您首次访问。
如果不是第一次访问,则提示:欢迎回来,您上次访问时间为:显示时间字符串。
2. 案例代码:
@WebServlet("/Servlet01")
public class Servlet01 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
//1.判断用户是否第一次访问
//看cookie中有没有lastTime
Cookie lastTime = CookieUtils.getCookie(request, "lastTime");
if(lastTime == null){
//没有,说明第一次访问
//记录这次访问的时间
Date date = new Date();