JSP built-in objects: the use of PageContext scope objects

Source: Internet
Author: User
What are the built-in objects for JSPs? What their function is, this article mentions the nine big built-in objects of JSP, in which the PageContext object is introduced in detail, PageContext object is the most important object in JSP technology, it represents the running environment of JSP page, This object not only encapsulates a reference to the other 8 large implicit objects, it itself is also a domain object (container) that can be used to hold the data. Other objects can also be obtained through PageContext, which is explained in detail below.

First, the principle of JSP operation

When each JSP page is accessed for the first time, the Web container will give the request to the JSP engine (a Java program) to process. The JSP engine first translates the JSP into a _jspservlet (essentially a servlet) and then invokes it in the way the servlet calls.
Because the JSP is translated into a servlet on the first visit, the first access is usually slower, but the second access, if the JSP engine finds that the JSP has not changed, is no longer translated, but is called directly, so the execution efficiency of the program will not be affected.
When the JSP engine invokes the _jspservlet of a JSP, it passes or creates 9 web-development-related objects for _jspservlet to use. JSP technology designers for developers to write JSP pages when they get references to these Web objects, deliberately defined 9 corresponding variables, developers in the JSP page through these variables can quickly get these 9 large object references.

Ii. recognize nine built-in objects

NO. Built-in objects Type
1 PageContext Javax.servlet.jsp.PageContext
2 Request Javax.servlet.http.HttpServletRequest
3 Response Javax.servlet.http.HttpServletResponse
4 Session Javax.servlet.http.HttpSession
5 Application Javax.servlet.ServletContext
6 Config Javax.servlet.ServletConfig
7 Out Javax.servlet.jsp.JspWriter
8 Page Java.lang.Object
9 exception Java.lang.Throwable

  

Request,response,session,application,config These objects have been described in detail in the first place, where the rest of the PageContext objects, out objects, page objects are highlighted.

Three, built-in object use instructions

3.1. Page Object

The Page object represents the current JSP page, which can be understood as an object itself, that is, to treat a jsp as an object. Page objects are almost never used in development.

3.2. Out Object

The Out object is used to send text data to the client.
The Out object is returned by calling the Getout method of the PageContext object, and its role and usage is very similar to the PrintWriter object returned by the Servletresponse.getwriter method.
The type of an out object in a JSP page is jspwriter,jspwriter equivalent to a printwriter with a cache feature, and the buffer property of the page directive that sets the JSP page can resize its cache or even turn it off.
Only if the content is written to an out object and any of the following conditions are met The Out object does not call the Servletresponse.getwriter method, and the PrintWriter object returned by the method actually writes the contents of the out-object's buffer to the buffer provided by the servlet engine:

    • Setting the Buffer property of the page directive turns off caching for out objects

    • The buffer for the Out object is full

    • End of entire JSP page

Working schematic of an Out object

  

3.3. PageContext Object

PageContext object is the most important object in JSP technology, which represents the environment of JSP page, which not only encapsulates the reference to other 8 large implicit objects, but also is a domain object (container), which can be used to save the data. Also, this object encapsulates common operations often involved in web development, such as introducing and jumping other resources, retrieving properties from other domain objects, and so on.

3.4. Get other objects through PageContext

    • The GetException method returns an exception implicit object

    • The GetPage method returns the page implicit object

    • The Getrequest method returns the request implicit object

    • The GetResponse method returns an response implicit object

    • The Getservletconfig method returns the Config implicit object

    • The Getservletcontext method returns an application implicit object

    • The GetSession method returns the session implicit object

    • The Getout method returns an out implicit object

3.5, PageContext package Other 8 large built-in objects meaning

If the PageContext object is passed to a plain Java object during programming, the Java object will be able to get 8 of the most implicit objects, and the Java object can interact with the browser, and the Java object becomes a dynamic web resource. This is the meaning of PageContext package other 8 large built-in objects, the PageContext to whom, who can become a dynamic Web resources, then under what circumstances need to pass PageContext to another Java class, under what circumstances need to use this technology, In the more formal development, JSP page is not allowed Java code, if the JSP page appeared Java code, then you should find a way to remove Java code, we can develop a custom tag to remove the Java code on the JSP page, First, a Java class is written around a custom tag, and the JSP engine invokes the Java class that is written around the custom tag when it executes the custom tag, and when the Java class is called, it passes the PageContext object to the Java class. Since the PageContext object encapsulates references to other 8 large implicit objects, it is possible to use the 8 large implicit objects in the JSP page in this Java class (Request,response,config,application,exception, Session,page,out), PageContext objects are particularly important in the development of JSP custom tags.

3.6. PageContext as a domain object

The PageContext object can be used as a container, so some data can be stored in the PageContext object.

Common methods for PageContext objects

       Java.lang.Object Findattribute (java.lang.String name)

Focus on the Findattribute method, which is used to find the attributes in each domain, the API for viewing this method can see a description of this method:
Searches for the named attribute on page, request, session (if valid), and application scope (s) in order and returns the V Alue associated or null.

When looking for an attribute, the Findattribute method finds the four objects in the lookup order "Page→request→session→application", returns the property value if found, and if none of the four objects finds the attribute to find, Returns a null.

Example: Using PageContext's Findattribute method to find property values

         The Findattribute method of PageContext finds the attribute value                                             Pagecontext.findattribute method finds: The Name1 property of the PageContext object: The Name2 property of the Request object: The Name3 property of the Session object: The Name4 property of the Application object: Find the Name5 property that does not exist:  output using an EL expression: The Name1 property of the PageContext object: ${name1} The Name2 property of the Request object: ${name2} The Name3 property of the Session object: ${name3} Application Object Name4 properties: ${ NAME4} non-existent Name5 property: ${name5}

Operation Result:

The EL expression statement, when executed, invokes the Pagecontext.findattribute method, using the identifier as the keyword, to find the corresponding object from page, request, session, and application four fields, and then returns the corresponding object. Returned "" (Note that it is not NULL, but an empty string).

Methods for accessing other domains are encapsulated in the PageContext object

  Java.lang.Object getattribute (java.lang.String name,   setAttribute (java.lang.String name, Java.lang.Object value ,   removeattribute (java.lang.String name, scope)

Constants that represent individual domains

    Pagecontext.page_scope

Example: PageContext access to other domains

         PageContext Accessing other domains                                 remove the attribute values stored in the SESSION object: The first approach: using Pagecontext.getattribute ("AttributeName", Pagecontext.session_ SCOPE); To remove the value name from the Session object: The second approach: use Session.getattribute ("AttributeName"); To remove the value name from the Session object:

3.7. PageContext introduction and jump to other resources

The PageContext class defines a forward method (for jumping pages) and two include methods (to introduce pages) to simplify and replace the Requestdispatcher.forward method and the Include method, respectively.
The resource received by the method starts with "/" and "/" represents the current web app.

Example: Jump to another page using PageContext's forward method

         Jump page using PageContext's forward method

The results of the operation are as follows:

1 Pagecontext.forward ("/pagecontextdemo05.jsp");

This notation is used to simplify and replace pagecontext.getrequest (). Getrequestdispatcher ("/pagecontextdemo05.jsp"). Forward (request, response); This kind of notation. In the actual development, using the Pagecontext.forward (Relativeurlpath) method to jump page is not much, mainly because of the JSP page nested Java code, so this approach is simple to understand, in development, To jump from one JSP page to another using a server-side jump, it is common to use the <jsp:forward> tag,<jsp:forward> tag to forward the request to another resource.

Example: Using the Include method of PageContext to introduce resources

        Using the Include method of PageContext to introduce resources

Operation Result:

In real-world development, using the PageContext include method to introduce pages is rarely used, and it is common to use jsp:include tags to introduce resources, so this approach can be understood.

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.