JavaWeb----Cookie&Session

## technical sessions

1, Session: a session comprises multiple requests and responses.

  * First session: The first browser sends a request to the server resources, session establishment, until one party off so far.

2, function: the range between multiple requests of the session again, the shared data

3 ways:

  1, client session technology: Cookie

  2, the server-side session techniques: Session  

 

##  Cookie

1, concepts: the data stored in the client, the client session technology

2, Getting Started:

  * Steps for usage:

    1, create a Cookie object data binding

    *  new  Cookie(String  name,  String  value)

    2, transmission Cookie object

    *  response.addCookie(Cookie  cookie)

    3, get Cookie, get the data

    *  Cookie[]  request.getCookies()

package com.Cookie.demo01;

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;

/**
 * Cookie快速入门
 */
@WebServlet("/CookieDemo01")
public class CookieDemo01 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、创建Cookie对象
        Cookie cookie = new Cookie("msg","hello");
        //2、发送Cookie
        response.addCookie(cookie);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
package com.Cookie.demo01;

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;

/**
 * 获取Cookie
 */
@WebServlet("/CookieDemo02")
public class CookieDemo02 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //3、获取Cookie
        Cookie[] cs = request.getCookies();
        //获取数据,遍历Cookies
        if (cs != null){
            for (Cookie c : cs) {
                String name = c.getName();
                String value = c.getValue();
                System.out.println(name+":"+value);
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

3. The principle

  * Based on the response, and set-cookie header cookie request header to achieve

 

 

 

 

4, cookie details

  1, once Can send multiple cookie?

    * Can

    * Cookie can create multiple objects, many times addCookie method can be used to send cookie response calls.

  2, cookie stored in the browser for how long?

    1, by default, when the browser is closed, Cookie data is destroyed

    2, persistent storage:

    *  setMaxAge(int  seconds)

      1, a positive number: Cookie data is written to the hard disk file. Persistent storage. cookie survival time.

      2, negative: Default

      3. Zero: Remove cookie information

package com.Cookie.demo01;

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;

/**
 * Cookie快速入门
 */
@WebServlet("/CookieDemo01")
public class CookieDemo01 extends HttpServlet {
    @Override
    protected voidthe doPost (the HttpServletRequest Request, the HttpServletResponse Response) throws ServletException, IOException {
         // . 1, create a Cookie object 
        Cookie cookie = new new Cookie ( "MSG", "Hello" );
         // set the cookie survival
         // survival negative Default 30 seconds (close the browser gone) 0 delete cookie information 
        cookie.setMaxAge (30 );
         // 2, transmits cookies 
        response.addCookie (cookie); 
    } 

    @Override 
    protected  void the doGet (the HttpServletRequest Request, the HttpServletResponse Response) throws ServletException, IOException {
         this.doPost(request, response);
    }
}
package com.Cookie.demo01;

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;

/**
 * 获取Cookie
 */
@WebServlet("/CookieDemo02")
public class CookieDemo02 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //3、获取Cookie
        Cookie[] cs = request.getCookies();
        //获取数据,遍历Cookies
        if (cs != null){
            for (Cookie c : cs) {
                String name = c.getName();
                String value = c.getValue();
                System.out.println(name+":"+value);
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

 

  3, cookie can not save Chinese?

    * Before tmocat8 cookie can not be stored directly in the Chinese data

      * Chinese data needs to be transcoded ---- ships URL encoded (% E3)

    * After Tomcat8 cookie data can be stored in Chinese

  4, cookie acquire what extent?

    1, assuming a tmocat server, deploy a number of web projects, the cookie can not share in these web project?

      * Cookie by default can not be shared

      * SetPath (String path): set a cookie acquisition range. By default, set the current virtual directory

      * If you want to share, you can set the path to "/"

    2, between different Tomcat server cookie sharing?

      * SetDomain (String path): If a domain name identical, then the cookie between multiple servers to share the available

        * SetDomain ( ". Baidu, com"), and then tieba.baidu.com news.baidu.com in the cookie can be shared

5, cookie characteristics and functions

  1, cookie data is stored in the client browser

  2, the browser is limited (4kb) for a single cookie size and there is a limit on the total number of cookie under the same domain name (20)

 

  * Role:

   1, cookie is generally less sensitive for storing small amounts of data

   2, without logging in, completing the server to the client's identity

Guess you like

Origin www.cnblogs.com/21seu-ftj/p/12563456.html