创建新模块13_cookie_session并且add web,然后创建Tomcat实例13_cookie_session
1、Cookie 饼干
a) 什么是Cookie?
1.Cookie翻译过来是饼干的意思。
2、Cooke是服务器通知客户端保存键值对的一种技术
3、客户端有了Cookie后,每次请求都发送给服务器。
4、每个Cookie的大小不能超过4kb
准备
web下cookie.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.O1 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="pragma" content="no-cache" /> <meta http-equiv="cache-control" kontent="no-cache" /> <meta http-equiv="Expires" content="0" /> <meta http-equiv="content-Type" content="text/html; charset=UTF-8"> <title>Cookie</title> <style type="text/css"> ul li{ list-style:none; } </style> </head> <body> <iframe name="target" width="500" height="500" style="..."></iframe> <div style="..."> <ul> <li><a href="" target="target">Cookie的创建</a></li> <li><a href="" target="target">cookie的获取</a></li> <li><a href="" target="target">cookie值的修改</a></li> <li>cookie的存活周期</li> <li> <ul> <li><a href="" target="target">cookie的默认存活时间(会话)</a></li> <li><a href="" target="target">Cookie立即删除</a></li> <li><a href="" target="target">Cookie存活3600秒(1小时)</a></li> </ul> </li> <li><a href="" target="target">Cookie的路径设置</a></li> <li><a href="" target="target">Cookie的用户免登录练习</a></li> </ul> </div> </body> </html>
导入 service.jar包
src/ 新建 com.servlet包/ 复制 BaseServlet
package com.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.lang.reflect.Method; public abstract class BaseServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String action=req.getParameter("action"); //action的value和调用的方法名是统一的 //反射 try { //获取action业务鉴别字符串,获取相应的业务方法 反射对象 Method method = this.getClass().getDeclaredMethod(action,HttpServletRequest.class,HttpServletResponse.class); //调用目标业务方法 method.invoke(this,req,resp); } catch (Exception e) { e.printStackTrace(); } } }
b)如何创建Cookie
新建CookieServlet servlet包下,并配置web.xml
package com.servlet; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class CookieServlet extends BaseServlet{ protected void createCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1 创建Cookie对象 Cookie cookie =new Cookie("key1","value1"); //2 通知客户端保存Cookie resp.addCookie(cookie); //1 创建Cookie对象 Cookie cookie2 = new Cookie("key2", "value2"); //2 通知客户端保存Cookie resp.addCookie(cookie2); //1 创建Cookie对象 Cookie cookie3 = new Cookie("key3", "value3"); //2 通知客户端保存Cookie resp.addCookie(cookie3); resp.getWriter().write("Cookie创建成功"); } }
修改 cookie.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.O1 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="pragma" content="no-cache" /> <meta http-equiv="cache-control" kontent="no-cache" /> <meta http-equiv="Expires" content="0" /> <meta http-equiv="content-Type" content="text/html; charset=UTF-8"> <title>Cookie</title> <base href="http://localhost:8080/13_cookie_session/"> <style type="text/css"> ul li{ list-style:none; } </style> </head> <body> <iframe name="target" width="500" height="500" style="..."></iframe> <div style="..."> <ul> <li><a href="cookieServlet?action=createCookie" target="target">Cookie的创建</a></li> <li><a href="" target="target">cookie的获取</a></li> <li><a href="" target="target">cookie值的修改</a></li> <li>cookie的存活周期</li> <li> <ul> <li><a href="" target="target">cookie的默认存活时间(会话)</a></li> <li><a href="" target="target">Cookie立即删除</a></li> <li><a href="" target="target">Cookie存活3600秒(1小时)</a></li> </ul> </li> <li><a href="" target="target">Cookie的路径设置</a></li> <li><a href="" target="target">Cookie的用户免登录练习</a></li> </ul> </div> </body> </html>
修改 BaseServlet 以解决中文乱码
package com.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.lang.reflect.Method; public abstract class BaseServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //解决post请求中文乱码问题 //一定要在获取请求参数之前调用才有效 req.setCharacterEncoding("UTF-8"); //解决响应中文乱码问题 resp.setContentType("text/html;charset=UTF-8"); String action=req.getParameter("action"); //action的value和调用的方法名是统一的 //反射 try { //获取action业务鉴别字符串,获取相应的业务方法 反射对象 Method method = this.getClass().getDeclaredMethod(action,HttpServletRequest.class,HttpServletResponse.class); //调用目标业务方法 method.invoke(this,req,resp); } catch (Exception e) { e.printStackTrace(); } } }
结果
c) 服务器如何获取coolie
服务器获取客户端的Cookie只需一行代码:req.getCookies() : Cookie[]
修改 cookie.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.O1 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="pragma" content="no-cache" /> <meta http-equiv="cache-control" kontent="no-cache" /> <meta http-equiv="Expires" content="0" /> <meta http-equiv="content-Type" content="text/html; charset=UTF-8"> <title>Cookie</title> <base href="http://localhost:8080/13_cookie_session/"> <style type="text/css"> ul li{ list-style:none; } </style> </head> <body> <iframe name="target" width="500" height="500" style="..."></iframe> <div style="..."> <ul> <li><a href="cookieServlet?action=createCookie" target="target">Cookie的创建</a></li> <li><a href="cookieServlet?action=getCookie" target="target">cookie的获取</a></li> <li><a href="" target="target">cookie值的修改</a></li> <li>cookie的存活周期</li> <li> <ul> <li><a href="" target="target">cookie的默认存活时间(会话)</a></li> <li><a href="" target="target">Cookie立即删除</a></li> <li><a href="" target="target">Cookie存活3600秒(1小时)</a></li> </ul> </li> <li><a href="" target="target">Cookie的路径设置</a></li> <li><a href="" target="target">Cookie的用户免登录练习</a></li> </ul> </div> </body> </html>
增加语句 CookieServlet
package com.servlet; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class CookieServlet extends BaseServlet { protected void createCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1 创建Cookie对象 Cookie cookie = new Cookie("key1", "value1"); //2 通知客户端保存Cookie resp.addCookie(cookie); //1 创建Cookie对象 Cookie cookie2 = new Cookie("key2", "value2"); //2 通知客户端保存Cookie resp.addCookie(cookie2); //1 创建Cookie对象 Cookie cookie3 = new Cookie("key3", "value3"); //2 通知客户端保存Cookie resp.addCookie(cookie3); resp.getWriter().write("Cookie创建成功"); } protected void getCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Cookie[] cookies = req.getCookies(); for (Cookie cookie : cookies) { //getName 方法返回Cookie的key(名) //getValue 方法返回Cookie的value(值) resp.getWriter().write("Cookie["+cookie.getName()+"="+cookie.getValue()+"]<br>"); } } }
结果
获取指定的Cookie
修改 CookieServlet
package com.servlet; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class CookieServlet extends BaseServlet { protected void createCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1 创建Cookie对象 Cookie cookie = new Cookie("key1", "value1"); //2 通知客户端保存Cookie resp.addCookie(cookie); //1 创建Cookie对象 Cookie cookie2 = new Cookie("key2", "value2"); //2 通知客户端保存Cookie resp.addCookie(cookie2); //1 创建Cookie对象 Cookie cookie3 = new Cookie("key3", "value3"); //2 通知客户端保存Cookie resp.addCookie(cookie3); resp.getWriter().write("Cookie创建成功"); } protected void getCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Cookie[] cookies = req.getCookies(); for (Cookie cookie : cookies) { //getName 方法返回Cookie的key(名) //getValue 方法返回Cookie的value(值) resp.getWriter().write("Cookie["+cookie.getName()+"="+cookie.getValue()+"]<br>"); } Cookie iWantCookie =null; for (Cookie cookie : cookies) { if ("key2".equals(cookie.getName())){ iWantCookie=cookie; break; } } //如果不等于null,说明赋过值,也就是找到了需要的Cookie if (iWantCookie!=null){ resp.getWriter().write("找到了需要的Cookie"); } } }
结果
创建工具类以获取指定的Cookie
com/创建util包/创建CookieUtils类
package com.util; import javax.servlet.http.Cookie; public class CookieUtils { /** * 查找指定名称的Cookie对象 * @param name * @param cookies * @return */ public static Cookie findCookie(String name,Cookie[] cookies){ if (name==null||cookies==null||cookies.length==0){ return null; } for (Cookie cookie : cookies) { if (name.equals(cookie.getName())){ return cookie; } } return null; } }
修改 CookieServlet
package com.servlet; import com.util.CookieUtils; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class CookieServlet extends BaseServlet { protected void createCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1 创建Cookie对象 Cookie cookie = new Cookie("key1", "value1"); //2 通知客户端保存Cookie resp.addCookie(cookie); //1 创建Cookie对象 Cookie cookie2 = new Cookie("key2", "value2"); //2 通知客户端保存Cookie resp.addCookie(cookie2); //1 创建Cookie对象 Cookie cookie3 = new Cookie("key3", "value3"); //2 通知客户端保存Cookie resp.addCookie(cookie3); resp.getWriter().write("Cookie创建成功"); } protected void getCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Cookie[] cookies = req.getCookies(); for (Cookie cookie : cookies) { //getName 方法返回Cookie的key(名) //getValue 方法返回Cookie的value(值) resp.getWriter().write("Cookie["+cookie.getName()+"="+cookie.getValue()+"]<br>"); } Cookie iWantCookie = CookieUtils.findCookie("key1",cookies); // for (Cookie cookie : cookies) { // if ("key2".equals(cookie.getName())){ // iWantCookie=cookie; // break; // } // } //如果不等于null,说明赋过值,也就是找到了需要的Cookie if (iWantCookie!=null){ resp.getWriter().write("找到了需要的Cookie"); } } }
d) Cookie值的修改
方案一
1、先创建一个要修改的同名的Cookie对象
2、在构造器,同时赋于新的Cookie值。
3、调用response.addCookie( Cookie );
修改 cookie.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.O1 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="pragma" content="no-cache" /> <meta http-equiv="cache-control" kontent="no-cache" /> <meta http-equiv="Expires" content="0" /> <meta http-equiv="content-Type" content="text/html; charset=UTF-8"> <title>Cookie</title> <base href="http://localhost:8080/13_cookie_session/"> <style type="text/css"> ul li{ list-style:none; } </style> </head> <body> <iframe name="target" width="500" height="500" style="..."></iframe> <div style="..."> <ul> <li><a href="cookieServlet?action=createCookie" target="target">Cookie的创建</a></li> <li><a href="cookieServlet?action=getCookie" target="target">cookie的获取</a></li> <li><a href="cookieServlet?action=updateCookie" target="target">cookie值的修改</a></li> <li>cookie的存活周期</li> <li> <ul> <li><a href="" target="target">cookie的默认存活时间(会话)</a></li> <li><a href="" target="target">Cookie立即删除</a></li> <li><a href="" target="target">Cookie存活3600秒(1小时)</a></li> </ul> </li> <li><a href="" target="target">Cookie的路径设置</a></li> <li><a href="" target="target">Cookie的用户免登录练习</a></li> </ul> </div> </body> </html>
增加语句 CookieServlet
package com.servlet; import com.util.CookieUtils; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class CookieServlet extends BaseServlet { protected void createCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1 创建Cookie对象 Cookie cookie = new Cookie("key1", "value1"); //2 通知客户端保存Cookie resp.addCookie(cookie); //1 创建Cookie对象 Cookie cookie2 = new Cookie("key2", "value2"); //2 通知客户端保存Cookie resp.addCookie(cookie2); //1 创建Cookie对象 Cookie cookie3 = new Cookie("key3", "value3"); //2 通知客户端保存Cookie resp.addCookie(cookie3); resp.getWriter().write("Cookie创建成功"); } protected void getCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Cookie[] cookies = req.getCookies(); for (Cookie cookie : cookies) { //getName 方法返回Cookie的key(名) //getValue 方法返回Cookie的value(值) resp.getWriter().write("Cookie["+cookie.getName()+"="+cookie.getValue()+"]<br>"); } Cookie iWantCookie = CookieUtils.findCookie("key1",cookies); // for (Cookie cookie : cookies) { // if ("key2".equals(cookie.getName())){ // iWantCookie=cookie; // break; // } // } //如果不等于null,说明赋过值,也就是找到了需要的Cookie if (iWantCookie!=null){ resp.getWriter().write("找到了需要的Cookie"); } } protected void updateCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 方案一 // 1、先创建一个要修改的同名的Cookie对象 // 2、在构造器,同时赋于新的Cookie值。 Cookie cookie=new Cookie("key1","newValue1"); // 3、调用response.addCookie( Cookie );//通知客户端保存修改 resp.addCookie(cookie); resp.getWriter().write("key1的Cookie已经修改好"); } }
结果
方案二:
1、先查找到需要修改的Cookie对象
2、调用setValue()方法赋于新的Cookie值。
3、调用response.addCookie()通知客户端保存修改
修改 CookieServlet
package com.servlet; import com.util.CookieUtils; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class CookieServlet extends BaseServlet { protected void createCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1 创建Cookie对象 Cookie cookie = new Cookie("key1", "value1"); //2 通知客户端保存Cookie resp.addCookie(cookie); //1 创建Cookie对象 Cookie cookie2 = new Cookie("key2", "value2"); //2 通知客户端保存Cookie resp.addCookie(cookie2); //1 创建Cookie对象 Cookie cookie3 = new Cookie("key3", "value3"); //2 通知客户端保存Cookie resp.addCookie(cookie3); resp.getWriter().write("Cookie创建成功"); } protected void getCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Cookie[] cookies = req.getCookies(); for (Cookie cookie : cookies) { //getName 方法返回Cookie的key(名) //getValue 方法返回Cookie的value(值) resp.getWriter().write("Cookie["+cookie.getName()+"="+cookie.getValue()+"]<br>"); } Cookie iWantCookie = CookieUtils.findCookie("key1",cookies); // for (Cookie cookie : cookies) { // if ("key2".equals(cookie.getName())){ // iWantCookie=cookie; // break; // } // } //如果不等于null,说明赋过值,也就是找到了需要的Cookie if (iWantCookie!=null){ resp.getWriter().write("找到了需要的Cookie"); } } protected void updateCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 方案一 1、先创建一个要修改的同名的Cookie对象 2、在构造器,同时赋于新的Cookie值。 // Cookie cookie=new Cookie("key1","newValue1"); 3、调用response.addCookie( Cookie );//通知客户端保存修改 // resp.addCookie(cookie); // resp.getWriter().write("key1的Cookie已经修改好"); // 方案二: // 1、先查找到需要修改的Cookie对象 Cookie cookie=CookieUtils.findCookie("key2",req.getCookies()); if (cookie!=null){ // 2、调用setValue()方法赋于新的Cookie值。 cookie.setValue("newValue2"); // 3、调用response.addCookie()通知客户端保存修改 resp.addCookie(cookie); } resp.getWriter().write("key2的Cookie已经修改好"); } }
结果
注意
e) 浏览器查看Cookie
谷歌浏览器如何查看Cookie
火狐浏览器如何查看Cookie
f)Cookie 生命控制
Cookie的生命控制指的是如何管理cookie什么时候被销毁〈删除)
setMaxAge()
正数,表示在指定的秒数后过期
负数,表示浏览器一关,Cookie就会被删除(默认值是-1)
零,表示马上删除Cookie
修改 cookie.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.O1 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="pragma" content="no-cache" /> <meta http-equiv="cache-control" kontent="no-cache" /> <meta http-equiv="Expires" content="0" /> <meta http-equiv="content-Type" content="text/html; charset=UTF-8"> <title>Cookie</title> <base href="http://localhost:8080/13_cookie_session/"> <style type="text/css"> ul li{ list-style:none; } </style> </head> <body> <iframe name="target" width="500" height="500" style="..."></iframe> <div style="..."> <ul> <li><a href="cookieServlet?action=createCookie" target="target">Cookie的创建</a></li> <li><a href="cookieServlet?action=getCookie" target="target">cookie的获取</a></li> <li><a href="cookieServlet?action=updateCookie" target="target">cookie值的修改</a></li> <li>cookie的存活周期</li> <li> <ul> <li><a href="cookieServlet?action=defaultLife" target="target">cookie的默认存活时间(会话)</a></li> <li><a href="cookieServlet?action=deleteNow" target="target">Cookie立即删除</a></li> <li><a href="cookieServlet?action=life3600" target="target">Cookie存活3600秒(1小时)</a></li> </ul> </li> <li><a href="" target="target">Cookie的路径设置</a></li> <li><a href="" target="target">Cookie的用户免登录练习</a></li> </ul> </div> </body> </html>
修改 CookieServlet
package com.servlet; import com.util.CookieUtils; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class CookieServlet extends BaseServlet { protected void createCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1 创建Cookie对象 Cookie cookie = new Cookie("key1", "value1"); //2 通知客户端保存Cookie resp.addCookie(cookie); //1 创建Cookie对象 Cookie cookie2 = new Cookie("key2", "value2"); //2 通知客户端保存Cookie resp.addCookie(cookie2); //1 创建Cookie对象 Cookie cookie3 = new Cookie("key3", "value3"); //2 通知客户端保存Cookie resp.addCookie(cookie3); resp.getWriter().write("Cookie创建成功"); } protected void getCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Cookie[] cookies = req.getCookies(); for (Cookie cookie : cookies) { //getName 方法返回Cookie的key(名) //getValue 方法返回Cookie的value(值) resp.getWriter().write("Cookie["+cookie.getName()+"="+cookie.getValue()+"]<br>"); } Cookie iWantCookie = CookieUtils.findCookie("key1",cookies); // for (Cookie cookie : cookies) { // if ("key2".equals(cookie.getName())){ // iWantCookie=cookie; // break; // } // } //如果不等于null,说明赋过值,也就是找到了需要的Cookie if (iWantCookie!=null){ resp.getWriter().write("找到了需要的Cookie"); } } protected void updateCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 方案一 1、先创建一个要修改的同名的Cookie对象 2、在构造器,同时赋于新的Cookie值。 // Cookie cookie=new Cookie("key1","newValue1"); 3、调用response.addCookie( Cookie );//通知客户端保存修改 // resp.addCookie(cookie); // resp.getWriter().write("key1的Cookie已经修改好"); // 方案二: // 1、先查找到需要修改的Cookie对象 Cookie cookie=CookieUtils.findCookie("key2",req.getCookies()); if (cookie!=null){ // 2、调用setValue()方法赋于新的Cookie值。 cookie.setValue("newValue2"); // 3、调用response.addCookie()通知客户端保存修改 resp.addCookie(cookie); } resp.getWriter().write("key2的Cookie已经修改好"); } protected void defaultLife(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Cookie cookie=new Cookie("defaultLife","defaultLife"); cookie.setMaxAge(-1);//设置存活时间 resp.addCookie(cookie); } protected void deleteNow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //先找到你要删除的Cookie对象 Cookie cookie=CookieUtils.findCookie("key3",req.getCookies()); if (cookie!=null){ //调用setMaxAge(0) cookie.setMaxAge(0);//表示马上删除,都不需要等待浏览器关闭 //调用response.addCookie( Cookie ) resp.addCookie(cookie); resp.getWriter().write("key3的Cookie已经被删除"); } } protected void life3600(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //先找到你要删除的Cookie对象 Cookie cookie=new Cookie("life3600","life3600"); cookie.setMaxAge(60*60);//设置Cookie一小时之后被删除,无效 resp.addCookie(cookie); resp.getWriter().write("已经创建了一个存活一小时的Cookie"); } }