cookie & session

Cookie

基本操作

该数据在HTTP协议中, 往返存在于 HTTP头中

服务器生成cookie时, 在 response 头里面设置方法如下

Set-Cookie: id1=9527; Expires=Thu, 10-Apr-2014 06:22:10 GMT
Set-Cookie: id2=4399
Set-Cookie: cn=%E4%B8%AD%E6%96%87

其中  Expires 参数值表示这个cookie值的到期时间

浏览器在收到这种头后, 下次发送 request时, 在HTTP头上带上:
Cookie: id=4399; id2=4399; cn=%E4%B8%AD%E6%96%87

servlet中具体操作流程:

public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html; charset=UTF-8");

		// System.out.println(request.getCookies().length);

		for (Cookie cookie : request.getCookies()) {
			response.getWriter().print(
					cookie.getName() + " : "
							+ URLDecoder.decode(cookie.getValue(), "UTF-8"));
			response.getWriter().print("\n");

			/*
			 * 浏览器输出 id : 4399 id2 : 4399 cn : 中文
			 */
		}

		Cookie cookie1 = new Cookie("id1", "9527");
		Cookie cookie2 = new Cookie("id2", "4399");

		Cookie cookie3 = new Cookie("cn", URLEncoder.encode("中文", "UTF-8"));	//COOKIE 中文需要编码

		cookie1.setMaxAge(5); // 设置过期时间为 5 秒后

		response.addCookie(cookie1);
		response.addCookie(cookie2);
		response.addCookie(cookie3);

	}

cookie目录区别

cookie可以设置不同的目录来区别, 浏览器在得到 带路径的cookie后, 只有下次访问对应的资源路径时才在 request中带上cookie值

服务器 response 带路径cookie时
Set-Cookie:id1=9527; Expires=Thu, 10-Apr-2014 07:23:15 GMT; Path=/123
Set-Cookie:id2=4399; Path=/123
Set-Cookie:cn=%E4%B8%AD%E6%96%87; Path=/123

浏览器在下次访问 /主机名/123 这个资源时才会在request HTTP 头中带上上次收到的 cookie值
Path=/ 时, 访问当前主机所有资源时都会带上该cookie, 默认 cookiepath为 null


public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html; charset=UTF-8");

		// System.out.println(request.getCookies().length);


		Cookie cookie1 = new Cookie("id1", "9527");
		Cookie cookie2 = new Cookie("id2", "4399");

		Cookie cookie3 = new Cookie("cn", URLEncoder.encode("中文", "UTF-8"));	//COOKIE 中文需要编码

		cookie1.setMaxAge(5); // 设置过期时间为 5 秒后

		System.out.println(cookie1.getPath()); // 默认为NULL

		cookie1.setPath("/123");
		cookie2.setPath("/123");
		cookie3.setPath("/");

		System.out.println(cookie1.getPath()); // 默认为NULL

		response.addCookie(cookie1);
		response.addCookie(cookie2);
		response.addCookie(cookie3);
	}

TestCookiePath 为例外一个路径的资源
public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.getWriter().print(request.getCookies()[0].getValue()); // 只能得到上个代码片设置的中文cookie
	}

session

基于cookie中的  JSESSIONID 或者 请求URL中的 参数 JSESSIONID 值作为 主键
浏览器只保存 主键数据, 实际存储数据在服务器, 服务器缺省 将30分钟未被访问过的session删除
不同于cookie中存储数据格式和大小有限制, session可直接将对象进行缓存

基础操作


服务器从request中获取 session主键时, 诺没有则新建一个, 并在response中下发主键到浏览器
浏览器下次访问服务器时, HTTP中

Cookie: JSESSIONID=9020E6ABEA6D3399822ED2184BCA45B2

servlet中操作过程:

public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		HttpSession seesion = request.getSession(); // 没有则立即创建
		System.out.println(seesion.isNew());// 此session是否当次交互创建

		System.out.println(seesion.getAttribute("haa")); // 第二次运行时输出 2312312
		seesion.setAttribute("haa", 2312312);
	}


不使用cookie存储主键方法

服务器在处理某些跳转时, 需要使用 session的, 可以在服务器生成跳转URL , 自行将 URL编码成 带 session主键的
public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		HttpSession seesion = request.getSession(); // 没有则立即创建
		System.out.println(seesion.isNew());// 此session是否当次交互创建

		System.out.println(seesion.getAttribute("haa")); // 第二次运行时输出 2312312
		seesion.setAttribute("haa", 2312312);
		
		String urlString = "/ServletContext/TestSession2";
		
		urlString = response.encodeUrl(urlString);

		System.out.println(urlString);
		// 编码为
		// /ServletContext/TestSession2;jsessionid=8FD1E71653F4FC7F4E0A16ABF62E6870

		response.sendRedirect(urlString);
	}













 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值