Introduction to Cookie session Technology

Conversation Technology:

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 among multiple requests within the scope of one session.

Method:

1:  Client session Technology: Cookie

2:  Session: session

Use steps:

1:  Create Cookie object and bind data.

new Cookie(String name,String value);

2:  Send Cookie object.

response.addCookie(Cookie cookie);

3:  Get cookies, get data

Cookie[] cs  =	request.getCookies();

Implementation principle:

Implementation based on response header set cookie and request header cookie

cookie details

 1:  Can I send more than one cookie at a time?

Yes, you can create multiple cookie objects and use the response call addCookie() to add multiple cookies.

2:  How long does the cookie stay in the browser?

1:  By default, when browsing is turned off, Cookie data is destroyed

2: Persistent storage:

setMaxAge(int seconds)

1:  Integer: write cookie data to the file on the hard disk, persist storage, cookie lifetime

2:  Negative: the default is that the browser closes the cookie and disappears

3:  Zero: delete cookie information

3: Can cookies store Chinese data?

   Chinese data cannot be stored in cookie s before tomcat8

   To solve the problem of transcoding Chinese data: generally URL encoding (% E3)

   After tomcat8, cookie s support Chinese data.

4: What is the scope of cookie acquisition?

 1:  If you deploy multiple web projects in a comcat server, can cookie s be shared in these web projects?

    Cookies cannot be shared by default

**Solution: cookie sharing among multiple web projects**

setPath(String path): set the scope of cookie acquisition. By default, the current virtual directory is set. If shared, the path can be set to "/"

2: cookie sharing between different tomcat servers?

setDomin(String path): if the primary domain name is the same, cookie s can be shared among multiple servers.

For example: setDomin(".baidu.com"), then cookie s in haha.baidu.com and hehe.baidu.com can be shared.

Features and functions of cookies:

 1:  Cookies store data in client browser

 2:  The browser has a limit on the size of a single cookie (4Kb) and the total number of cookies under the same domain name (20)

Effect:

1:  Generally used to store a small amount of less sensitive data.

2:  In the case of no login, complete the identification of the server to the client.

Cookie quickly create a small case:
1: The maven management jar package is used to import the jar package

 <!--javaEE-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!--jstl rely on-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>



2: Create Cookie and send Cookie to browser code:

package com.servlet01.cookie;


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 java.io.IOException;

/**
 * package_name:com.servlet01.cookie
 *
 * @author:Xu Yayuan Date:2020/3/3 20:27
 * Project Name: servlet project
 * Description:TODO
 * Version: 1.0
 **/
@WebServlet("/cookie01")
public class Cookie01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp );
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //Create Cookie object and bind data
        Cookie cookie1 = new Cookie("username","admin" );
        //Send cookie

        resp.addCookie(cookie1);
    }
}

The cookie information saved in the browser is as follows: set Cookie: username = admin

3: Obtain the cookie information and output the cookie information code in control mode as follows:

package com.servlet01.cookie;

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 java.io.IOException;

/**
 * package_name:com.servlet01.cookie
 *
 * @author:Xu Yayuan Date:2020/3/3 20:31
 * Project Name: servlet project
 * Description:TODO
 * Version: 1.0
 **/
@WebServlet("/cookie02")
public class Cookie02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp );
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //Get cookie s get data
       Cookie [] cs = req.getCookies();
       for (Cookie cookie : cs){
           String name = cookie.getName();
           String value = cookie.getValue();
           System.out.println("name:"+name+" "+"value:"+value);
       }
    }
}

4: Get the cookie information and output the information in control mode as follows:

79 original articles published, praised 6, and 2107 visitors
Private letter follow

Tags: Session JSP Java encoding

Posted on Mon, 09 Mar 2020 03:08:25 -0400 by myitanalyst