JSP nine large built-in objects and four scopes

Source: Internet
Author: User

Nine main objects:

Built-in objects (also known as hidden objects, 9 built-in objects): Free to use in script code and expressions without pre-declaration


The Javax.servlet.jsp.JspWriter type that represents the output stream object. Scope is page (page execution period)

A subtype of request:javax.servlet.ServletRequest that encapsulates HTTP generated by a Web browser or other client

The details of the request (parameters, attributes, headers, and data). The scope is request (user demand period).
Method can be viewed by the API

The Response:javax.servlet.ServletResponse subtype, which encapsulates the output returned to the HTTP client, to the page

The author provides a way to set the response headers and status codes. Often used to set HTTP headers, add cookies, set the type and shape of the response content

HTTP redirection and encoding URLs. A scope is page (page execution period).

PageContext:javax.servlet.jsp.PageContext (abstract Class) type, scoped to page (pages execution period). This object

Provides all four scope-level property query and modification capabilities, and it also provides methods for forwarding requests to other resources and containing other resources

The methods of the object are abstract methods

Session:javax.servlet.http.HttpSession type, primarily used to track conversations. Scope session (session period-).

HttpSession is a hash table-like object associated with a single Web browser session that exists between HTTP requests and can store any

What type of named object.
If you do not need to track the session object between requests, you can specify session= "false" in the page directive
It should be remembered that the PageContext object can also be taken in the same way as Session.getattribute (), Session.setattribute ()

and set the session properties.

Application:javax.servlet.ServletContext type, the servlet environment is called by the Getservletconfig

(). GetContext () method obtained. The scope is application (the entire program runs). It provides information about the server version, the application

Level initialization parameters and absolute path of in-app resources, how to register information

Config:javax.servlet.ServletConfig, scoped to page (pages execution period)

Exception:java.lang.Throwable, a catch block in a JSP error page has been benefited but not captured

Any instance of the java.lang.Throwable is passed to the URI of the ErrorPage. A scope is page (page execution period). Attention

Exception is valid only if the page directive has the attribute iserrorpage= "true".

The Page:java.lang.Object type, which points to the way the page itself. Scope is page (pages execution period

The nine large built-in objects in the JSP are:
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 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 object Type Javax.lang.Object Scope page
Exception exception Object type javax.lang.Throwable Scope page

The Request object represents requests from the client, such as the information we fill out in the form form, and is the most commonly used object

Common methods are: GetParameter, getparameternames, and Getparametervalues call these methods to get the values of the parameters contained in the Request object.

The response object represents the response to the client, which means that the data sent to the client can be organized through the response object. However, due to the organization of the lower level, it is not recommended for ordinary readers, need to send text to the client directly using

The PageContext object literal can be called a "page Context" object, which represents some of the properties of the current page run

Common methods are: Findattribute, GetAttribute, Getattributesscope and Getattributenamesinscope
In general, the PageContext object is not much used, only in the case of the situation of the project is more complex, will use the page properties to assist processing.

The Session object represents the sessions established by the server and the client, when it is necessary to retain customer information in different JSP pages, such as online shopping, customer track tracking, etc. The "session" object is based on a cookie and should be used with care to determine if the client has opened the cookie. Common methods include GetID, GetValue, GetValueNames and Putvalue.

HTTP is a stateless (stateless) protocol;
Web Server has no historical memory for every client request;
Session is used to save client state information;
Written by web Server;
stored in the client;
Each access to the client passes the last session record to the Web Server;
Web server reads the client-submitted session to get status information for the client

The Application object is responsible for providing some global information when the application is running on the server, such as GetMimeType and Getrealpath.

The Out object represents the object that sends the data to the client, and unlike the "response" object, the content sent through the "out" object will be the content that the browser needs to display, is the text level, and can be written directly to the client by the "Out" object, which dynamically generates the HTML file from the program. Common methods include clear, Clearbuffer, flush, getbuffersize, and getremaining, in addition to Pirnt and println, because a buffer is contained inside the "Out" object. So you need some way to manipulate the buffers.

The "config" object provides configuration information that is commonly used in Getinitparameter and getinitparameternames to obtain parameters for servlet initialization.

The "Page" object represents a running class object produced by a JSP file and is not recommended for general audiences.

The "Exception" object represents the exception object that is generated by the JSP file runtime, which cannot be used directly in a generic JSP file, but only in a JSP file that uses "<%@ page iserrorpage=" true "%>".

Four scopes:

What is a scope
First let's look at the effect:

The process is like this, when we visit 04-01/index.jsp, respectively, to PageContext, request, session,

Application The variables in four scopes to accumulate. (Of course, the variable is not present, if the variable does not exist, then

Initialize the variable to 1. After the calculation is complete, jump from index.jsp execution forward to test.jsp. One more time in the test.jsp.

Accumulate and then show these four integers.

Judging from the results shown, we can intuitively draw the conclusion that:

The variables in page cannot be passed from index.jsp to test.jsp. As soon as the page jumps, they are gone.

The variables in the request can span two pages before and after forward. But as soon as you refresh the page, they recalculate.

The variables in the session and the application are always cumulative and start to see no difference, as long as you close the browser, restart the browser again to access

On this page, the variables in the session are recalculated.

The variables in the application keep accumulating until you restart Tomcat, otherwise it will keep getting bigger.

The scope specifies the validity period of the variable.

If you put the variable in the PageContext, it means that its scope is page, its valid range is only in the current JSP page.

You can use this variable from the start of placing the variable in PageContext to the end of the JSP page.

If you put the variable in the request, it means that it is scoped to request, and its valid range is the current demand period.

The so-called request period, that is, from the HTTP request initiation, to the end of the server processing, return the entire process of response. In this process, it is possible to make

Jump through multiple JSP pages in a forward way, and you can use this variable on these pages.

If you put the variable in the session, it means that it is scoped to the session, and its valid range is the current one.

The so-called current session refers to the process of opening the browser from the user to the user closing the browser. This process may contain multiple

Request response. In other words, as long as the user does not close the browser, the server has a way to know that these requests are initiated by a person, the entire process is

Referred to as a session, and the variables placed in the session can be used in all requests for the current conversation.

If you put the variable in the application, it means that its scope is application, and its effective range is the entire application.

The entire application is launched from the app to the end of the app. We did not say "boot from server to server shutdown" because a service

Can deploy multiple applications, of course, if you shut down the server, you will shut down all the applications above.

Application The variables in the scope, they survive the longest, and if not manually deleted, they can be used all the time.

Unlike the three above, the variables in the application can be shared by all users. If user A's operation modifies the application

The variable in the user B is given a modified value when it is accessed. This will not happen in any other scope, page, request,

The session is completely isolated, and any modification will not affect the other person's data.

We get the value of the variable using public Object getattribute (String name), using the public void SetAttribute

(String name, Object value) saves the value of the variable to the corresponding scope. An example of PageContext is:

Integer countpage = (integer) pagecontext.getattribute ("Countpage");
if (countpage = = null) {
Pagecontext.setattribute ("Countpage", 1);
} else {
Pagecontext.setattribute ("Countpage", Countpage + 1);
This first takes the integer named Countpage from the PageContext, because the Java.lang.Object type is returned, so a strong

Transformation into the shaping we need. The variable obtained here will return NULL if it does not exist, by judging countpage = = NULL to distinguish

If no variable exists, set to 1 if it does not exist, accumulate if it exists, and then use the SetAttribute () method to modify the

The value of the variable into PageContext.

Replace the PageContext with request, session, application can manipulate the variables in the other three scopes.

Quote Original: http://www.cnblogs.com/stanljj/p/4117980.html

Blog is to remember that they are easy to forget things, but also a summary of their work, the article can be reproduced, without copyright. Hope to do their own efforts to do better, we work together to improve!

If there is any problem, welcome to discuss together, code if there is a problem, you are welcome to the great God!

JSP nine large built-in objects and four scopes

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.