Javaweb---Summary (17) nine built-in objects in a JSP

Source: Internet
Author: User

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.

Iii. built-in object Usage Description 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

1 public void SetAttribute (java.lang.String name,java.lang.object value)
2 public java.lang.Object getattribute (java.lang.String name)
3 public void RemoveAttribute (java.lang.String name)
4 Public 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

1 <% @page contenttype= "Text/html;charset=utf-8"%>
2 <% @page import= "java.util.*"%>
3 <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
4 5 <title>pagecontext Findattribute Method Find attribute value </title>
6 7 <%
8 Pagecontext.setattribute ("name1", "aloof pale Wolf");
9 Request.setattribute ("Name2", "White Tiger God Emperor");
Ten Session.setattribute ("Name3", "Xuan Tian Evil Emperor");
One Application.setattribute ("Name4", "The Demon of the World");
%>
<%
14//Use PageContext's Findattribute method to find the attribute, because the obtained value is of type object, so you must use string to force a downward transformation into a string type
15//Find Name1 property, in order "page→request→session→application" in these four objects to find
String refName1 = (string) pagecontext.findattribute ("name1");
RefName2 String = (string) pagecontext.findattribute ("name2");
String refName3 = (string) pagecontext.findattribute ("Name3");
String refName4 = (string) pagecontext.findattribute ("Name4");
String refName5 = (string) pagecontext.findattribute ("Name5");//Find a property that does not exist
%>
The The Name1 property of the The Name2 property of the The Name3 property of the The Name4 property of the The Name1 property of the The Name2 property of the The Name3 property of the The Name4 property of the Name5 attribute:${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

1 public java.lang.Object getattribute (java.lang.String name,int scope)
2 public void SetAttribute (java.lang.String name, Java.lang.Object value,int scope)
3 public void RemoveAttribute (java.lang.String name,int scope)

Constants that represent individual domains

1 Pagecontext.application_scope
2 Pagecontext.session_scope
3 Pagecontext.request_scope

Example: PageContext access to other domains

1 <% @page contenttype= "Text/html;charset=utf-8"%>
2 <% @page import= "java.util.*"%>
3 <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
4 5 <title>pagecontext Access other domains </title>
6 7 <%
8//At this time equivalent to the session object stored in a name attribute, equivalent to Session.setattribute ("name", "Aloof Wolf");
9 Pagecontext.setattribute ("name", "Aloof pale Wolf", pagecontext.session_scope);
Ten%>
<%
12//Get Properties of Session object, use PageContext object to get
String refName1 = (string) pagecontext.getattribute ("name", Pagecontext.session_scope);
14//Because the obtained value is of type object, you must use string to force a downward transformation into a string type
String refName2 = (string) session.getattribute ("name");
%>17 <p> The first approach: use Pagecontext.getattribute ("AttributeName", Pagecontext.session_scope), to remove the value of the SESSION object </p >
<p> second approach: Use Session.getattribute ("AttributeName") to remove the value of the Session object </p>

  

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





5


8
9 Pagecontext.forward ("/pagecontextdemo05.jsp");
//Use Pagecontext.forward (Relativeurlpath) to replace Requestdispatcher.forward (Relativeurlpath)
One //use RequestDispatcher's forward method to achieve the jump mode
//pagecontext.getrequest (). Getrequestdispatcher ("/pagecontextdemo05.jsp"). Forward (request, response);
%>

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




4


7
9 introducing resources using the Include method of PageContext
Ten <%
pagecontext.include ("/jspfragments/foot.jsp");
%>
<%--
<jsp:include page= "/jspfragments/head.jsp"/>
16 using Jsp:include tags to introduce resources
<jsp:include page= "/jspfragments/foot.jsp"/
--%>

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.

Javaweb---Summary (17) nine built-in objects in a JSP

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.