search
HomeJavajavaTutorialWhat are the four major scopes of jsp

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.

What are the four major scopes of jsp

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!

Statement
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
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

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...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

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...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

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...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

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

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

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 default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

MantisBT

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

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools