Introduction to nine built-in JSP objects and four scopes

Source: Internet
Author: User

Nine JSP 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)

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: The subtype of javax. servlet. servletresponse. This object encapsulates the output returned to the HTTP client and sends it to the page

The author provides a method to set the response header and status code. It is often used to set HTTP headers, add cookies, and set the types and formats of response content.

Sends 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 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 be retrieved in the same way as session. getattribute () and session. setattribute ().

And set session properties.

Application: javax. servlet. servletcontext type. The servlet environment calls 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

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

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, but can only be used in" ".

Four Scopes:

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.

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.