I. Overview of JSP built-in objects
Definition:Member variables that can be used in JSP page scripts (Java program fragments and Java expressions) without declaration. In JSP technology, several internal JSP objects have been defined. Applying these internal objects can implement many important functions.
JSP has the following nine basic built-in components (corresponding to the six internal components of ASP ):
Built-in object |
Representative content |
Range |
Request |
Request to trigger a service call |
Request |
Response |
Response to the request |
Page |
Session |
Session created for the requested customerObject |
Session |
Application |
Slave ServletServlet obtained from the configuration objectContext (for example, in getservletconfig (), Getcontext ()) |
Application |
Out |
The object that writes content to the output stream. |
Page |
Pagecontext |
This JSPPage Context |
Page |
Page |
Instance that processes the current request class on this page |
Page |
Config |
This JSPServletconfig |
Page |
Exception |
JSPPage running exception |
Page |
Ii. Four Scopes
The third column in the table specifies the scope of each object, that is, the scope. First, we should declare that the so-called "Scope" is the "scope of information sharing", that is, the scope of a piece of information that can be effective. The four built-in JSP objects have the following scopes: application, session, request, and page. The JSP built-in object scope table is as follows:
Name |
Scope |
Application |
Valid in all applications |
Session |
Valid in current session |
Request |
Valid in current request |
Page |
Valid on current page |
The basic unit of web interaction is HTTP requests. Each user's process from entering the website to leaving the website is called an http session. When a server is running, multiple users access it, that is, multiple HTTP sessions. The scope is explained as follows.
- Application ScopeThe application scope refers to the entire period from server startup to server shutdown. The information set in this scope can be used by all applications. The information transfer in the application scope is implemented through servletcontext, which provides the following methods: Object getattribute (string name) // obtain information from the application; void setattribute (string name, object value) // set information to the application scope.
- The session scope is easy to understand. The same browser accesses the Server Multiple times and transmits information between these accesses, which is the embodiment of the session scope. The session is implemented through the httpsession interface. The main method provided is as follows. Object httpsession. getattribute (string name) // obtain information from the session. Void httpsession. setattribute (string name, object Value) // Save the information to the session. Httpsession httpservletrequest. getsessio () // gets the object of the session in which the current request is located. The start time of the session is easier to judge. The session starts when the browser sends the first HTTP request. However, the end time is difficult to judge, because the browser does not notify the server when it is closed, so it can only be judged through the following method: if the client does not respond within a certain period of time, the session is deemed to have ended. The default value of Tomcat 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, when you click "logout", you can use the invalidate () method of httpsession to forcibly end the current session: void invalidate ()
The processing of an HTTP request in the request scope may require the cooperation of multiple Servlets, which can transmit information in some way, but this information is invalid after the request ends. Servlet Information sharing is implemented through the httpservletrequest interface. Void setattribute (string name, object Value) // Save the object value with name in the request scope. Object getattribute (string name) // obtain the specified name information from the request scope. The first parameter of the doget () and dopost () methods in JSP is the httpservletrequest object. Information can be transmitted using the setattribute () method of this object. After setting the information, how can I transmit the information to other servlets? This requires the forward () method of the requestdispatcher interface to forward requests to other servlets. Requestdispatcher servletcontext. getrequestdispatcher (string path) // get the dispatcher for forwarding. path is the target servlet for forwarding. Void requestdispatcher. forward (servletrequest request, servletresponse response) // forward the request and response. Therefore, you only need to set the corresponding attributes in the current servlet through the setattribute () method, and then use forward () method. Finally, you can use the getattribute () method in the servlet to transfer information. PHP programmers may not quite understand this section. Because PHP does not have the forwarding concept, a request can only be processed by one PHP file, so PHP does not have the concept of request scope. The servlet is different. requests can be forwarded randomly in the application. Therefore, the request scope is used to transmit information between different servlets.Pay attention to the following two points:1. Forwarding is not redirected, but is performed inside the web application. PHP supports redirection but does not forward data. 2. Forwarding is transparent to the browser. That is to say, no matter how it is forwarded on the server, the address bar of the browser still displays the original servlet address.
Page ScopeThe scope of the page object is limited to the current page requested by the user. Reference to the page object will be released after the response is returned to the client, or released after the request is forwarded to other places. References to page objects are usually stored in pagecontext objects. The scope of the above introduction is getting smaller and smaller. 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 ). In order to make it easier for everyone to understand the scope of application, session, request, and page objects, we provide two programs for detailed description.
[Program 1] page01.jsp
<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8" <br/> pageencoding = "UTF-8" %> <br/> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd"> <br/> <JSP: usebean id = "pagevar" Scope = "page" class = "Java. lang. stringbuffer "/> <br/> <JSP: usebean id =" requestvar "Scope =" request "class =" Java. lang. stringbuffer "/> <br/> <JSP: usebean id =" sessionvar "Scope =" session "class =" Java. lang. stringbuffer "/> <br/> <JSP: usebean id =" applicationvar "Scope =" application "class =" Java. lang. stringbuffer "/> <br/> <HTML> <br/> <pead> <br/> <meta http-equiv =" Content-Type "content =" text/html; charset = UTF-8 "> <br/> <title> JSP built-in object scope </title> <br/> </pead> <br/> <body> <br/> <% <br/> pagevar. append ("page01"); <br/> requestvar. append ("page01"); <br/> sessionvar. append ("page01"); <br/> applicationvar. append ("page01"); <br/>%> <br/> <JSP: forward page = "page02.jsp"/> <br/> </body> <br/> </ptml>
[Program 2] page02.jsp
<% @ Page Language = "Java" Import = "Java. util. * "contenttype =" text/html; charset = UTF-8 "<br/> pageencoding =" UTF-8 "%> <br/> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd"> <br/> <JSP: usebean id = "pagevar" Scope = "page" class = "Java. lang. stringbuffer "/> <br/> <JSP: usebean id =" requestvar "Scope =" request "class =" Java. lang. stringbuffer "/> <br/> <JSP: usebean id =" sessionvar "Scope =" session "class =" Java. lang. stringbuffer "/> <br/> <JSP: usebean id =" applicationvar "Scope =" application "class =" Java. lang. stringbuffer "/> <br/> <HTML> <br/> <pead> <br/> <meta http-equiv =" Content-Type "content =" text/html; charset = UTF-8 "> <br/> <title> JSP built-in object scope </title> <br/> </pead> <br/> <body> <br/> <% <br/> pagevar. append ("page02"); <br/> requestvar. append ("page02"); <br/> sessionvar. append ("page02"); <br/> applicationvar. append ("page02"); <br/>%> <br/> page = <% = pagevar. tostring () %> <br/> request = <% = requestvar. tostring () %> <br/> session = <% = sessionvar. tostring () %> <br/> application = <% = applicationvar. tostring () %> <br/> </body> <br/> </ptml>
Test steps and result analysis:
1. The result of directly running program 1 is: (figure 1)
We can see that the value of the page scope is page02, indicating that it only takes effect on the current page, that is, the page2 page to jump to. The request scope is valid in the current request, so its value is the sum of program 1 and jump to program 2; the scope of session is the current session, so its value is also the sum of program 1 and jump to program 2; and application is valid for all applications, that is, as long as the application is applied, the value in program 1 must be superimposed with the value in program 2;
2. Do not close the browser running program 1 and directly run program 2. The result is: (figure 2)
Compared with the result in Figure 1, we found that the page scope has not changed, and its value is only the value in program 2. The request scope is only applied to the current request, so the value of Program 2 prevails, changed to page02; the session scope is the current Session. Because the browser running program 1 is maintained, it indicates that it is still in the same session. Therefore, a page02 should be superimposed on the previous one; the application is effective for all applications, that is, as long as the application is superimposed, that is, the page02 of Program 2 is superimposed on the basis of the previous;
3. Close the browsers that run programs 1 and 2 in the previous two steps, but do not close the server and re-run program 2. The result is as follows:
Compared with the previous results, we found that the page scope remains unchanged. Its value is only the value in the page where Program 2 is located. The request scope is only used for the current request, therefore, the value of Program 2 is changed to page02. The session scope is the current Session. Because the browser that runs the program in the first two steps is closed, the previous sessions are all over, therefore, the value is restored to the value page02 in the current program 2, and the application is valid for all applications, that is, as long as the application is cleared before the server is restarted, it must be superimposed, that is, add a page02 of Program 2 on the basis of the previous one;