Nine objects:
Built-in object (also called implicit object, with nine built-in objects ):CodeAnd can be used freely in expressions.
1-out:
Javax. servlet. jsp. jspwriter type, representing the object of the output stream. Scope: page (page execution period)
Request: subtype of javax. servlet. servletrequest. This object encapsulates the HTTP
Request details (parameters, attributes, headers, and data ). The scope is request (user request period ).
You can view the API
Response: 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 ).
Pagecontext: javax. servlet. jsp. pagecontext (abstract class) type, scope is page (page execution period ). This object provides the ability to query and modify attributes at all four levels of scopes. It also provides methods to forward requests to other resources and include other resources:
All methods of this object are abstract methods.
Session: javax. servlet. http. httpsession type. It is 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 be stored as any
The type of the named object.
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.
Application: javax. servlet. servletcontext type. The servlet environment calls getservletconfig
(). Getcontext () method. The scope is application (the entireProgramRuntime ). It provides information about Server versions, application-level initialization parameters, absolute paths of resources in the application, and registration.
Config: javax. servlet. servletconfig, with the scope of page (page execution period)
Exception: Java. Lang. throwable, which has been obtained 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.
Page: Java. Lang. Object type, pointing to the page itself. The scope is page (page execution period
The nine built-in objects in 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 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 Object Type javax. Lang. object scope page
Exception exception object type javax. Lang. throwable scope page
Come
The request object represents a request from a client, such as the information we enter in the form. The most common methods of objects are: getparameter, getparameternames, and getparametervalues call these methods 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, you can only use four scopes in the JSP file that uses "<% @ page iserrorpage =" true "%>:
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, initialize the variable to 1 .) After the calculation is complete, execute forward from index. jsp to test. jsp. Accumulate again in test. jsp, and then display the four integers.
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. As long as you close the browser and restart the browser to access this page, the variables in the session will be 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, you may use the forward method to jump 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 responses. 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. The whole process is called a session and put into the session variable, it 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 one server may deploy multiple applications. Of course, if you close the server, all the above applications will be shut down.
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 variables in the application, user B obtains the modified value during access. This will not happen in other scopes. pages, requests, and sessions are completely isolated, and data of others will not be affected no matter how modified.
We use public object getattribute (string name) to obtain the variable value, and use public void setattribute (string name, object value) to save 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 to forcibly convert them to the integer we need. If the obtained variable does not exist, null is returned. countpage = NULL is used to identify whether the variable exists. If the variable does not exist, it is set to 1. If the variable exists, it is accumulated, finally, use the setattribute () method to put the modified variable value into pagecontext.
Replace pagecontext with request and session, and the application can operate the variables in the other three scopes.