Cookie 是服务器通知客户端保存键值对的一种技术,客户端有了 Cookie 后,每次请求都发送给服务器并且每个 Cookie 的大小不能超过 4kb。
import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class CookieServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8"); //1 创建Cookie 对象 Cookie cookie = new Cookie("username", "root"); //2 通知客户端保存Cookie resp.addCookie(cookie); //1 创建Cookie 对象 Cookie cookie1 = new Cookie("password", "123456"); //2 通知客户端保存Cookie resp.addCookie(cookie1); resp.getWriter().write("Cookie 创建成功"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
CookieServlet CookieServlet CookieServlet /createCookie
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; } }
import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class getCookieServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8"); Cookie[] cookies = req.getCookies(); for (Cookie cookie : cookies) { // getName 方法返回Cookie 的key(名) // getValue 方法返回Cookie 的value 值 resp.getWriter().write("Cookie[" + cookie.getName() + "=" + cookie.getValue() + "]
"); } Cookie mycookie = CookieUtils.findCookie("username", cookies); // 如果不等于null,说明赋过值,也就是找到了需要的Cookie if (mycookie != null) { resp.getWriter().write("找到了需要的 Cookie"); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
getCookieServlet getCookieServlet getCookieServlet /getCookie
更新cookie有俩种方法:
管理 Cookie 什么时候被销毁(删除)
通过Cookie对象中的setMaxAge()来控制Cookie的声明周期。
cookie.setMaxAge(60 * 60);
表示一小时后cookie失效cookie.setMaxAge(-1);
表示浏览器一关cookie就会失效cookie.setMaxAge(-1);
表示cookie马上就会失效。Cookie 的 path 属性可以有效的过滤哪些 Cookie 可以发送给服务器。哪些不发。
cookie.setPath( req.getContextPath() + "/abc" ); 表示工程路径/abc路径下面的可以获取到cookie信息。
Session 就是一个接口(HttpSession),作为一个会话,它是用来维护一个客户端和服务器之间关联的一种技术。每个客户端都有自己的一个 Session 会话。
通过HttpServletRequest中的getSession()方法来创建一个session对象,之后使用该方法都是获取到之前创建的session对象。
isNew()
: 判断到底是不是刚创建出来的(新的)getId()
: 得到 Session 的会话 id 值。SessionServlet类
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; public class SessionServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { HttpSession session = req.getSession(); session.setAttribute("username","xiaoming"); session.setAttribute("password","123456"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
getSessionServlet类
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; public class getSessionServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { HttpSession session = req.getSession(); String username = (String) session.getAttribute("username"); String password = (String) session.getAttribute("password"); System.out.println("用户名:" + username + ",密码:" + password); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
SessionServlet SessionServlet SessionServlet /createSession getSessionServlet getSessionServlet getSessionServlet /getSession
20
Session 技术,底层其实是基于 Cookie 技术来实现的。
欢迎java热爱者了解文章,作者将会持续更新中,期待各位友友的关注和收藏,另外对编程感兴趣的友友们可以加以下群共同学习。群号:127871664
上一篇:安全焦点是什么意思啊
下一篇:sunbook是什么衣服