The mental journey of learning the front-end web ------ session technology Cookie and Session


insert image description here

Conversational Technology Concepts

  Session : A session contains multiple requests and responses.

  One session : The browser sends a request to the server resource for the first time, and the session is established until one party disconnects.

  Function : Share data between multiple requests within the scope of a session

  Ways :
    1. Client-side session technology: Cookie
    2. Server-side session technology: Session

Cookie

  Concept : client-side session technology, saving data to the client

Quick start:

* 使用步骤:
	1. 创建Cookie对象,绑定数据
		* new Cookie(String name, String value) 
	2. 发送Cookie对象
		* response.addCookie(Cookie cookie) 
	3. 获取Cookie,拿到数据
		* Cookie[]  request.getCookies()  

  Implementation principle : based on response header set-cookie and request header cookie implementation

Cookie details:

1. 一次可不可以发送多个cookie?
	* 可以
	* 可以创建多个Cookie对象,使用response调用多次addCookie方法发送cookie即可。
2. cookie在浏览器中保存多长时间?
	1. 默认情况下,当浏览器关闭后,Cookie数据被销毁
	2. 持久化存储:
		* setMaxAge(int seconds)
			a. 正数:将Cookie数据写到硬盘的文件中。持久化存储。并指定cookie存活时间,时间到后,cookie文件自动失效
			b. 负数:默认值
			c. 零:删除cookie信息
3. cookie能不能存中文?
	* 在tomcat 8 之前 cookie中不能直接存储中文数据。
		* 需要将中文数据转码---一般采用URL编码(%E3)
	* 在tomcat 8 之后,cookie支持中文数据。特殊字符还是不支持,建议使用URL编码存储,URL解码解析
4. cookie共享问题?
	1. 假设在一个tomcat服务器中,部署了多个web项目,那么在这些web项目中cookie能不能共享?
		* 默认情况下cookie不能共享
		* setPath(String path):设置cookie的获取范围。默认情况下,设置当前的虚拟目录
			* 如果要共享,则可以将path设置为"/"

Features and functions of cookies:

  • Features:
    1. Cookies store data in the client browser
    2. The browser has a limit on the size of a single cookie (4kb) and a limit on the total number of cookies under the same domain name (20)
  • Functions:
    1. Cookies are generally used to store a small amount of less sensitive data
    2. Complete the server's identification of the client without logging in

Case: Remember last visit time

1. 需求:
	a. 访问一个Servlet,如果是第一次访问,则提示:您好,欢迎您首次访问。
	b. 如果不是第一次访问,则提示:欢迎回来,您上次访问时间为:显示时间字符串

2. 分析:
	a. 可以采用Cookie来完成
	b. 在服务器中的Servlet判断是否有一个名为lastTime的cookie
		a. 有:不是第一次访问
			1. 响应数据:欢迎回来,您上次访问时间为:201861011:50:20
			2. 写回Cookie:lastTime=201861011:50:01
		b. 没有:是第一次访问
			1. 响应数据:您好,欢迎您首次访问
			2. 写回Cookie:lastTime=201861011:50:01

Session

  Concept : Server-side session technology, which shares data among multiple requests in a session, and saves data in server-side objects. HttpSession

Quick start:

1. 获取HttpSession对象:
	HttpSession session = request.getSession();
2. 使用HttpSession对象:
	Object getAttribute(String name)  
	void setAttribute(String name, Object value)
	void removeAttribute(String name)  

  Principle : The implementation of Session is dependent on Cookie.

Session details:

1. When the client is closed, the server does not close, is the session obtained twice the same?
  默认情况下。不是。

  如果需要相同,则可以创建Cookie,键为JSESSIONID,设置最大存活时间,让cookie持久化保存。
  ​Cookie c = new Cookie(“JSESSIONID”,session.getId());
  c.setMaxAge(60*60);
  response.addCookie( c );

2. The client is not closed. After the server is closed, is the session obtained twice the same?
  不是同一个,但是要确保数据不丢失。tomcat自动完成以下工作

  • ​Session passivation:
      Serialize the session object to the hard disk before the server shuts down normally
  • ​Session activation:
      After the server is started, the session file can be converted into a session object in memory.

3. When is the session destroyed?
  1. The server shuts down
  2. The session object calls invalidate().
  3. The default session expiration time is 30 minutes

  Optional configuration modification:

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

Features of session:

  • Session is used to store the data of multiple requests for a session, which is stored on the server side
  • Sessions can store data of any type and size

The difference between session and cookie:

	1. session存储数据在服务器端,Cookie在客户端
	2. session没有数据大小限制,Cookie有
	3. session数据安全,Cookie相对于不安全

insert image description here

Guess you like

Origin blog.csdn.net/S_yyuan/article/details/122602610