Built-in objects (also known as hidden objects, 9 built-in objects): Free to use in script code and expressions without pre-declaration
The nine large built-in objects in the JSP are:
request--Request Object-type javax.servlet.servletrequest--scope--request
response--Response Object-type javax.servlet.srvletresponse--scope--page
pagecontext--Page Context Object--type javax.servlet.jsp.pagecontext--scope--page
session--Session Object--type javax.servlet.http.httpsession--scope--session
application--Application Object--type javax.servlet.servletcontext--scope--application
out--Output Object-type javax.servlet.jsp.jspwriter--scope--page
config--Configuration Object--type javax.servlet.servletconfig--scope--page
page--Page Object--type javax.lang.object--scope--page
exception--exception Object-type javax.lang.throwable--scope--page
The main four:
The Request object represents requests from the client, such as the information we fill out in the form form, and is commonly used for the most commonly used objects: GetParameter (), Getparameternames, getparametervalues Call these methods to get the values of the parameters contained in the Request object.
The response object represents the response to the client, which means that the data sent to the client can be organized through the response object. However, due to the organization of the lower level, it is not recommended for ordinary readers, need to send text to the client directly using
The PageContext object literal can be called a "page Context" object, which represents some of the properties of the current page run. Common methods are: Findattribute, GetAttribute, Getattributesscope and Getattributenamesinscope. In general, the PageContext object is not much used, only in the case of the situation of the project is more complex, will use the page properties to assist processing.
The Session object represents the sessions established by the server and the client, when it is necessary to retain customer information in different JSP pages, such as online shopping, customer track tracking, etc. The "session" object is based on a cookie and should be used with care to determine if the client has opened the cookie. Common methods include GetID, GetValue, GetValueNames and Putvalue.
Expand reading:
The Application object is responsible for providing some global information when the application is running on the server, such as GetMimeType and Getrealpath.
The Out object represents the object that sends the data to the client, and unlike the "response" object, the content sent through the "out" object will be the content that the browser needs to display, is the text level, and can be written directly to the client by the "Out" object, which dynamically generates the HTML file from the program. Common methods include clear, Clearbuffer, flush, getbuffersize, and getremaining, in addition to Pirnt and println, because a buffer is contained inside the "Out" object. So you need some way to manipulate the buffers.
The Config object provides configuration information that is commonly used in Getinitparameter and getinitparameternames to obtain parameters for servlet initialization.
The Page object represents a running class object produced by a JSP file and is not recommended for general audiences.
The exception object represents the exception object that is generated by the JSP file runtime, which cannot be used directly in a generic JSP file, but can only use four scopes in a JSP file that uses "<%@ page iserrorpage=" true "%>".
HTTP is a stateless (stateless) protocol, and Web server has no historical memory for every client request, the session is used to hold the client state information, written by the Web server, and stored on the client, Each access to the client passes the last session record to the Web server,web Server to read the session submitted by the client to get the status information of the client
What is the scope, let us first look at the effect: the approximate process is this, when we visit 04-01/index.jsp, respectively, PageContext, request, session, application four variables in the scope of the cumulative. (Of course, first of all to determine whether this variable is not present, if the variable does not exist, you have to initialize the variable to 1.) After the calculation is complete, jump from index.jsp execution forward to test.jsp. Once again in the test.jsp, the four integers are displayed.
Judging from the results shown, we can intuitively draw the conclusion that:
The variables in page cannot be passed from index.jsp to test.jsp. As soon as the page jumps, they are gone. The variables in the request can span two pages before and after forward. But as soon as you refresh the page, they recalculate. The variables in the session and the application always accumulate, and at the beginning there is no difference, as long as you close the browser and restart the browser to access the page again, the variables in the session are recalculated. The variables in the application keep accumulating until you restart Tomcat, otherwise it will keep getting bigger.
The scope specifies the validity period of the variable. If you put the variable in the PageContext, it means that its scope is page, its valid range is only in the current JSP page. You can use this variable from the start of placing the variable in PageContext to the end of the JSP page. If you put the variable in the request, it means that it is scoped to request, and its valid range is the current demand period. The so-called request period, that is, from the HTTP request initiation, to the end of the server processing, return the entire process of response. You may use forward to jump through multiple JSP pages in this process, and you can use this variable in these pages.
If you put the variable in the session, it means that it is scoped to the session, and its valid range is the current one. The so-called current session refers to the process of opening the browser from the user to the user closing the browser. This process may contain multiple request responses. In other words, as long as the user does not close the browser, the server has the means to know that these requests are initiated by a person, the entire process is called a session, and the variables placed in the session can be used in all requests of the current conversation. If you put the variable in the application, it means that its scope is application, and its effective range is the entire application. The entire application is launched from the app to the end of the app. We did not say "boot from server to server shutdown" because a server might deploy multiple applications, and of course you shut down the server and shut down all of the applications.
Application The variables in the scope, they survive the longest, and if not manually deleted, they can be used all the time.
Unlike the three above, the variables in the application can be shared by all users. If user A's operation modifies the variable in application, the User B gets the modified value when he accesses it. This will not happen in any other scope, page, request,session are completely isolated, regardless of the modification will not affect other people's data.
We get the value of the variable using public Object getattribute (string name), using the public void SetAttribute (string name, Object value) to save the value of the variable to the corresponding scope. An example of PageContext is:
Page
Integer countpage = (integer) pagecontext.getattribute ("Countpage");
if (countpage = = null)
{
Pagecontext.setattribute ("Countpage", 1);
}
Else
{
Pagecontext.setattribute ("Countpage", Countpage + 1);
}
This first takes the integer named Countpage from the PageContext, because the Java.lang.Object type is returned, so we need to cast it into the shape we need. The variable obtained here will return NULL if it does not exist, by judging countpage = = NULL to determine if the variable exists, if it does not exist, set to 1, if present, accumulate, and finally use setattribute () method to put the modified variable value into PageContext.
Replace the PageContext with request, session, application can manipulate the variables in the other three scopes.
JSP nine large objects