http协议是一个无状态的协议,你每一个跳转到下一个页面的时候都是需要先登录才能使用,这样就很麻烦比如淘宝,没有cookie和session的话,用户在首页已经登录上去了,但是需要再次登录才能选择商品,需要再次登录才能放到购物车,需要再次登录才能然后购买,这样用户的体验是相当差的。
cookie是在浏览器中保存的
如果想要使用cookie要保证我们的浏览器是开启cookie,所以说有一定的弊端,如果浏览器没有开启cookie,就不能再使用cookie了
cookie的大小是有限制的,通常是4096byte
cookie的保存是以键值对的形式存在的
//1.cookie的构造方法,目的是实例化出来cookie对象
Cookie(String name,String value)
//2.设置cookie的方法
setValue(String value) //修改cookie的值
setMaxAge(int time) //设置cookie的有效时间
setPath(String path) //设置当前cookie的有效路径
//3.要将cookie发送到浏览器
response.addCookie(Cookie cookie);
package com.by.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
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 SetCookieServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws Exception {
/**
* 1.创建cookie对象
* 将键:java2311 值:sb ,存到cookie对象中
*/
Cookie cookie = new Cookie("msg", "sb");
/**
* 2.设置有效时间
* 正数:表示当前cookie的有效时间
* 负数:表示当前浏览器打开的时候存在,关闭的时候没了
* 0:销毁当前的cookie
*/
cookie.setMaxAge(60*60*24);//设置了有效期是个正数,
//3.把cookie发送到浏览器
response.addCookie(cookie);
}
}
<servlet>
<servlet-name>setCookieservlet-name>
<servlet-class>com.by.servlet.SetCookieServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>setCookieservlet-name>
<url-pattern>/setCookieurl-pattern>
servlet-mapping>
package com.by.servlet;
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 doPost(HttpServletRequest request, HttpServletResponse response) {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
//获取浏览器中cookie,返回值是一个数组
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
System.out.println("==============");
System.out.println(cookie.getName());//获取键
System.out.println(cookie.getValue());//获取值
}
}
}
<servlet>
<servlet-name>getCookieservlet-name>
<servlet-class>com.by.servlet.GetCookieServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>getCookieservlet-name>
<url-pattern>/getCookieurl-pattern>
servlet-mapping>
@WebServlet("/destroyCookie")
public class DestroyCookieServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
//退出登录
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
if (cookie.getName().equals("msg")){
cookie.setMaxAge(0);//销毁cookie
//重新发送给浏览器
response.addCookie(cookie);
}
}
}
}
<servlet>
<servlet-name>destroyCookieservlet-name>
<servlet-class>com.by.servlet.DestroyCookieServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>destroyCookieservlet-name>
<url-pattern>/destroyCookieurl-pattern>
servlet-mapping>
package com.by.servlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
public class SetSessionServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response){
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
//1.获取session对象
HttpSession session = request.getSession();
System.out.println(session);
//获取的是JSESSIONID 服务器唯一的标识
System.out.println(session.getId());
//给session设置一个时间,有效果的,里面放的是秒
session.setMaxInactiveInterval(60*60*24);
User user = new User();
user.setUsername("张5丰");
user.setBirthday(new Date());
user.setSex("1");
session.setAttribute("user",user);
}
}
setSession
com.by.servlet.SetSessionServlet
setSession
/setSession
package com.by.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
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 {
protected void doPost(HttpServletRequest request, HttpServletResponse response){
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
//1.获取Session对象
//第一次创建session的时候默认为true
//false的话,这个session使用的是已经创建好的session对象
HttpSession session = request.getSession(false);
//2.获取session,通过键取值
Object user = session.getAttribute("user");
System.out.println(user);
}
}
<servlet>
<servlet-name>getSessionservlet-name>
<servlet-class>com.by.servlet.GetSessionServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>getSessionservlet-name>
<url-pattern>/getSessionurl-pattern>
servlet-mapping>
package com.by.servlet;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class DestroySessionServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
//1.获取session对象
HttpSession session = request.getSession(false);
//销毁当前的session
session.invalidate();
}
}
<servlet>
<servlet-name>destroySessionservlet-name>
<servlet-class>com.by.servlet.DestroySessionServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>destroySessionservlet-name>
<url-pattern>/destroySessionurl-pattern>
servlet-mapping>