Java web learning summary -- nine built-in objects in JSP

1, JSP operation principle

When each JSP page is accessed for the first time, the WEB container will hand over the request to the JSP engine (i.e. a Java program) for processing. The JSP engine first translates JSP into a_ JspServlet (which is also a servlet in essence), and then call it according to the call mode of servlet.
Because the JSP will be translated into a servlet during the first access, the first access is usually slow, but the second access, if the JSP engine finds that the JSP has not changed, it will no longer translate, but call directly, so the execution efficiency of the program will not be affected.
The JSP engine is calling the corresponding JSP_ When jspServlet, nine Web development related objects will be passed or created for reference_ jspServlet uses. In order to facilitate developers to obtain the references of these web objects when writing JSP pages, the designer of JSP technology specially defines 9 corresponding variables. Developers can quickly obtain the references of these 9 objects through these variables in JSP pages.

2, Recognize nine built-in objects

NO. Built in object 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 and config have been described in detail above. Here we focus on the remaining pageContext objects, out objects and page objects.

3, Built in object instructions

3.1 page object

The page object represents the current JSP page, which can be understood as an object itself, that is, treat a JSP as an object. Page objects are rarely used in development. Just learn about them

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. Its function and usage are the same as servletresponse The PrintWriter object returned by the getwriter method is very similar.
The type of out object in JSP page is JspWriter, which is equivalent to a PrintWriter with caching function. Setting the buffer attribute of page instruction of JSP page can adjust its cache size or even close its cache.
The out object calls servletresponse only when the content is written to the out object and any of the following conditions are met Getwriter method and write the contents of the buffer of the out object into the buffer provided by the Servlet engine through the PrintWriter object returned by this method:

  • Setting the buffer attribute of the page instruction turns off the caching function of the out object
  • The buffer for the out object is full
  • The entire JSP page ends

Working principle diagram of out object


3.3. pageContext object

pageContext object is the most important object in JSP technology. It represents the running environment of JSP page. This object not only encapsulates the reference to other 8 implicit objects, but also a domain object (container) that can be used to save data. Moreover, this object also encapsulates some common operations often involved in web development, such as introducing and jumping other resources, retrieving attributes in other domain objects and so on.

3.4. Get other objects through pageContext

  • The getException method returns an implicit exception object
  • The getPage method returns the page implicit object
  • The getRequest method returns the request implicit object
  • The getResponse method returns the response implicit object
  • The getServletConfig method returns the config implicit object
  • The getServletContext method returns the application implicit object
  • The getSession method returns the session implicit object
  • The getOut method returns the out implicit object

3.5. pageContext encapsulates the meaning of other 8 built-in objects

If the pageContext object is passed to an ordinary Java object in the programming process, the Java object can obtain eight implicit objects. At this time, the Java object can interact with the browser. At this time, the Java object becomes a dynamic web resource. This is the meaning of the pageContext encapsulating the other eight built-in objects. Whoever passes the pageContext to can become a dynamic web resource, Then, under what circumstances do we need to pass pageContext to another Java class, and under what circumstances do we need to use this technology? In more formal development, java code is not allowed on jsp pages. If java code appears on jsp pages, we should find a way to remove the Java code. We can develop a user-defined tag to remove the Java code on jsp pages. First, write a Java class around the user-defined tag, When the jsp engine executes the custom tag, it will call the Java class written around the custom tag. When calling the Java class, it will pass the pageContext object to the Java class. Because the pageContext object encapsulates the reference to the other eight implicit objects, the eight implicit objects (request, response, config, application, exception, Session, page, out) in the jsp page can be used in this Java class, pageContext object is particularly important in jsp custom tag development.

3.6. pageContext as domain object

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

Common methods of pageContext object

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 attributes in various fields. Check the API of this method to see the description of this method:
  Searches for the named attribute in page, request, session (if valid), and application scope(s) in order and returns the value associated or null.

When searching for an attribute, the findAttribute method searches the four objects according to the search order "page → request → session → application". As long as it is found, it returns the attribute value. If none of the four objects find the attribute to be searched, it returns a null.

Example: use the findAttribute method of pageContext to find the attribute value

 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 <head>
 5     <title>pageContext of findAttribute Method to find a property value</title>
 6 </head>
 7 <%
 8     pageContext.setAttribute("name1", "Lonely Wolf");
 9     request.setAttribute("name2", "White tiger Emperor");
10     session.setAttribute("name3", "Xuantian evil emperor");
11     application.setAttribute("name4", "Exterminator");
12 %>
13 <%
14     //use pageContext of findAttribute Method to find a property because the obtained value is Object Type, so you must use String Forced downward transformation into String type
15     //lookup name1 Properties, in order"page→request→session→application"Look in these four objects
16     String refName1 = (String)pageContext.findAttribute("name1");
17     String refName2 = (String)pageContext.findAttribute("name2");
18     String refName3 = (String)pageContext.findAttribute("name3");
19     String refName4 = (String)pageContext.findAttribute("name4");
20     String refName5 = (String)pageContext.findAttribute("name5");//Find a property that does not exist
21 %>
22 <h1>pageContext.findAttribute Property value found by method:</h1>
23 <h3>pageContext Object name1 Properties:<%=refName1%></h3>
24 <h3>request Object name2 Properties:<%=refName2%></h3>
25 <h3>session Object name3 Properties:<%=refName3%></h3>
26 <h3>application Object name4 Properties:<%=refName4%></h3>
27 <h3>Find non-existent name5 Properties:<%=refName5%></h3>
28 <hr/>
29 <h1>use EL Expression for output:</h1>
30 <h3>pageContext Object name1 Properties: ${name1}</h3>
31 <h3>request Object name2 Properties: ${name2}</h3>
32 <h3>session Object name3 Properties: ${name3}</h3>
33 <h3>application Object name4 Properties: ${name4}</h3>
34 <h3>Nonexistent name5 Properties: ${name5}</h3>

Operation results:


When the EL expression statement is executed, pagecontext will be called The findattribute method uses the identifier as the keyword to find the corresponding object from the four fields of page, request, session and application respectively. If it is found, it returns the corresponding object, and if it is not found, it returns "" (note that it is not null, but an empty string).

The pageContext object encapsulates methods to access other domains

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)

Constant representing each field

4 PageContext.PAGE_SCOPE 

Example: pageContext accesses 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 <head>
 5     <title>pageContext Access other domains</title>
 6 </head>
 7 <%
 8     //This is equivalent to going to session Object name Attribute, equivalent to session.setAttribute("name","Lonely Wolf");
 9     pageContext.setAttribute("name","Lonely Wolf",PageContext.SESSION_SCOPE);
10 %>
11 <%
12     //obtain session Object properties, using pageContext Object acquisition
13     String refName1 = (String)pageContext.getAttribute("name",PageContext.SESSION_SCOPE);  
14     //Because the value obtained is Object Type, so you must use String Forced downward transformation into String type
15      String refName2 = (String)session.getAttribute("name");  
16 %>
17 <h1>Take out and store in session Property values in object:</h1>
18 <p>The first way: use pageContext.getAttribute("attributeName",PageContext.SESSION_SCOPE);Take it out session Object median</p>
19 <h3>full name:<%=refName1%></h3>
20 <p>The second approach: use session.getAttribute("attributeName");Take it out session Object median</p>
21 <h3>full name:<%=refName2%></h3>


3.7. PageContext introduction and jump to other resources

PageContext class defines a forward method (used to jump to the page) and two include methods (used to introduce the page) to simplify and replace requestdispatcher Forward method and include method.
If the resource received by the method starts with "/", "/" represents the current web application.

Example: use the forward method of pageContext to jump to other pages

 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 <head>
 5     <title>use pageContext of forward Method jump page</title>
 6 </head>
 7 <%
 8     //use pageContext of forward Method jump to pageContextDemo05.jsp Page,/Represents the current web application
 9     pageContext.forward("/pageContextDemo05.jsp");
10     //use pageContext.forward(relativeUrlPath)replace RequestDispatcher.forward(relativeUrlPath)
11     //use RequestDispatcher of forward Jump mode implemented by method
12     //pageContext.getRequest().getRequestDispatcher("/pageContextDemo05.jsp").forward(request, response);
13 %>

The operation results are as follows:

1 pageContext.forward("/pageContextDemo05.jsp");

This writing method is used to simplify and replace pagecontext getRequest(). getRequestDispatcher("/pageContextDemo05.Jsp"). forward(request, response); This way of writing is. In actual development, pagecontext The forward (relativeurlpath) method is rarely used to jump pages, mainly because java code needs to be nested in Jsp pages, so this method can be simply understood. In development, if you want to jump from one Jsp page to another by server-side jump, you will generally use the < Jsp: forward > tag, which is used to forward requests to another resource.

Example: use the include method of pageContext to introduce resources

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 3 <head>
 4     <title>use pageContext of include Method to introduce resources</title>
 5 </head>
 6 <%
 7     pageContext.include("/jspfragments/head.jsp");
 8 %>
 9     use pageContext of include Method to introduce resources
10 <%
11     pageContext.include("/jspfragments/foot.jsp");
12 %>
13 <hr/>
14 <%--
15 <jsp:include page="/jspfragments/head.jsp"/>
16 use jsp:include Tag import resource
17 <jsp:include page="/jspfragments/foot.jsp"/>
18 --%>

Operation results:


In actual development, it is rarely used to use the include method of pageContext to introduce pages. Generally, jsp:include tag is used to introduce resources, so you can understand this method.

Posted by kevdoug on Sat, 14 May 2022 04:40:42 +0300