Nine objects:
Built-in object (also known as implicit object, with nine built-in objects): You can use it in script code and expressions without pre-declaration.
1-out:
Javax. servlet. jsp. jspwriter type, representing the object of the output stream. Scope: page (page execution period)
2-request:
The subtype of javax. servlet. servletrequest. This object encapsulates the details (parameters, attributes, header labels, and data) of HTTP requests generated by Web browsers or other clients ). The scope is request (user request period ). You can view the API
3-response:
The subtype of javax. servlet. servletresponse. This object encapsulates the output returned to the HTTP client and provides the page author with the method of setting the response header and status code. It is often used to set HTTP headers, add cookies, set the type and status of response content, and send HTTP redirection and encoded URLs. The scope is page (page execution period ).
4-pagecontext:
Javax. servlet. jsp. pagecontext (abstract class) type, scope is page (page execution period ). This object provides attribute query and modification capabilities for all four scopes. It also provides methods for forwarding requests to other resources and including other resources: the methods of this object are abstract methods.
5-session:
Javax. servlet. http. httpsession type, mainly used to track conversations. Scope session (Session Period -).
Httpsession is an object related to a single web browser session similar to a hash table. It exists between HTTP requests and can store any type of named objects.
If you do not need to track session objects between requests, you can specify session = "false" in the page command"
Remember that the pagecontext object can also be obtained and set session properties in the same way as session. getattribute () and session. setattribute.
6-application:
Javax. servlet. servletcontext type. The servlet environment is obtained by calling the getservletconfig (). getcontext () method. The scope is application (throughout the program running ). It provides the server version and Application
LEVEL initialization parameters and absolute path of resources in the application, registration information method
7-config:
Javax. servlet. servletconfig, with the scope of page (page execution period)
8-page:
Java. Lang. Object type, pointing to the page itself. The scope is page (page execution period
9-exception:
Java. Lang. throwable, which has been benefited but not captured by a catch block on the JSP error page
Any instance of Java. Lang. throwable transmits to the URI of errorpage. The scope is page (page execution period ). Note:
Exception is valid only when the page command has the attribute iserrorpage = "true.
The nine built-in objects in JSP are:
1 request object type javax. servlet. servletrequest scope request
2 response object type javax. servlet. srvletresponse scope page
3 pagecontext Page Context object type javax. servlet. jsp. pagecontext scope page
4 session object type javax. servlet. http. httpsession scope session
5 Application Object Type javax. servlet. servletcontext Scope application
6 out output object type javax. servlet. jsp. jspwriter scope page
7 config configure the object type javax. servlet. servletconfig scope page
8 page object type javax. Lang. object scope page
9 exception object type javax. Lang. throwable scope page
The request object represents a request from the client, such as the information we fill in the Form. It is the most common object.
Common methods include getparameter, getparameternames, and getparametervalues. These methods are called to obtain the values of parameters contained in the request object.
The response object represents the response to the client, that is, the data sent to the client can be organized through the response object. However, because the organization mode is relatively low-level, it is not recommended for common readers to use it directly when sending text to the client.
The pagecontext object can be called a page context object during literal translation, which indicates the running properties of the current page.
Common methods include findattribute, getattribute, getattributessscope, and getattributenamesinscope.
In general, not many pagecontext objects are used. Page properties are used for processing only when the project is complex.
The Session object represents the session established between the server and the client. It is used when customer information needs to be retained on different JSP pages, such as online shopping and customer Track Tracking. The "session" object is based on the cookie. Therefore, you should check whether the client has enabled the cookie. Common methods include GETID, getvalue, getvaluenames, and putvalue.
Summary
HTTP is a stateless protocol;
The web server has no historical memory for each client request;
Session is used to save client status information;
Written by the Web server;
Stored on the client;
Each client access passes the last session record to the Web server;
The Web server reads the session submitted by the client to obtain the client's status information.
The Application Object provides global information about the application running on the server. Common methods include getmimetype and getrealpath.
The out object represents the object that sends data to the client. Unlike the "response" object, the content sent through the "out" object is the content that the browser needs to display, which is text-level, you can use the "out" object to directly write an HTML file dynamically generated by the program to the client. In addition to pirnt and println, common methods include clear, clearbuffer, flush, getbuffersize, and getremaining. This is because the "out" object contains a buffer, therefore, some operations on the buffer zone are required.
The Config object provides some configuration information. Common methods include getinitparameter and getinitparameternames to obtain the parameters during servlet initialization.
The page object represents a running class object generated by JSP files. It is not recommended for general readers.
The exception object represents the exception object generated when the JSP file is running. This object cannot be directly used in common JSP files, instead, it can only be used in JSP files that use "<% @ page iserrorpage =" true "%>"
Four Scopes: pagecontext, request, session, and Application
Scope
Let's take a look at the effect:
The general process is as follows: when we access 04-01/index. jsp, we perform pagecontext, request, session,
The variables in the four scopes of application are accumulated. (Of course, first determine whether the variable exists. If the variable does not exist, you must
Initialize the variable to 1 .) After the calculation is complete, execute forward from index. jsp to test. jsp. Run the test. jsp command again.
Then, the four integers are displayed.
From the displayed results, we can intuitively draw a conclusion:
The variables in the page cannot be passed from index. jsp to test. jsp. As long as the page jumps, they will disappear.
The variables in the request can span the two pages before and after the forward. However, you only need to refresh the page, and they will recalculate it.
The variables in the session and application have been accumulating and there is no difference at first. You only need to close the browser and restart the browser to access
On this page, the variables in the session are recalculated.
The variables in the application keep accumulating. Unless you restart tomcat, it will keep increasing.
The scope specifies the validity period of the variable.
If you put the variable in pagecontext, it indicates that its scope is page, and its valid range is only in the current JSP page.
You can use this variable from placing it in pagecontext to the end of the JSP page.
If you put the variable in the request, it indicates that its scope is request, and its valid range is the current request cycle.
The request cycle refers to the whole process from initiating an HTTP request to processing the server and returning a response. In this process
The forward method redirects to multiple JSP pages. You can use this variable in these pages.
If the variable is put into the session, it indicates that its scope is session, and its valid range is the current session.
The current session refers to the process from when the user opens the browser to when the user closes the browser. This process may contain multiple
Request Response. That is to say, as long as the user is not in the browser, the server can know that these requests are initiated by a person, and the whole process is
It is called a session, and the variables put in the session can be used in all requests of the current session.
If you put the variable in the application, it indicates that its scope is application, and its effective scope is the entire application.
The entire application starts from the application and ends with the application. We didn't say "starting from the server to shutting down the server" because of a service
You may deploy multiple applications. Of course, if you close the server, all the above applications will be closed.
Variables in the application scope have the longest survival time. They can be used without manual deletion.
Different from the preceding three, the variables in the application can be shared by all users. If user a's operations modify the application
User B obtains the modified value during access. This will not happen in other scopes, such as page, request,
Sessions are completely isolated, and no matter how modified, other people's data will be affected.
We use public object getattribute (string name) to obtain the variable value, and use public void setattribute
(String name, object Value) saves the variable value to the corresponding scope. For example, pagecontext:
// Page
Integer countpage = (integer) pagecontext. getattribute ("countpage ");
If (countpage = NULL ){
Pagecontext. setattribute ("countpage", 1 );
} Else {
Pagecontext. setattribute ("countpage", countpage + 1 );
}
Here, we first retrieve the integer countpage from pagecontext, because the returned values are of the Java. Lang. Object type, so we need
Convert to the integer we need. If the obtained variable does not exist, null is returned, which is determined by countpage = NULL.
Whether the variable exists. If it does not exist, set it to 1. If it exists, accumulate the variable and use the setattribute () method to modify the variable.
Put the variable value in pagecontext.
Replace pagecontext with request and session, and the application can operate the variables in the other three scopes.