Home >Java >javaTutorial >jsp built-in objects: use of pageContext scope objects

jsp built-in objects: use of pageContext scope objects

php是最好的语言
php是最好的语言Original
2018-08-08 16:10:188203browse

What built-in objects does JSP have? What are their functions? This article mentioned the nine built-in objects of JSP, including a detailed introduction to the pageContext object. The pageContext object is the most important object in JSP technology. It represents the running environment of the JSP page. This object not only encapsulates In addition to the references to the other eight implicit objects, it is also a domain object (container) itself, which can be used to save data. Other objects can also be obtained through pageContext. The specific use is explained below.

1. JSP operating 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) to process. The JSP engine first translates the JSP into a _jspServlet (essentially a servlet), and then calls it according to the servlet calling method.
Since JSP will be translated into servlet when accessed for the first time, the first access will usually be slower. However, on the second access, if the JSP engine finds that the JSP has not changed, it will no longer translate it, but call it directly, so The execution efficiency of the program will not be affected.
When the JSP engine calls the _jspServlet corresponding to JSP, it will pass or create 9 objects related to web development for _jspServlet to use. In order to facilitate developers to obtain references to these web objects when writing JSP pages, the designers of JSP technology specifically defined 9 corresponding variables. Developers can quickly obtain references to these 9 objects in JSP pages through these variables.

2. Get to know the nine built-in objects

3##javax.servlet.http.HttpServletResponse4session567outjavax.servlet.jsp.JspWriterpageexception
NO. Built-in object Type
1 pageContext javax.servlet.jsp .PageContext
2 request ##javax.servlet.http.HttpServletRequest
response
##javax.servlet.http.HttpSession
application javax.servlet.ServletContext
config javax.servlet.ServletConfig
##8
java.lang.Object 9
java.lang.Throwable

 

The request, response, session, application, and config objects have been introduced in detail before. Here we focus on the remaining pageContext objects, out objects, and page objects.

3. Instructions for using built-in objects

3.1. Page object

Page object represents the current JSP page , can be understood as an object itself, that is: treating a JSP as an object. The page object is rarely used in development, just understand it

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 very similar to the PrintWriter object returned by the ServletResponse.getWriter method.
The type of out object in the JSP page is JspWriter. JspWriter is equivalent to a PrintWriter with caching function. Setting the buffer attribute of the page instruction of the JSP page can adjust its cache size or even close its cache.
Only when content is written to the out object and any of the following conditions is met, the out object calls the ServletResponse.getWriter method, and the content in the buffer of the out object is actually written through the PrintWriter object returned by this method. Enter the buffer provided by the Servlet engine:

  • Set the buffer attribute of the page instruction to turn off the caching function of the out object

  • The out object The buffer is full

  • The entire JSP page has ended

The working principle diagram of the out object

 jsp built-in objects: use of pageContext scope objects

3.3, pageContext object

The pageContext object is the most important object in JSP technology. It represents the running environment of the JSP page. This object not only encapsulates references to the other eight implicit objects, but also itself It is 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 to other resources, retrieving attributes in other domain objects, etc.

3.4. Obtain other objects through pageContext

  • getException method returns exception implicit object

  • getPage method returns page implicit Object

  • getRequest method returns request implicit object

  • getResponse method returns response implicit object

  • getServletConfig method returns the config implicit object

  • getServletContext method returns the application implicit object

  • getSession method returns the session implicit object

  • The getOut method returns the out implicit object

3.5. The meaning of pageContext encapsulating other 8 built-in objects

If during the programming process, Pass the pageContext object to an ordinary java object, then this java object will be able to obtain 8 implicit objects. At this time, this java object can interact with the browser, and this java object will become a dynamic web resource. This is the meaning of pageContext encapsulating other 8 built-in objects. Whoever you pass pageContext to can become a dynamic web resource. So under what circumstances does it need to pass pageContext to another java class? Under what circumstances does this technology need to be used? Well, in more formal development, java code is not allowed to appear on the jsp page. If java code appears on the jsp page, then we should find a way to remove the java code. We can develop a custom tag to remove the jsp For the java code on the page, first write a java class around the custom 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. For this java class, since the pageContext object encapsulates references to the other eight implicit objects, the eight implicit objects in the jsp page (request, response, config, application, exception, Session) can be used in this java class. , page, out), 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.

Commonly used methods of pageContext objects

       java.lang.Object findAttribute(java.lang.String name)

Focus on the findAttribute method. This method is used to find attributes in each domain. View The API of this method 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.

  当要查找某个属性时,findAttribute方法按照查找顺序"page→request→session→application"在这四个对象中去查找,只要找到了就返回属性值,如果四个对象都没有找到要查找的属性,则返回一个null。

范例:使用pageContext的findAttribute方法查找属性值

 
 
 
 
     pageContext的findAttribute方法查找属性值
 
 
     
 
                                    
 pageContext.findAttribute方法查找到的属性值:
 pageContext对象的name1属性:
 request对象的name2属性:
 session对象的name3属性:
 application对象的name4属性:
 查找不存在的name5属性:
 
 使用EL表达式进行输出:
 pageContext对象的name1属性:${name1}
 request对象的name2属性:${name2}
 session对象的name3属性:${name3}
 application对象的name4属性:${name4}
 不存在的name5属性:${name5}

运行结果:

  EL表达式语句在执行时,会调用pageContext.findAttribute方法,用标识符为关键字,分别从page、request、 session、application四个域中查找相应的对象,找到则返回相应对象,找不到则返回”” (注意,不是null,而是空字符串)。

pageContext对象中封装了访问其它域的方法

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

代表各个域的常量

    PageContext.PAGE_SCOPE

范例:pageContext访问其它域

 
 
 
 
     pageContext访问其它域
 
 
       
 
                      
 取出存放在session对象中的属性值:
 第一种做法:使用pageContext.getAttribute("attributeName",PageContext.SESSION_SCOPE);去取出session对象中值
 姓名:
 第二种做法:使用session.getAttribute("attributeName");去取出session对象中值
 姓名:  

3.7、PageContext引入和跳转到其他资源

  PageContext类中定义了一个forward方法(用来跳转页面)和两个include方法(用来引入页面)来分别简化和替代RequestDispatcher.forward方法和include方法。
  方法接收的资源如果以“/”开头, “/”代表当前web应用。

范例:使用pageContext的forward方法跳转到其他页面

 
 
 
 
     使用pageContext的forward方法跳转页面

运行结果如下:

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

  这种写法是用来简化和替代pageContext.getRequest().getRequestDispatcher("/pageContextDemo05.jsp").forward(request, response);这种写法的。在实际开发中,使用pageContext.forward(relativeUrlPath)方法跳转页面用得不多,主要是因为要在Jsp页面中嵌套java代码,所以这种做法简单了解一下即可,在开发中,要想从一个Jsp页面采用服务器端跳转的方式跳转到另一个Jsp页面,那么一般会使用标签,标签用于把请求转发给另外一个资源。

范例:使用pageContext的include方法引入资源

 
 
 
     使用pageContext的include方法引入资源

运行结果:

   在实际开发中,使用pageContext的include方法引入页面这种做法也很少用,一般都使用jsp:include标签引入资源,因此这种做法了解一下即可。

相关推荐:

JSP的内部对象

js的内置对象详解

The above is the detailed content of jsp built-in objects: use of pageContext scope objects. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn