The four scopes of JSP are page, request, session, and application. You can use setattribute ("", ""); getattribute ("", ""); When page is used, name pagecontext. setattribute ("", ""); it can only be valid on the same page
Request. The request range refers to the failure of the attribute when a JSP page sends a request to another JSP page. You can use setattribute () and getattribute () in the request object to set the request range ()
The range of a session is the duration of the user's connection to the server. However, if the session is disconnected from the server, this attribute is invalid. As long as the data is stored in the session object, the data range is session
The scope of application is to execute services at the beginning of the server. By the time the server is shut down, the maximum application range and maximum stay time are reached, therefore, pay special attention when using the service. Otherwise, the server load may become heavier and heavier. As long as the data is stored in the Application object, the scope of the data is the application
Application and session are relatively simple. Here we mainly describe the scope of application and page.
Application: global scope of action. The whole application is shared, that is, the same webapp in the deployment file is shared, and the lifecycle is: The application starts to stop.
Session: Session scope. When a user accesses a session for the first time, a new session is generated, and the server will be able to remember the session status. Life cycle: the session times out, or the server forces the session to fail.
Request: Request scope, which is a client request.
Page: a JSP page.
The scope of the above is getting smaller and smaller, and the lifecycle of requests and pages is short. The difference between them is that a request can contain multiple page pages (include, forward, and filter ). A simple example:
Jsp1.jsp
<JSP: usebean id = "beanid" class = "XXX. XXX. beanclass" Scope = "request"/>
<JSP: Include page = "jsp2.jsp"/>
Jsp2.jsp
<JSP: getproperty name = "beanid" property = "sample"/>
Run jsp1.jsp to display the data normally, because the scope of function is request, which is equivalent to calling request. setattribute () method. The jsp2 page passes the request. getattribute to obtain the bean. if you change the request to page, nullpointereffectioin will be thrown.
A request can span several JSP (include and forword) and Servlet (such as filter ). however, there is a difference between <JSP: Include> and <% @ include %>. if the preceding <JSP: Include page = "jsp2.jsp"/> is changed to <% @ include file = "jsp2.jsp" %>, even if the scope is page, <% @ include
%> Is included during compilation. <JSP: Include> is included at runtime. The former is equivalent to a macro. It is replaced during compilation. The latter is equivalent to a function and is returned at runtime.