Saving Value Operation of Request and Session

  • 2021-10-24 22:42:48
  • OfStack

Saving Value of Request and Session

Storage value of request field:

request can be directly entered by set


request.setAttribute("request", value);

There are two ways to get the value of request field:


"${request}"
------------
"${requestScope.request}"

Or you want to get some 1 valueBean object in the key value


"${request.name}"

Storage value of session field:

session needs to get session under get1 before set can enter key and value values


request.getSession().setAttribute("session", value);
----------------------------------------------------
HttpSession session = request.getSession();
session.setAttribute("session", value);

Value of session field:


"${sessionScope.session}"

Differences between Request and Session

To put it simply, the biggest difference between request objects and session objects is the life cycle.

request

The request range is one smaller, just one request.

The lifecycle of an request object is one request to a client (to be exact, a browser application). When the request is completed, the contents of request will also be released.

Simply put, it is an operation you have on the page. request. getParameter () is to get parameters from url and form in the previous page.

However, if an request involves multiple classes and parameters are taken later, you can use request. setAttribute () and request. getAttribute ().

But when the result is output, request ends.

session

session can span many pages.

The life cycle of session is also aimed at one client, but it is in the session cycle set by others (1 is generally 20-30 minutes), and the content in session will exist directly. Even if the client browser session is closed, it will not be released immediately.

It can be understood that there are multiple requests from the client with one IE window.

Parameters can be passed between them, for example, users of many websites log in.

Comparison

request occupies less resources and has high security, but it lacks continuity relatively.

session is relatively resource-intensive and slightly less secure, but it can implement, for example, session tracing techniques.

If you can use request, try to use request because the consumption of resources is still important relative to the server.

While request can no longer be delivered to the next 1 page during page delivery, sesison is not, that is, request is limited to 2 adjacent pages

Every time a link on a web page is pressed, it is a new request. When the server returns an response to the browser, request ends, and the object stored in request does not exist.

But application-server will open a new session to you when you connect to the server with a viewer, and session will only be destroyed when the connection times out or the browser is closed.

Therefore, the scope of action is different, and session can track the user's status.

session is equivalent to a global variable of one client,

For example, session. setAttribute ("aaa") = "ComputerA" is set when the A machine and the server access for the first time. Then the value of session. getAttribute ("aaa") that can be taken on any one page that the A machine accesses and continues to access is ComputerA;

request is a local variable for a one-time access,

The life cycle is only one request. Therefore, the variables of login should be placed in session


Related articles: