Nine built-in Jsp objects and nine built-in jsp objects

Source: Internet
Author: User

Nine built-in Jsp objects and nine built-in jsp objects

Nine built-in Jsp objects

Implicit object

Description

Request

After translation, the corresponding HttpServletRequest/ServletRequest object

Response

Corresponding to the HttpServletRespons/ServletResponse object after translation

Session

Corresponding HttpSession object after translation

Application

Corresponding to the ServletContext object after translation

Out

Corresponding to the JspWriter object after translation, which is internally associated with a PringWriter object

Page

Corresponding to this after translation

Config

Corresponding to the ServletConfig object after translation

Exception

After translation, the corresponding Throwable object indicates the exception object thrown by other JSP pages. It only appears on the JSP error page (the JSP page with isErrorPage set to true)

PageContext

After translation, it corresponds to the PageContext object. It encapsulates JSP page resources and can set page range attributes.

Jsp implicit variable 1. out comes from the Java. io. Writer class, which is used to send the output to the client.
2. The request comes from the javax. servlet. http. HttpServletRequest interface. It is associated with every HTTP request.
3. response comes from javax. servlet. http. HttpServletResponse. The response object is used to return the obtained data to the client.
4. pageContext provides a namespace for accessing JSP pages. It also provides access to other JSP implicit objects.
5. The session is from javax. servlet. http. HttpSession. It is used to store client request information, so it is stateful interactive.
6. The application comes from javax. servlet. ServletContext.
7. config comes from ServletConfig, which contains the configuration information of the WEB application where the current JSP/Servlet is located.
8. page comes from the instantiation of the currently accessed JSP page. It actually uses the Servlet converted from JSP.
9. exception is used to capture exceptions thrown by JSP. It is available only when the JSP page attribute isErrorPage = true.

 

Nine built-in JSP objects:

One request object:

This object encapsulates the information submitted by the user. You can call the corresponding method of this object to obtain the encapsulated information, that is, you can use this object to obtain the information submitted by the user. The Request Parameters of the client are encapsulated in this object.

2. response object: This object dynamically responds to a customer's request and sends data to the client. Three session objects

1. What is session: the session object is a JSP built-in object, which is automatically created when the first JSP page is loaded to complete session management.
A session starts when a client opens a browser and connects to the server, and ends when the client closes the browser and leaves the server. When a customer accesses a server, it may connect several pages of the server and refresh a page repeatedly. The server should know in some way that this is the same customer, this requires the session object.
2. session Object ID: when a customer visits a JSP page on the server for the first time, the JSP engine generates a session object and assigns a String-type ID, the JSP engine sends this ID number to the client and stores it in the Cookie, so that the session object and the customer have a one-to-one correspondence relationship. When the customer accesses other pages connected to the server, it will not allocate new session objects to the customer until the client closes the browser, and the session objects of the client on the server will be canceled, and the relationship with the customer's session disappears. When the client re-opens the browser and connects to the server, the server creates a new session object for the client.

4. aplication object

1. What is application:
After the server is started, this application object is generated. When the client browses between pages of the accessed website, the application object is the same until the server is closed. However, unlike session, all customers share the same application object.
2. Common Methods for application objects:
(1) public void setAttribute (String key, Object obj): add the Object obj specified by the parameter Object to the application Object, and specify an index keyword for the added Object.
(2) public Object getAttribute (String key): obtains the Object containing keywords in the application Object.

5-out object

An out object is an output stream used to output data to the client. The out object is used to output various types of data.

6. pagejava. lang. Object

Corresponds to the this keyword. JSP page itself
The page object is an instance of the Servlet class after the current page is converted. From the converted Servlet class code, we can see this relationship:
Object page = this;
In JSP pages, page objects are rarely used.

Seven config

Javax. servlet. ServletConfig instance, which represents the configuration information of the JSP. Common methods include getInitPararneter (String paramNarne) and getInitPararneternarnes. In fact, JSP pages do not need to be configured, so there is no configuration information. Therefore, this object is more effective in Servlet.

8. exceptionjava. lang. Throwable instance

This instance indicates exceptions and errors on other pages. This object can be used only when the isErrorPage attribute of the compiled command page is true. Common methods include getMessageO and printStackTraceO.

9. pageContext

Javax. servlet. jsp. an example of PageContext. when an object is literally translated, it can be called a "Page Context" object, which represents some attributes of the current page. Through this object, you can get the other eight objects, you can use this object to access the shared data on the page. Common methods include getServletContextO and getServletConfigO.

// Use pageContext to set the property, which is within the page range by default
PageContext. setAttribute ("page", "hello ");

// Use request to set the property, which is within the request range by default
Request. setAttribute ("request", "hello ");

// Use pageContext to set attributes in the request range
PageContext. setAttribute ("request2", "hello", pageContext. REQUEST_SCOPE );

// Use session to set the attribute to the session range
Session. setAttribute ("session", "hello" l;

// Use pageContext to set the attribute to the session range
PageContext. setAttribute ("session2", "hello", pageContext. SESSION_SCOPE );

// Use application to set attributes in the application range
Application. setAttribute ("app", "hello ");

// Use pageContext to set attributes in the application range
PageContext. setAttribute ("app2", "hello", pageContext. APPL ccation_scope );

Four Scopes: request domain session domain application domain page domain

Application:
Global function scope: the whole application is shared, that is, the same webApp in the deployment file is shared, the life cycle is: The application starts to stop.

Session:
Session scope. When a user accesses a session for the first time, a new session is generated. Then the server can remember the session status. Life cycle: the session times out, or the server forces the session to fail.

Request:
The request scope is a client request.

Page:
A JSP page.

The scope of the above is getting smaller and smaller, and 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 ).

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.

Request Request object type javax. servlet. ServletRequest scope request

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 object type javax. lang. Throwable scope page

Classification: Servlet

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.