javaweb Learning -- Nine Built-in Objects in JSP

Posted by chetanmadaan on Fri, 02 Aug 2019 11:56:13 +0200

1 JSP Running Principle

When each JSP page is first accessed, the web container handles the request to a JSP engine (that is, a java program). The JSP engine first translates the JSP into a _jsp Servlet (actually a Servlet), and then installs the call of the Servlet.

Because the first access will be translated into Servlet, the first visit is usually slow, but the second visit, if the JSP engine finds that the JSP has not changed, it will not translate, but call directly, and the execution efficiency of all programs will not be affected.

When the JSP engine calls the corresponding _jspServlet of JSP, it passes or creates nine objects related to web development to provide _jspServlet for use. In order to facilitate developers to get the references of these web objects when they write JSP pages, the designer of JSP technology has deliberately defined nine corresponding variables, so developers can quickly get the references of these nine objects through these variables in JSP pages.

2 JSP 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

3 Instructions for the Use of Built-in Objects

3.1 page object

The page object represents the current JSP page and can be understood as an object itself, that is, to treat the JSP page as an object. Page objects are hardly needed in development, so you can understand them.

3.2 out objects

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 very similar to the PrintWriter object returned by the ServletResponse.getWriter method.

The type of out object of JSP page is JspWriter. JspWriter is equivalent to a PrintWriter with retardation function. Setting the buffer attribute of page instruction of JSP page can adjust its cache size or even close its cache.

Only if you want to write content in the out object and satisfy any of the following conditions, the out object calls the ServletResponse.getWriter method, and returns the PrintWriter object to write the content of the out object buffer into the buffer provided by the Servlet engine:

  • Setting the buffer attribute of the page instruction closes the caching function of the out object

  • Buffer of out object is full

  • The entire JSP page ends

 

3.3 pageContext object

Page Context object is the most important object in JSP technology. It represents the running environment of JSP pages. This object not only encapsulates references to eight other implicit objects, but also is a domain object (container) itself, which can be used to save data. Moreover, this object 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 to the implicit emperor town of exception
  • The getPage method returns the page implicit object
  • The getRequest method returns the request implicit object
  • The getResponse method returns the Response implicit object
  • getServletConfig method returns config implicit object
  • The getServletContext method returns the application implicit object
  • The getOut method returns out implicit objects

3.5 pageContext encapsulates the meaning of eight other built-in objects

If the pageContext object is passed to a common java object in the programming process, the object will be able to acquire 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 pageContext encapsulating eight other built-in objects. If you pass pageContext to anyone, who can become a dynamic web resource, then under what circumstances you need to pass pageContext to another java class, and under what circumstances you need to use this technology. In more formal development, JSP pages are not allowed to appear java code, if the JSP pages have java generation. Code, then try to remove the java object, we can develop a custom tag to remove the java code on the JSP page, first write a java class around the custom tag, the JSP engine will call the java class written around the custom tag when executing the custom tag, and will call the java class when calling the java class. The pageContext object is passed to the java class. Because the pageContext object encapsulates the references of the other eight implicit objects, the eight implicit objects in the JSP page can be used in this java class. The pageContext object is 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.

public void setAttribute(java.lang.String name,java.lang.Object value)
public java.lang.Object getAttribute(java.lang.String name)
public void removeAttribute(java.lang.String name)
public java.lang.Object findAttribute(java.lang.String name)

Focus on the findAttribute method, which is used to find attributes in various domains. Looking at the API of this method, you can 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 an attribute is to be found, the findAttribute method searches in four objects in the order of "page - > request - > session - > application" and returns the attribute value as soon as it is found, and null if none of the four objects is found.

Example: Find attribute values using pageContext's findAttribute method

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>pageAttribute Of findAttribute Method Find Attribute Value</title>
</head>
<body>
	<%
		pageContext.setAttribute("name1", "java");
		request.setAttribute("name2", "sql");
		session.setAttribute("name3", "js");
		application.setAttribute("name4", "springMVC");
	
	%>
	<%
		//findAttribute method of pageContext is used to find attributes. Because the value obtained is Object type, String must be used to force rural transformation.
		//Find the name attribute in the order of "page - > request - > session - > application" in these four objects.
		String refName1 = (String)pageContext.findAttribute("name1");
		String refName2 = (String)pageContext.findAttribute("name2");
		String refName3 = (String)pageContext.findAttribute("name3");
		String refName4 = (String)pageContext.findAttribute("name4");
		String refName5 = (String)pageContext.findAttribute("name5");//Find an attribute that does not exist
		
	%>
	<h1>pageContext.findAttribte Method Find Attribute Value</h1>
	<h3>pageContext Object name1 Attributes:<%=refName1 %></h3>
	<h3>request Object name2 Attributes:<%=refName2 %></h3>
	<h3>session Object name3 Attributes:<%=refName3 %></h3>
	<h3>application Object name4 Attributes:<%=refName4 %></h3>
	<h3>Find nonexistent name5 Attributes:<%=refName5 %></h3>
	<hr/>
	<h1>Use EL Expressions are output</h1>
	<h3>pageContext Object name1 Attributes: ${name1} </h3>
	<h3>request Object name1 Attributes: ${name2} </h3>
	<h3>session Object name1 Attributes: ${name3} </h3>
	<h3>application Object name1 Attributes: ${name4} </h3>
	<h3>Nonexistent name5 Attributes: ${name5} </h3>
	
	
	
</body>
</html>

Operation results:

When an EL expression statement is executed, it calls the pageContext.findAttribute method, and uses the identifier as the keyword to find the corresponding object from the four fields of page, request, session and application, and returns the corresponding object when it is found. If it is not found, it returns "(Note that it is not null, but empty string).

The pageContext object encapsulates methods for accessing other domains:

public java.lang.Object getAttribute(java.lang.String name,int scope)
public void setAttribute (java.lang.String name,java.lang.Object value,int scope)
public void removeAttribute(java.lang.String name,int scope)

Constants representing domains

PageContext.APPLICATION_SCOPE
PageContext.SESSION_SCOPE
PageContext.REQUEST_SCOPE
PageContext.PAGE_SCOPE

Example: Other domains of the PageContext paradigm

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>pageContext Access to other domains</title>
</head>
<body>
	<%
		//This is equivalent to storing one of those attributes in the session object, which is equivalent to session.setAttribute("name","java learning").
		pageContext.setAttribute("name", "javaweb Study",PageContext.SESSION_SCOPE);
	%>
	<%
		//Get the properties of the session object, using the pageContext object
		String refName = (String)pageContext.getAttribute("name",PageContext.SESSION_SCOPE);
		//Since the value is of Object type, you must use String to force the downward transition to String type
		String refName2 = (String)session.getAttribute("name");
	%>
	<h1>Remove and store in session Attribute values in objects</h1>
	<p>The first approach is to use pageContext.getAttribute("attribute",PageContext.SESSION_SCOPE);take out session Object median</p>
	<h3>Full name:<%=refName %></h3>
	<p>The second approach is to use session.getAttribute("attribute");take out session Values in objects</p>
	<h3>Full name:<%=refName2 %></h3>
	
</body>
</html>

Operation results:

3.7 PageContext Introduces and Jumps Other Resources

The PageContext class defines a forward method (used to jump pages) and two include methods (used to introduce pages) to simplify and replace the RequesDispatcher.foward method and the include method, respectively.

If the resource received by the method starts with "/", "/" represents the current web application.

Example: Use the forward method of PageContext to jump pages

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Use pageContext Of forward Method jump page</title>
</head>
<body>
<%
	//Use the forward method of page to jump to pageContextDemo.jsp page, / representing the current web application
	pageContext.forward("/pageContextDemo.jsp");
	//Replace RequestDispatcher.forward() with pageContext.forward()
	//Jump implemented by forward method of RequestDispatcher
	//pageContext.getRequest().getRequestDispatcher("/pageContextDemo.jsp").forward(request,response);
%>
</body>
</html>

This writing is mainly to simplify and replace pageContext.getRequest().getRequestDispatcher("/pageContextDemo.jsp").forward(request,response); this writing. Page Context. forward () method is rarely used in actual development, mainly because java code is nested in JSP pages, so this method is easy to understand. In development, if you want to jump from one JSP page to another JSP page by server jump, you will generally use < jsp. The < jsp: forward > tag is used to send requests to another resource.

Example: Introducing resources using page Context's include method

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Use pageContext Of include Method Introducing Resources</title>
</head>
<body>
<%
	pageContext.include("/jspfragments/head.jsp");
%>
//Introducing resources using page Context's include method
<%
	pageContext.include("/jspfragments/foot.jsp");
%>
</body>
</html>

Operation results:

In practical development, the include method of pageContext is seldom used to introduce pages. Generally, jsp:forward tag is used to introduce resources, so this method can be understood.

 

Topics: JSP Java Session Attribute