Cookie和Session
1.Cookie
1.1 什么是Cookie
1、Cookie 翻译过来是饼干的意思。
2、Cookie 是服务器通知客户端保存键值对的一种技术。
3、客户端有了 Cookie 后,每次请求都发送给服务器。
4、每个 Cookie 的大小不能超过 4kb
1.2 Cookie的创建
public class CookieServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String action = req.getParameter("action");
if("createCookie".equals(action)){
createCookie(req,resp);
}
}
private void createCookie(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//1.创建Cookie对象
Cookie cookie1 = new Cookie("key1","value1");
Cookie cookie2 = new Cookie("key2","value2");
//2.通知客户端保存