First of all, it is necessary to declare that the so-called "scope" is the "scope of information sharing", that is to say, the scope within which an information can be effective. The scopes of the four JSP built-in objects are: application, session, request, and page.
Recommended course: Java Tutorial.
The four major scopes of jsp are:
1, application function Domain
If you put a variable in application, it means that its scope is application and its effective scope is the entire application. The entire application refers to the start of the application to the end of the application. We don't say "from server startup to server shutdown" because a server may deploy multiple applications. Of course, if you shut down the server, all the applications on it will be shut down. Variables in the application scope have the longest survival time. If they are not manually deleted, they can always be used.
Object getAttribute(String name) //Get information from application;
void setAttribute(String name, Object value) //Set information to application scope.
2. Session scope
If you put a variable in the session, it means that its scope is the session and its valid scope is the current session. The so-called current session refers to the process from when the user opens the browser to when the user closes the browser.
Object HttpSession.getAttribute(String name) //Get information from session.
void HttpSession.setAttribute(String name,Object value) //Save information to the session.
HttpSessionHttpServletRequest.getSessio() //Get the session object where the current request is located.
session The session is considered started when the browser makes the first HTTP request. But it is difficult to judge the end, because the server will not be 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() method of HttpSession:
void setMaxInactiveInterval(int interval)
If you want to actively end the session, for example, the user When clicking the "Logout" button, you can use the invalidate() method of HttpSession to force the end of the current session: void invalidate()
The time when the Session is deleted is:
1) Session Timeout: Timeout refers to the fact that the server does not receive the request from the client corresponding to the session for a certain period of time, and this time exceeds the maximum session timeout set by the server.
2) The program calls HttpSession.invalidate()
3) The server is shut down or the service is stopped
Note:
1) Access the static of *.html Because resources will not be compiled into Servlets, session issues are not involved.
2) When the JSP page does not explicitly prohibit sessions, the server will automatically create a session for it when opening the browser to request the jsp for the first time, and give it a sessionID, which will be sent to the client. terminal browser.
3) Since session consumes memory resources, if you do not plan to use session, you should close it in all JSPs.
4) The session will not be deleted when the browser is closed and can only be deleted through the above three methods.
Where is the session stored? -----------In memory on the server side.
3. Request scope
The variables in request can span the two pages before and after forward. But whenever the page is refreshed, they are recalculated.
Request forwarding: servlet.getRequestDispatcher("new.jsp").forward(req,resp);
Note:
1. Forwarding is a server behavior, and re- Targeting is client behavior.
2. No matter how it is forwarded on the server, the address of the original Servlet is still displayed in the browser address bar.
4. Page scope
The scope of the page object is limited to the current page requested by the user
The life cycles of request and page are short-lived The difference between them: 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!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools