今日分享===Cookie & Session

今天总结一下Cookie & Session的小知识.
Cookie对象是一种浏览器的技术
它是通过服务器的程序能将一些只须保存在客户端,或者在客户端进行处理的数据,放在本地计算机上,不需要通过网络传输.

cookie的大小在4kb左右,每个浏览器在同一域名小存放cookie的数量是有限的,谷歌浏览器大约是50个.

优点:提高网页的效率,减轻服务器的负载
缺点:安全性较差

Cookie对象:

1.创建Cookie
Cookie cookie = new Cookie(“cookie的名称”“cookie的值”);
ps:键和值都是字符串,不支持中文

2.发送cookie
response.addCookie(cookie对象);

public class Servlet01 extends HttpServlet {

	@Override
	protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
		System.out.println("cookie01...");
		
		//创建cookie对象
		Cookie cookie = new Cookie("uname", "zhangsan");
		
		//发送cookie
		arg1.addCookie(cookie);
	}

}

3.获取Cookie的值
request.getCookies(); 返回的是所有的Cookie的数组

public class Servlet02 extends HttpServlet {

	@Override
	protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
		System.out.println("cookie02...");

		// 获取一个cookie的数组
		Cookie[] cookies = arg0.getCookies();
		// 判断数组是否为空
		if (cookies != null & cookies.length > 0) {
			// 遍历数组
			for (Cookie cookie : cookies) {
				// 得到cookie的值
				// 得到value的值
				String name = cookie.getName();
				String value = cookie.getValue();

				// 得到指定键的值
				if ("uname".equals(name)) {
					System.out.println("指定cookie的值为:" + value);
				}

			}

		}
	}

}

4.Cookie的到期时间设置
ps:Cookie是浏览器技术,关闭服务器不会影响cookie的变化
到期时间:指该cookie何时失效.
默认为当前浏览器关闭即失效.
可以手动设置cookie的有效时间(通过到期时间来计算)
通过setMaxAge(int time);方法设定cookie的最大有效时间,以秒为单位.
大于0的整数,表示存储的秒数
若为负数,则表示不存储该 cookie;若为 0,则删 除该 cookie。
负整数:cookie 的 maxAge 属性的默认值就是-1,表示只在浏览器内存中存活,一旦关闭浏览器窗口,那么 cookie 就会消失。
正整数:表示 cookie 对象可存活指定的秒数。当生命大于 0 时,浏览器会把 Cookie 保存到硬盘上,就算关闭浏览器,就算重启客户端电脑,cookie 也会存活相应的时间。
零:cookie 生命等于 0 是一个特殊的值,它表示 cookie 被作废!也就是说,如果原来浏览器已经保存了这个 Cookie,那么可以通过 Cookie 的setMaxAge(0)来删除这个 Cookie。 无论是在浏览器内存中,还是在客户端硬盘上都会删除这个 Cookie。

public class Servlet04 extends HttpServlet {

	@Override
	protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
		System.out.println("cookie04...");
		// 创建cookie对象
		Cookie cookie = new Cookie("uname", "wangwu");
		// 设置失效时间
		cookie.setMaxAge(15);// 15秒后失效,
		cookie.setMaxAge(0);// 即刻删除cookie
		cookie.setMaxAge(-1);// 默认关闭浏览器即失效
		// 发送cookie
		arg1.addCookie(cookie);

		//删除指定cookie对象
		Cookie cookie2 =new Cookie("uname", null);
		cookie2.setMaxAge(0);//删除cookie
		arg1.addCookie(cookie2);//响应cookie
	}

}

5.cookie不能存中文
有中文的话则通过URLEncoder.encode()来进行编码;
获取时通过URLDecoder.decode()来进行解码

public class Servlet03 extends HttpServlet {

	@Override
	protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
		System.out.println("cookie03...");
		//创建cookie对象
		Cookie cookie=new Cookie(URLEncoder.encode("姓名"), URLEncoder.encode("张三"));
		//发送cookie对象
		arg1.addCookie(cookie);
		
	}

}

6.Cookie的覆盖
如果出现相同的cookie名称,则会覆盖原来的cookie(在相同的域名和路径下)

public class Servlet05 extends HttpServlet {

	@Override
	protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
		System.out.println("cookie05...");

		Cookie cookie =new Cookie("uname", "wangwu");
		
		arg1.addCookie(cookie);
	}

}

7.Cookie的path
只有访问的地址中包含cookie的path值时,才能得到cookie对象.

public class Servlet06 extends HttpServlet {

	@Override
	protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
		System.out.println("cookie06...");

		// 创建cookie
		Cookie cookie = new Cookie("uname", "admin");
		// 设置path(当前服务器下任意资源都可以访问)
		cookie.setPath("/");
		// 发送cookie
		arg1.addCookie(cookie);

	}

}

HttpSession对象

1.Session的作用
Session的作用就是为了标识一次会话,或者说确认一个用户;
并且在一次会话(一个用户的多次请求)期间共享数据.
可以通过 request.getSession()方法,来获取当前会话的session对象.

public class Session01 extends HttpServlet {

	@Override
	protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
		System.out.println("Session01...");
		// 获取session对象
		HttpSession session = arg0.getSession();
		// 会话标识符
		String id =session.getId();
		System.out.println("会话标识符:"+id);
		//创建时间
		System.out.println("创建时间:"+session.getCreationTime());
		//最后一次访问时间
		System.out.println("最后一次访问时间:"+session.getLastAccessedTime());
		//是否是新的session
		System.out.println("是否是新的session对象:"+session.isNew());
	}

}

2.JSESSIONID
SessionId是为了标识一次会话的唯一标识
每当一次请求到达服务器,如果开启了会话(访问了session),访问器第一步会查看是否从客户端回传一个名为JSESSIONID的cookie;
如果JSESSIONID不存在,则服务器会新建session对象,并重新标识;
如果JSESSIONID存在,服务器会将客户端回传回来的ID去服务器中查找与之对应的session对象.
如果没找到,则服务器会新建session对象,并重新标识
如果找到,则获取session对象,响应给客户端;

public class Session02 extends HttpServlet {

	@Override
	protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
		System.out.println("Session02...");
		// 创建或获取session对象
		HttpSession session = arg0.getSession();
		//设置session域对象
		session.setAttribute("aa", "AA");
		//设置request域对象
		arg0.setAttribute("bb", "BB");
		//请求转发跳转到指定的servlet
		arg0.getRequestDispatcher("s03").forward(arg0, arg1);
		
		//重定向
		arg1.sendRedirect("s03");
	}

}

3.Session域对象
通过 setAttribute(name,value);方法向域对象中添加数据,
通过getAttribute(name) 从域对象中获取数据,
通过removeAttribute(name)从域对象中移出数据.

public class Session03 extends HttpServlet {

	@Override
	protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
		System.out.println("Session03...");
		// 创建或获取session对象
		HttpSession session = arg0.getSession();
		System.out.println(session.getId());
		//获取session域对象
		String aa = (String) session.getAttribute("aa");
		System.out.println("session中的域对象:"+aa);
		
		//获取request域对象
		String bb = (String) arg0.getAttribute("bb");
		System.out.println("Request中的域对象:"+bb);
	}

}

4.Session的失效
1.达到最大不活动时间
Tomcat中默认最大不活动时间为30分钟
可以自行修改默认不活动时间,但不建议.在web.xml文件中

<session-config>
        		<session-timeout>30</session-timeout>
</session-config>

2.自己设定过期时间
通过 session.setMaxInactiveInterval(int);来设定 session 的最大不活动时间,单位为秒。
通过 getMaxInactiveInterval();方法来查看当前 Session 对象的最大不活动时间。

public class Session04 extends HttpServlet {

	@Override
	protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
		System.out.println("Session03...");
		// 创建或获取session对象
		HttpSession session = arg0.getSession();
		System.out.println(session.getId());
		//当前session对象的最大不活动时间
		System.out.println(session.getMaxInactiveInterval());
		//设置session对象的最大不活动时间
		session.setMaxInactiveInterval(150);
		//手动销毁session
		//session.invalidate();
		
	}

}

3.立即失效
手动销毁session对象 session.invalidate();

4.关闭浏览器
session的底层是依赖cookie,默认关闭浏览器失效

5.关闭服务器
非正常关闭服务器时才会失效
如果是正常关闭服务器,session会被钝化到本地磁盘,下次访问会从本地磁盘中活化出来.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值