What are the four major scopes of jsp

anonymity
Release: 2019-05-29 17:00:08
Original
12641 people have browsed it

JSP built-in object scope

What are the four major scopes of jsp

application: valid in all applications, that is, as long as the website is running, this scope is valid, this refers to the program working process.

session: valid in the current session, that is, starting from the browser accessing the server, this refers to an access process of the user, that is, from the beginning to the end of the session

request: in the current session Valid in the request, responsible for information sharing between multiple servlets, that is, putting the information into the request, it is valid during the entire request stage, that is, the user can obtain this data at this stage of access

page : Valid on the current page, that is, valid on a jsp page.

First of all, we must declare that the so-called "scope" is the "scope of information sharing", that is to say, the scope within which a piece of information can be effective.

The most basic unit of Web interaction is HTTP request. The process from entering the website to leaving the website for each user is called an HTTP session. During the operation of a server, multiple users will access it, which is multiple HTTP sessions. Scope is explained below.

application: The period from when the server starts to when it stops.

session: The period from the beginning to the end of the HTTP session.

request: The period from the beginning to the end of the HTTP request.

page: The time from opening to closing of the current page.

1. application scope

The application scope is the entire period from startup to shutdown of the server. The information set in this scope can be used by all applications. Information transfer on the application scope is implemented through ServletContext, and the main methods it provides are as follows:

Object getAttribute (String name): Get information from the application.

void setAttribute(String name, Object value): Set information to the application scope.

2. Session scope

The session scope is easier to understand. The same browser visits the server multiple times and transfers information between these multiple visits. It is the embodiment of session scope. Session is implemented through the HttpSession interface, and the main methods it provides are as follows.

Object HttpSession.getAttribute(String name): Get information from session.

void HttpSession.setAttribute(String name, Object value): Save information to the session.

HttpSession HttpServletRequest.getSession(): Get the session object where the current request is located.

The start time of the session is relatively easy to judge. It is considered that the session starts when the browser sends the first HTTP request. But the end time is difficult to judge, because the server is not notified when the browser is closed, so it can only be judged by the following method: if the client does not respond within a certain period of time, the session is considered to be over. Tomcat's default value is 120 minutes, but this value can also be set through the setMaxInactiveInterval(int interval) method of HttpSession. If you want to actively end the session, such as when the user clicks the "Logout" button, you can use HttpSession's invalidate() Method used to forcefully end the current session.

3. Request scope

The processing of an HTTP request may require the cooperation of multiple Servlets, and information can be transferred between these Servlets in some way. But this information is invalid after the request is completed.

Information sharing between Servlets is achieved through two methods of the HttpServletRequest interface.

void setAttribute(String name, Object value): Save the object value to the request scope with name as the name.

Object getAttribute(String name): Get the information of the specified name from the request scope.

The first parameter of the doGet() and doPost() methods in JSP is the HttpServletRequest object. Use the setAttribute() method of this object to transfer information.

So after setting the information, how do you pass the information to other Servlets? This requires using the forward() method of the RequestDispatcher interface, through which the request is forwarded to other Servlets.

RequestDispatcher ServletContext.getRequestDispatcher(String path): Get the Dispatcher for forwarding. path is the destination Servlet for forwarding.

void RequestDispatcher.forward(ServletRequest request, ServletResponse response): Forward request and response.

Therefore, you only need to set the corresponding attributes through the setAttribute() method in the current Servlet, then use the forward() method to jump, and finally use the getAttribute() method in the jumped Servlet. Information transfer can be achieved.

PHP programmers may not understand this paragraph very well, because there is no concept of forwarding in PHP, and a request can only be processed by one PHP file, so there is no concept of request scope in PHP. Servlets are different. Requests can be forwarded arbitrarily in the application, so the request scope is used to transfer information between different Servlets.

Note two points:

Forwarding is not redirection, forwarding is performed within the web application. PHP supports redirects but not forwarding.

Forwarding is transparent to the browser, that is to say, no matter how it is forwarded on the server, the address of the original Servlet is still displayed in the browser address bar. The redirected browser address changes.

4. Page Scope

The scope of the page object is limited to the current page requested by the user. The reference to the page object will be retrieved after the response is returned to the client. Released, or released after the request has been forwarded elsewhere. A reference to the page object is usually stored in the pageContext object.

The scope of the above introduction is getting smaller and smaller. The life cycles of request and page are short-lived. The difference between them is: a request can contain multiple pages (include, forward and filter).

The above is the detailed content of What are the four major scopes of jsp. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
jsp
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template