Cookie和Seesion

我的网站:欢迎访问奥

Cookie和Session

Http协议

无状态的(基于TCP)

需要在浏览器和服务器之间的多次交互需要数据共享

什么是会话

web应用中的会话是指一个客户端浏览器与web服务器之间连续发生的一系列请求和响应过程;

会话可简单理解为:用户开一个浏览器,访问某一个web网站,在这个站点网站有多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一次会话;

Cookie

Cookie将信息保存在浏览器中 – > 不安全

添加Cookie

​ Cookie c = new Cookie("","");

​ resp.addCookie();

查询Cookie

​ Cookie[] cookies = req.getCookies();

修改Cookie

​ 第一种:添加一个name值一样的Cookie

​ 第二种:s.setValue()

解决Cookie的value值的中文乱码问题

​ cookie的name不能使用中文,value可以,但是需要编码解码

Cookie生命周期

生:Cookie在创建之后放到浏览器中
死:关闭浏览器

Demo

package com.ifueen.classtest;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;

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;

@WebServlet("*.cookie")
public class CookieServlet extends HttpServlet{
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse reps) throws ServletException, IOException {
		String uri = req.getRequestURI();//拿到uri
		reps.setContentType("text/html;charset=utf-8");
		//截取uri的长度,使之成为我们想要的
		uri = uri.substring(uri.lastIndexOf("/")+1, uri.lastIndexOf("."));
		switch (uri) {
		case "add":
			doAdd(req,reps); 
			break;
		case "find":
			doFind(req,reps);
			break;
		}
	}

	private void doFind(HttpServletRequest req, HttpServletResponse reps) throws IOException {
		// TODO Auto-generated method stub
		//拿到Cookie对象的数组
		Cookie[] cookies = req.getCookies();
		if (cookies==null) {
			reps.getWriter().print("<h2>没有Cookie</h2>");
		}else{
		for (Cookie cookie : cookies) {	//遍历
			reps.getWriter().print("<h2>"+cookie.getName()+"</br>"+URLDecoder.decode(cookie.getValue(), "utf-8")+"</h2>");
		}
		}
	}

	/**
	 * @param req
	 * @param reps
	 * 添加cookie
	 * @throws IOException 
	 */
	private void doAdd(HttpServletRequest req, HttpServletResponse reps) throws IOException {
		// TODO Auto-generated method stub
		Cookie cookie1 = new Cookie("name", URLEncoder.encode("这个世界会好吗", "utf-8"));
		Cookie cookie2 = new Cookie("nick", URLEncoder.encode("妈妈,他们抛弃了我", "utf-8"));
		reps.addCookie(cookie1);
		cookie1.setMaxAge(60);
		reps.addCookie(cookie2);
		reps.getWriter().print("<h2>Cookie添加成功!</h2>");
	}
}

Session

什么是Session?

在服务器端维护的这些用于保存与不同客户端交互时的数据的对象叫做Session;

1.创建session

​ 手动创建:req.getSession();
​ 自动创建:访问jsp

2.使用Session

​ setAttribute();
​ getAttribute();
​ removeAttribute();

3.设置过期时间

​ session.setMaxInactiveInterval(30);

4.删除session

​ 使用场景:注销用户
​ session.invalidate();

5.Session优缺点

​ 优点:安全,保存的数据类型没有限制,大小没有限制
​ 缺点:增加服务器的压力

Demo

package com.ifueen.classtest;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;

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 javax.servlet.http.HttpSession;

@WebServlet("*.session")
public class SessionServlet extends HttpServlet{
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse reps) throws ServletException, IOException {
		String uri = req.getRequestURI();//拿到uri
		reps.setContentType("text/html;charset=utf-8");
		//截取uri的长度,使之成为我们想要的
		uri = uri.substring(uri.lastIndexOf("/")+1, uri.lastIndexOf("."));
		switch (uri) {
		case "add":
			doAdd(req,reps); 
			break;
		
		}
	}

	/**
	 * @param req
	 * @param reps
	 * 添加session
	 * @throws IOException 
	 */
	private void doAdd(HttpServletRequest req, HttpServletResponse reps) throws IOException {
		// TODO Auto-generated method stub
		HttpSession session = req.getSession();
		session.setAttribute("name", "fueen");
		session.setMaxInactiveInterval(20);
		reps.sendRedirect("session.jsp");
	}
}

Cookie和Seesion的区别(面试题)

Cookie是将数据存放在浏览器中,可以选择在内存或者硬盘里面,相对来说不太安全,Cookie限制了存储的数量,最多只允许4KB

Session将数据存放在服务器中的,Seesion存储的数量没有限制

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值