cookie and session learning the basics (b)

It for cookie and session also a beginner, the main purpose of writing this article is easy to understand their own session cookie and relevant content, some not so professional and comprehensive description, please bear with me.
In the last article cookie and session learning the basics (a) which introduced the concept of technical sessions, as well as some of the basics of cookie. This article is primarily concerned with some basic knowledge about the session.

First, the simple use of session

session is a server-side technology, you can create a session object exclusive to each user's browser to the server at runtime.
the session using the steps:

  • Gets the session object
  • Use session to store data
  • Using data acquisition session

Note the difference in the session and cookie use, cookie Cookie is the object, while session isHttpSessionObjects; cookie objects created by themselves, while the session object is to obtain the object (request.getSession()), Rather than create HttpSession object itself.

Here is a simple case of session, SessionTest1 store data, SessionTest2 get the data.

SessionTest1:

package com.study.cookieandsessionstudy;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "SessionTest1", value = "/SessionTest1")
public class SessionTest1 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获取session
        HttpSession httpSession = request.getSession();

        //存储数据
        httpSession.setAttribute("msg", "hello-world");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request, response);

    }
}

SessionTest2:

package com.study.cookieandsessionstudy;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "SessionTest2", value = "/SessionTest2")
public class SessionTest2 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获取session
        HttpSession httpSession = request.getSession();

        //获取数据
        Object object = httpSession.getAttribute("msg");
        System.out.println(object);

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request, response);

    }
}

Two, session of the principle of use

session is different from the cookie, when a user first accesses the server, the server creates exclusive session object for the user, and then will return to the object id session cookie form to the client. In view of this situation cookie has a special propertyJSESSIONIDTo store the session id.
Thus, when the case where the user holds the current browser to go to access the server, depending on the value of JSESSIONID pass over the cookie brought it to find its exclusive session, and then to complete the corresponding session.
Thus, the session implementation relies on the cookie .

Here Insert Picture Description
2019.12.22

Published 52 original articles · won praise 59 · views 6821

Guess you like

Origin blog.csdn.net/ataraxy_/article/details/103655753