27, cookie Detailed

A common question?
    1. Can you send more than one 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. Under default, when the browser is closed, Cookie data is destroyed
        2. persistent storage (think the browser is closed after Cookie also be able to continue saving):
            * setMaxAge (int seconds The)
                1. Positive: The Cookie write data hard disk file. Persistent storage. And specify the cookie survival time after time, cookie files automatically lapse
                2. Negative: Default --- the browser is closed, cookie disappears
                3. Zero: delete cookie information
    3. A cookie can not save Chinese?
        * Before tomcat 8 cookie can not be stored directly in the Chinese data.
            * Chinese data needs to be transcoded --- generally use URL encoding (% E3)
        * After tomcat 8, cookie support Chinese data. Special characters are not supported or recommended storage URL encoding, decoding URL parsing
    4. cookie sharing?
        (1) Assuming that in a tomcat 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 multiple items directly implement cookie, you can set the path to "/", which represents the entire root directory of the web server

        
        among different tomcat server cookie sharing (2). ?
            * SetDomain (String path): If you set an identical domain name, the cookie can be shared among multiple servers
                * setDomain ( "baidu.com.") , Then tieba.baidu.com and news.baidu.com in the cookie can be shared
two, .Cookie features and functions
    1. cookie data stored in the client browser
    2. the browser is limited (typically 4kb, different in different browsers) and the total amount at the same cookie has a domain size of a cookie for a single limit (typically 20, different browsers)

    * role:
        1. the cookie used to store a small amount of generally less sensitive data
        2. without login, identity server to the client to complete

Third, the case

Servlet code verification: CookieDemo1B.java

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;

@WebServlet("/CookieDemo1B")
public class CookieDemo1B extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie[] cs = request.getCookies();
        for (Cookie c : cs) {
            String name = c.getName();
            String value = c.getValue();
            System.out.println(""+name+":"+value);
        }
    }
}

 

Case 1, a send multiple cookie

How to test?

And receiving demo front joint test printout.

CookieDemo2.java

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;

@WebServlet("/CookieDemo2")
public class CookieDemo2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie cookie = new Cookie("msg", "lisi");
        Cookie cookie1 = new Cookie("msg2", "zhanshan");
        response.addCookie(cookie);
        response.addCookie(cookie1);
    }
}

Case II, verify the settings buffer

And receiving a preceding test joint test, run this servlet in a browser, then close the browser 30, to run in front of the verification of the servlet, 30S seconds after verification operation again servlet.

CookieDemo3.java

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;

@WebServlet("/CookieDemo3")
public class CookieDemo3 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、创建cookie对象
        Cookie cookie = new Cookie("msg", "lisi");
        //2、设置缓冲事件
        cookie.setMaxAge(30);//设置浏览器可以放到本地硬盘,缓冲30S后自动删除
        //3、发送cookie
        response.addCookie(cookie);
    }
}

Case III, verify that the same server, cross-application sharing

The establishment of two module, we referred to as A, B.

A plurality of modules are arranged in the article in accordance with the previous application to a web server deployment.

With Firefox browser module to perform A servlet. By other browsers to execute servlet B module.

A test module servlet code:

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;

@WebServlet("/CookieDemo4")
public class CookieDemo4 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、创建cookie对象
        Cookie cookie = new Cookie("msg", "lisi");
        //2、设置path路径为WEB服务齐全,这样就可以让其他项目访问
        cookie.setPath("/");
        //3、发送cookie
        response.addCookie(cookie);
    }
}

Test B servlet code module:


@WebServlet("/ServletDemo")
public class ServletDemo extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

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

 

Published 285 original articles · won praise 5 · views 10000 +

Guess you like

Origin blog.csdn.net/l0510402015/article/details/104776969