JSP has nine built-in objects:
(1) Session Object of the httpsession class
Purpose: stores the information of each user separately and the session associated with the request;
Session status maintenance is a must for Web application developers.
(2) request object of the httpservletrequest class
Role: indicates the request object. It is mainly used to receive data transmitted from the client to the server over HTTP.
(3) respone object of the httpservletresponse class
Role: indicates the response object, which is mainly used to send data to the client.
(4) Out object of the jspwriter class
Role: Mainly used to output data to the client;
The base class of out is javax. servlet. jsp. jspwriter.
(5) Application Object of the servletcontext class
Role: it is mainly used to save user information and the runtime environment of code snippets;
It is a shared built-in object, that is, multiple users in a container share an application object, so the stored information is shared by all users.
(6) pagecontext object of the pagecontext class
Purpose: Manage webpage attributes, wrap the context of the JSP page, and manage access to named objects in special visible parts of JSP, it is created and initialized by the container.
(7) config object of the servletconfig class
Purpose: The code snippet configuration object, indicating the servlet configuration.
(8) object-class page (equivalent to this) object
Purpose: Process JSP web pages. It is an instance of the object class and refers to an instance of the JSP implementation class. That is, it is also the JSP itself and is valid only within the JSP page range.
(9) exception
Purpose: handle errors and exceptions when executing JSP files
El has a total of 11 built-in objects:
JSP developers can use el implicit objects in El expressions without explicit coding and live declarations. Implicit objects are designed for use.
To provide convenience for JSP programming. Through implicit objects, code written with El can directly use some of the most commonly used JSP pages. There are a total of 11 implicit objects, which can be divided into five categories:
* JSP implicit object.
* Scope accesses implicit objects.
* Parameters access implicit objects.
* First, access the implicit object.
* The initialization parameters access the implicit object.
There is only one El implicit object in the JSP implicit object class, which is the pagecontext implicit object. This is the same as the JSP implicit object with the same name. The remaining El implicit objects are Java ing (MAP), which only provides an easier way to access certain properties of the pagecontext implicit object.
There are four scope implicit objects, pagination, requestscope, sessionscope, and applicationscope. These implicit objects are ing, and they can easily access the scope attributes. For example, the username attribute appended to the request scope can be directly accessed through the El expression $ {requestscope. Username.
Two parameters can be used to access the HTTP Request Parameters (Form submission parameters), namely, Param and paramvalues. param is a ing used to access Single-value parameters. paramvalues can be used to access parameters that may contain multiple values. The following experiment will show how this will be done.
There are three header access implicit objects, which can be used to access the HTTP header, namely header, headervalues and cookie. These mappings are useful if you want to access the HTTP header or cookie in the original way.
There is also an implicit object for accessing the initialization parameter: initparm. This ing can be used to access the values of initialization parameters. The values of initialization parameters are generally set in Web. xml.
Category |
Identifier |
Description |
JSP |
Pagecontext |
The pagecontext instance corresponds to the processing of the current page |
Scope |
Pagination |
Map class associated with the name and value of the page scope attribute |
Requestscope |
Map class associated with the name and value of the Request scope attribute |
Sessionscope |
Map class associated with the name and value of the session scope attribute |
Applicationscope |
Map class associated with the name and value of the application scope attribute |
Request Parameters |
Param |
Map class that stores the main values of Request Parameters by name |
Paramvalues |
Use all the values of the request parameters as the map class stored in the string array. |
Request Header |
Header |
Map class that stores the main values of request headers by name |
Headervalues |
Use all the values in the Request Header as the map class stored in the string array. |
Cookie |
Cookie |
Store the map class of the cookie attached to the request by name |
Initialization parameters |
Initparam |
Map class that stores context initialization parameters for Web applications by name |
In fact, my main purpose is to clarify the pagecontext object, as follows:
Functions of pagecontext in JSP
The implicit object of pagecontext corresponds to javax. servlet. JSP. pagecontext objects and hidden objects are automatically added to pagecontext. You can use them to obtain servlet objects corresponding to JSP-related hidden objects, such as getrequest () servletrequest can be obtained, while servletconfig () can be obtained, servletconfig can be obtained, and httpsession can be obtained by getsession. Servlet objects corresponding to hidden objects are not the main function of pagecontext. Its main function is to provide a single interface to manage various public objects (such
Httpsession, servletcontext, servletconfig, servletrequest, servletresponse, etc.), provides a single API to manage the attribute scope and so on.
We have previously used the setattribute () method of the session to set attribute objects that can be shared by a process. The attributes set by the session can be shared in the same process, except for sessions, you can also use methods such as setattribute () for pagecontext, request, and application (For details, refer to the API file) to set attribute objects that can be shared, however, the attributes set by these four objects have different sharing ranges.
The property objects set by pagecontext are shared within the same JSP page and the property objects set by request are used, it can be shared during the same request processing (including forward to other JSP pages), and the property objects set by the session object can be shared only during the same process, the attributes set by the Application object can be shared on the JSP page of the entire web application.
In the following example, you can set some objects to application as attributes, and the other JSP page can obtain this attribute object as appropriate. For example:
<%
String ATTR = "String object ";
Out. println ("set attributes to application:" + ATTR );
Application. setattribute ("str", ATTR );
%>
We first connect to this JSP page to perform attribute settings, and then we connect to this JSP page:
<%
String ATTR = (string) application. getattribute ("str ");
Out. println ("Get application attributes:" + ATTR );
%>
Since we have set the string object in application as an attribute, we can obtain
In the same way, you can set attributes for objects such as pagecontext, request, and session in the same way, you can get the set attribute object.
You can use the pagecontext object to set attributes and specify the scope of the attributes, instead of using individual pagecontext, request, session, and application to set attributes. This is what we mentioned earlier, pagecontext provides a single API to manage the attribute scope. You can use the following methods to set attributes:
Getattribute (string name, int scope)
Setattribute (string name, object value, int scope)
Removeattribute (string name, int scope)
The scope can be specified using the following constants: pagecontext. page_scope, pagecontext. request_scope, pagecontext. session_scope, pagecontext. application_scope. The constant name already specifies the attribute range. We can change the preceding two JSP pages to the following format:
<%
String ATTR = "String object ";
Out. println ("set attributes to application:" + ATTR );
Pagecontext. setattribute ("str", ATTR, pagecontext. application_scope );
%>
<%
String ATTR = (string) pagecontext. getattribute ("str", pagecontext. application_scope );
Out. println ("Get application attributes:" + ATTR );
%>
JSP implicit object and El implicit object
JSP implicit object:
First, you need to know what is a JSP implicit object. It is an instance of a group of classes loaded by web containers. It does not need to be "new" to obtain instances as common Java objects do, instead, you can directly use the object on the JSP page. The implicit objects provided by JSP are divided into four main categories:
1. Input and Output objects: request, response, and out objects.
2. Scope communication objects: Session, application, and pagecontext.
3. servlet objects: config and page.
4. Error object: exception.
El implicit object identifier
Do not confuse these objects with JSP implicit objects (only nine in total). Only one of these objects is shared by them.
El implicit object
CATEGORY identifier description
The JSP pagecontext instance corresponds to the processing of the current page
Map class associated with the names and values of the pagination scope and page scope attributes
Map class associated with the names and values of requestscope and request scope attributes
Map class associated with the names and values of sessionscope and session scope attributes
Map class associated with the names and values of applicationscope attributes and application scope attributes
Request Parameter Param stores the map class of the main values of Request Parameters by name
Paramvalues uses all the values of request parameters as the map class stored in the string array.
The request header stores the map class of the main value of the Request Header by name.
Headervalues uses all the values in the Request Header as the map class stored in the string array.
Cookie cookie store the map class of the cookie included in the request by name
The initialization parameter initparam stores the map class of the Web application context initialization parameter by name
Although there is only one common object (pagecontext) in JSP and El implicit objects, other JSP implicit objects can be accessed through El. The reason is that pagecontext has the ability to access all the other eight JSP implicit objects. In fact, this is the main reason to include it in the El implicit object.
All other El implicit objects are mapped and can be used to find the objects corresponding to the name. The first four mappings indicate various attribute scopes discussed previously. They can be used to find identifiers in a specific scope, instead of relying on the sequential lookup process used by El by default.
The next four mappings are used to obtain the values of Request Parameters and request headers. Because HTTP allows Request Parameters and request headers to have multiple values, each of them has a ing relationship. The first ing in each pair returns the main value of the request parameter or header, which usually happens to be the value specified first in the actual request. The second ing of each pair allows you to retrieve all values of a parameter or header. The keys in these mappings are the names of parameters or headers, but these values are arrays of string objects. Each element is a single parameter value or header value.
The cookie implicit object provides access to the cookie name set by the request. This object maps all cookie names associated with the request to Cookie objects that represent the cookie features.
The last El implicit object initparam is a ing that stores the names and values of initialization parameters for all contexts associated with the Web application. The initialization parameter is specified through the Web. xml deployment descriptor file, which is located in the WEB-INF directory of the application.