JSP nine large built-in objects

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

Out : 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

A subtype of response:javax.servlet.ServletResponse that encapsulates the output returned to the HTTP client, providing the page author with a way to set the response header and status code. Often used to set HTTP headers, add cookies, set the type and status of response content, send HTTP redirects, and encode 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 including 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 is to be remembered that the PageContext object can also get and set session properties in the same way as Session.getattribute (), Session.setattribute ().

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 a way to register information about server versions, application-level initialization parameters, and absolute path to in-app resources

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 requestresponse         Response object              type Javax.servlet.SrvletResponse       Scope  pagepagecontext    Page Context object       type Javax.servlet.jsp.PageContext      Scope    Pagesession           Session object               type Javax.servlet.http.HttpSession     scope    sessionapplication      Application Object          type Javax.servlet.ServletContext        scope    applicationout                output Object               type Javax.servlet.jsp.JspWriter scope    pageconfig             Configuration object               type Javax.servlet.ServletConfig          scope    pagepage              Page object               type Javax.lang.Object                       scope    pageexception        exception Object                type Javax.lang.Throwable                  Scope    

  

The Request object represents requests from the client, such as the information we fill out in the form form, and is commonly used for the most commonly used objects: 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.

Profile

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 can only use four scopes in a JSP file that uses "<%@ page iserrorpage=" true "%>":

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, first of all to determine whether this variable is not present, if the variable does not exist, you have to initialize the variable to 1.) After the calculation is complete, jump from index.jsp execution forward to test.jsp. Once again in the test.jsp, the four integers are displayed.

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 always accumulate, and at the beginning there is no difference, as long as you close the browser and restart the browser to access the page again, 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. You may use forward to jump through multiple JSP pages in this process, and you can use this variable in 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 responses. In other words, as long as the user does not close the browser, the server has the means to know that these requests are initiated by a person, the entire process is called a session, and the variables placed in the session can be used in all requests of 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 server might deploy multiple applications, and of course you shut down the server and shut down all of the applications.

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 variable in application, the User B gets the modified value when he accesses it. This will not happen in any other scope, page, request,session are completely isolated, regardless of the modification will not affect other people's data.

We get the value of the variable using public Object getattribute (string name), using the public void SetAttribute (string name, Object value) to save the value of the variable to the corresponding scope. An example of PageContext is:

  

Page 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 we need to cast it into the shape we need. The variable obtained here will return NULL if it does not exist, by judging countpage = = NULL to determine if the variable exists, if it does not exist, set to 1, if present, accumulate, and finally use setattribute () method to put the modified variable value into PageContext.

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

(Transferred from Http://www.233.com/Java/jichu/20100329/091644792.html)

JSP nine large built-in objects (GO)

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.