Cookie笔记
1 Cookie的创建和发送
//1、创建cookie对象 Cookie cookie = new Cookie("name","zhangsan"); //2、将cookie中存储的信息发送到客户端---头 response.addCookie(cookie);
2 Cookie的持久化
//1.1 为cookie设置持久化时间 ---- cookie信息在硬盘上保存的时间 cookie.setMaxAge(10*60);//10分钟 ---- 时间设置为0代表删除该cookie
3 什么时候携带Cookie?
//1.2 为cookie设置携带的路径 //cookie.setPath("/WEB16/sendCookie");//访问sendCookie资源时才携带这个cookie cookie.setPath("/WEB16");//访问WEB16下的任何资源时都携带这个cookie //cookie.setPath("/");//访问服务器下的所有的资源都携带这个cookie
4 Cookie的删除
使用同名同路径的cookie,设置时间为0,进行覆盖即可。
//删除客户端保存 name=zhangsan的cookie信息 Cookie cookie = new Cookie("name",""); //将path设置成与要删除cookie的path一致 cookie.setPath("/WEB16"); //设置时间是0 cookie.setMaxAge(0); response.addCookie(cookie);
5 服务端如何获取客户端携带的 Cookie
package cookie; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class GetCookieServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获得客户端携带的cookie的数据 Cookie[] cookies = request.getCookies(); //Cookie cookie = new Cookie("name","zhangsan"); //通过cookie名称获得想要的cookie if(cookies!=null){ for(Cookie cookie : cookies){ //获得cookie的名称 String cookieName = cookie.getName(); if(cookieName.equals("name")){//name设置时候的名字 name //获得该cookie的值 String cookieValue = cookie.getValue(); System.out.println(cookieValue); } } } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
6 案例-显示用户的上次访问时间
1 案例分析
2 代码实现
package lastaccesstime; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LastAccessTimeServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获得当前时间 Date date = new Date(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String currentTime = format.format(date); //1、创建Cookie 记录当前的最新的访问时间 Cookie cookie = new Cookie("lastAccessTime",currentTime); cookie.setMaxAge(60*10*500); response.addCookie(cookie); //2、获得客户端携带cookie ---- lastAccessTime String lastAccessTime = null; Cookie[] cookies = request.getCookies(); if(cookies!=null){ for(Cookie coo : cookies){ if("lastAccessTime".equals(coo.getName())){ lastAccessTime = coo.getValue(); } } } response.setContentType("text/html;charset=UTF-8"); if(lastAccessTime==null){ response.getWriter().write("您是第一次访问"); }else{ response.getWriter().write("您上次的访问的时间是:"+lastAccessTime); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }