JSP built-in object scope
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!

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software