Home >Java >javaTutorial >What 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.
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!