当前位置:网站首页>Cookie session explanation

Cookie session explanation

2022-06-23 05:08:00 Don't want to be a programmer

1、 conversation

conversation : The user opens a browser , Click a lot of hyperlinks , More visits web resources , Close the browser , This process can be called conversation .
Stateful conversation : A classmate came to the classroom , Come back to the classroom next time , We will know this classmate , I have been to the classroom , It's called stateful conversation .
How can you prove that you are a student ?

  • Student ID card : The school will give you a student card
  • School Registration : The school signs you've been here

How can a website prove that you've been here ?
client Server side

  1. The server sends a letter to the client , The next time the client accesses the server, it is OK to bring a letter ;cookie
  2. The server registers that you have been here , Next time I come, I will match you ;session

2、 Two techniques for saving a session

cookie: Client technology ( Respond to 、 request )

session: Server technology , Using this technology , Can save user session information ? We can put information or data in Session in !

Common practice : After the website logs in , Don't log in next time , On the second visit, you can go up directly .

3、Cookie

  1. Get... From the request cookie Information
  2. The server corresponding to the client cookie
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
//  The server tells you , When you came , Encapsulate this time into a letter , You bring it next time   I knew you were here 

        req.setCharacterEncoding("gbk");
        resp.setCharacterEncoding("gbk");

        PrintWriter out = resp.getWriter();

// Cookie, The server gets it from the client 
        Cookie[] cookies = req.getCookies();// Here we return the array , explain Cookie There could be multiple 

//  Judge Cookie Whether there is 
        if (cookies!=null){
    
//  What if it exists 
            out.write(" The time of your last visit was :");
            for (int i = 0; i < cookies.length; i++) {
    
                Cookie cookie = cookies[i];
//  obtain cookie Name 
                if (cookie.getName().equals("lastLoginTime")){
    
//  obtain cookie The value in 
                    long lastLoginTime = Long.parseLong(cookie.getValue());
                    Date date = new Date(lastLoginTime);
                    out.write(date.toLocaleString());
                }
            }
        }else {
    
            out.write(" This is my first visit to this site ");
        }

        Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis()+"");
        cookie.setMaxAge(24*60*60);// Valid for one day 
        resp.addCookie(cookie);
    }
 req.getCookies();// obtain cookie
 cookie.getName();// obtain cookie Of key
 cookie.getValue();// obtain cookie Medium value
 new Cookie("lastLoginTime", System.currentTimeMillis()+"");// Create a new one cookie
 cookie.setMaxAge(**);//  Set up cookie The period of validity 
 resp.addCookie();// The response gives the client a cookie

Cookie: It is usually saved in the local user directory appdata;
A website cookie Whether there is an upper limit ! Talk about the details

  • One Cookie Only one message can be saved
  • One web Sites can send multiple cookie, At most 20 individual cookie;
  • Cookie There's a limit to the size 4kb;
  • 300 individual cookie Browser limit

Delete cookie;

  • Do not set the validity period , Close the browser , Automatic failure ;
  • Set the validity period to 0;
//  Create a cookie, The name must be the same as the one to be deleted 
        Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis()+"");
// cookie The validity period is set to 0, Expire immediately 
        cookie.setMaxAge(0);
        resp.addCookie(cookie);

Encoding and decoding :

URLDecoder.decode(cookie.getValue(),"UTF-8");
URLEncoder.encode(" chinese Cookie","utf-8");

4、Session( a key

What is? Session:

  • The server will give every user ( browser ) Create a Session object ,
  • One Session Monopolize a browser , As long as the browser is not turned off , This Session There is a
  • After the user logs in , It can access the whole website !– Save user information ; Save shopping cart information ……

session and Cookie The difference between :

  • cookie It is to write the user's data to the user's browser , Browser save ( You can save multiple )
  • Session Write the user's data to the user's exclusive Session in , Server side save ( Keep important information , Reduce the waste of server resources )
  • session Objects are created by services ;

Use scenarios :

  • Save the information of a login user ;
  • Shopping cart information ;
  • Data often used throughout the website , We keep it in Session in

Manual logout

//  Manual logout session
        session.invalidate();

Session expires automatically

<!--  Set up session Default expiration time -->
    <session-config>
<!-- 1 Minutes later session Automatic failure -->
        <session-timeout>1</session-timeout>
    </session-config>
原网站

版权声明
本文为[Don't want to be a programmer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230136591991.html