会话技术Cookie&Session

一、会话技术Cookie&Session简介

1.存储客户端的状态
例如网站的购物系统,用户将购买的商品信息存储到哪里?因为Http协议是无状态的,也就是说每个客户访问服务器端资源时,服务器并不知道该客户端是谁,所以需要会话技术识别客户端的状态。会话技术是帮助服务器记住客户端状态

2.会话技术
从打开一个浏览器访问某个站点,到关闭这个浏览器的整个过程,成为一次会话。
会话技术就是记录这次会话中客户端的状态与数据的。
会话技术分为Cookie和Session:
Cookie:数据存储在客户端本地,减少服务器端的存储的压力,安全性不好,客户端 可以清除cookie
Session:将数据存储到服务器端,安全性相对好,增加服务器的压力

首先,我们讲讲Cookie:

1.客户端Cookie的生成与发送:

1.Cookie的创建: 最好不要存中文~
Cookie cookie = new Cookie(String cookieName,String cookieValue);

2.设置Cookie在客户端的持久化时间

cookie.setMaxAge(int seconds); ---时间秒

3.设置Cookie的携带路径:

cookie.setPath(String path);

如果没有设置Cookie的携带路径 那么系统默认会在该web文件下的所有路径都携带cookie信息

4.向客户端发送cookie:

response.addCookie(Cookie cookie);

注:Cookie是以数组的形式发送的

5.删除客户端的cookie:
把Cookie的持续时间设置为0即可:

cookie.setMaxAge(0);

完整代码:

package com.gzx;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;

@WebServlet("/addCookieServlet")
public class AddCookieServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Cookie cookie = new Cookie("name","zhangsan");//生成cookie
		cookie.setMaxAge(6*60);//6分钟
		cookie.setPath("/*");//对所有路径生效
		response.addCookie(cookie);//向服务端发送Cookie 注意:发送的cookie是一个数组
	}

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

}



2.服务端获取Cookie和操作Cookie:

1.通过request来获取Cookie:

Cookie[] cookies = request.getCookies();

2.遍历Cookie数组,通过Cookie的名称获得我们想要的Cookie

for(Cookie cookie : cookies){
	if(cookie.getName().equal(cookieName)){
		String cookieValue = cookie.getValue();
	}
}

完整代码:

package com.gzx;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;

@WebServlet("/showCookieServlet")
public class ShowCookieServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    Cookie[] cookies = request.getCookies();
	    
	    for(Cookie cookie:cookies) {
	    	System.out.println(cookie.getName()+cookie.getValue());
	    	
	    }
	    
	}

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

}

**

接下来,我们谈谈Session:

**

Session技术是将数据存储在服务器端的技术,为每个客户端都创建一块内存空间存储客户的数据,但客户端需要每次都携带一个标识ID去服务器中寻找属于自己的内存空间。所以说Session的实现是基于Cookie,Session需要借助于Cookie存储客户的唯一性标识JSESSIONID。

1.操作Session对象

HttpSession session = request.getSession();
        //生成一个session,并对session进行赋值:
        session.setAttribute("String name","Object obj");
        //例如:
        session.setAttribute("name","张三");
        //在另外的页面,获取session:
        session.getAttribute(String name);
        //例如:以下变量name中的值就是"张三"
   	    String name = session.getAttribute("name");
   	    //移除session中的值:
   	    session.removeAttribute(String name);

2.Session的生命周期:
从不操作服务器端的资源开始计时,Session的默认有效时间为:30分钟,当然我们可以通过配置文件来修改session的有效时间

3.修改session的有效时间:

<session-config>
        <session-timeout>50</session-timeout>
</session-config>

在web工程中修改单个web.xml中的session-timeout只会对当前web工程有效,如果想对所有工程有效,可以到工作空间下找到Server工程,修改内部Tomcat中的web.xml文件,如下:
会话技术Cookie&Session
会话技术Cookie&Session
会话技术Cookie&Session找到session-config 即可进行配置。

4.手动销毁Session
session可以通过有效时间过后自动销毁,当然也可以进行手动销毁,代码如下:

 session.invalidate();

注意:关闭浏览器,Session是不会自动销毁的,需要我们手动销毁或者等待有效时间过期