目录
回调函数的写法
正常的匿名函数:this指向的就是函数的调用者
箭头函数:this还是之前的this【推荐使用】
记忆点:只要是写了function,this就会变化;只要不写function,this就不会发生变化。推荐使用箭头函数。
cookie
可以保存http的状态
保存在客户端,保存在浏览器上,登录信息保存在浏览器上,以键值对的形式,不安全。
创建cookie(发送cookie)的步骤
-
创建Cookie对象
-
设置最大时效
-
把cookie放入到Http响应头中
//获取cookie
Cookie[] cookies = request.getCookies();
if(Objects.nonNull(cookies) && cookies.length > 0){
System.out.println("发现了cookie...");
//有cookie的情况
for(Cookie cookie : cookies){
System.out.println(cookie.getName() + "->" + cookie.getValue());
System.out.println(cookie.getMaxAge() + "<-");
if(cookie.getName().equals("username")){
response.sendRedirect("success.html");
}
}
}else{
System.out.println("没有发现cookie,创建cookie...");
//1.创建cookie
if(Objects.equals(username,"admin") && Objects.equals(password,"123456")){
Cookie cookie = new Cookie("name","admin");
//2.设置cookie的最大时效
cookie.setMaxAge(10);
//3.把cookie放入到响应中
response.addCookie(cookie);
response.sendRedirect("success.html");
}
}
session
//获取session会话
HttpSession session = request.getSession();
String username = request.getParameter("username");
//把username放入到session中
session.setAttribute("username",username);
response.sendRedirect("success.html");
session失效的几种场景
-
过期了
-
关闭浏览器(并不能完全的销毁session,JSESSIONID,根据JSESSIONID找到之前的session)
-
调用session销毁的方法【session.invalidate】
session的方法
//session销毁的方法
session.invalidate();
//设置session的过期时间
session.setMaxInactiveInterval(3000);
//根据key的值删除对应的属性
session.removeAttribute("");