Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the scope of jsp

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail what the scope of jsp is, Xiaobian thinks it is quite practical, so share it with you as a reference, I hope you can gain something after reading this article.

Application: valid in all applications, i.e., as long as the site is running, the scope is valid, which refers to the running process of the program.

session: valid in the current session, that is, from the browser to access the server, this refers to a user's access process, that is, the beginning of the session to the end

request: valid in the current request, responsible for information sharing among multiple servlets, that is, information is put into the request, valid throughout the request phase, that is, the user can obtain this data at this stage of access

Page: valid on the current page, i.e. valid on a jsp page.

First of all, it should be stated that the so-called "scope" is the "scope of information sharing", that is, how much information can be valid.

The most basic unit of Web interaction is the HTTP request. Each user from entering the website to leaving the website this process is called an HTTP session, a server running process will have multiple users access, that is, multiple HTTP sessions. The scope is explained as follows.

Application: The time between start and stop of the server.

Session: The time between the start and end of an HTTP session.

request: The time between the start and end of an HTTP request.

Page: The time between the opening and closing of the current page.

1. application scope

An application scope is the entire time between startup and shutdown of the server. Information set in this scope can be used by all applications. Information transfer on the application scope is implemented through ServletContext, which provides the following main methods:

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

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

2. session scope

Session scope is relatively easy to understand, the same browser to the server for multiple visits, in this multiple visits between the transfer of information, is the embodiment of session scope. Session is implemented through the HttpSession interface, which provides the following main methods:

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

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

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

The start of a session is easy to determine, it can be considered to start a session from the browser's first HTTP request. However, it is difficult to judge the end time, because the browser does not notify the server when it is closed, so it can only be judged by the following method: if the client does not respond within a certain period of time, it is considered that the session is 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 end the session actively, for example, when the user clicks the "logout" button, you can use the invalidate() method of HttpSession to force the current session to end.

3. request scope

The processing of an HTTP request may require multiple servlets to cooperate, and these servlets can pass information 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 as name in the request scope.

Object getAttribute (String name): Gets information about the specified name from the request scope.

The first parameter of the doGet() and doPost() methods in JSP is the HttpServletRequest object, and the setAttribute() method of this object can be used to pass information.

So after setting up the information, how do you pass it on to other servlets? This uses the forward() method of the RequestDispatcher interface, which forwards requests to other servlets.

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

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

Therefore, you only need to set the corresponding attribute in the current Servlet through the setAttribute() method, then use the forward() method to jump, and finally use the getAttribute() method to transfer information in the Servlet you jump to.

PHP programmers may not understand this paragraph well, because there is no concept of forwarding in PHP, a request can only be handled by a PHP file, so there is no concept of request scope in PHP. Servlets, on the other hand, are different. Requests can be forwarded anywhere in the application, so the request scope is used to pass information between different servlets.

Note two points:

Forwarding is not a redirection; forwarding occurs internally within the Web application. PHP supports redirection but no forwarding.

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

4. page scope

The scope of the page object is limited to the current page requested by the user, and references to the page object are released after the response is returned to the client, or after the request is forwarded elsewhere. References to page objects are typically stored in pageContext objects.

The scope of the above description is getting smaller and smaller. The life cycle of request and page is short. The difference between them is that a request can contain multiple pages (include, forward and filter).

About "what is the scope of jsp" this article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report