jsp built-in objects: use of pageContext scope objects
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
NO. | Built-in object | Type |
1 | pageContext | javax.servlet.jsp .PageContext |
2 | request | ##javax.servlet.http.HttpServletRequest |
response | ##javax.servlet.http.HttpServletResponse||
session | ##javax.servlet.http.HttpSession | 5 |
application | javax.servlet.ServletContext | 6 |
config | javax.servlet.ServletConfig | 7 |
javax.servlet.jsp.JspWriter | ##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 objects3.1. Page objectPage 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 working principle diagram of the out object 3.3, pageContext objectThe 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
3.5. The meaning of pageContext encapsulating other 8 built-in objectsIf 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 objectThe 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) Copy after login 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: 当要查找某个属性时,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} Copy after login 运行结果: 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) Copy after login 代表各个域的常量 PageContext.PAGE_SCOPE Copy after login 范例:pageContext访问其它域 pageContext访问其它域 取出存放在session对象中的属性值: 第一种做法:使用pageContext.getAttribute("attributeName",PageContext.SESSION_SCOPE);去取出session对象中值 姓名: 第二种做法:使用session.getAttribute("attributeName");去取出session对象中值 姓名: Copy after login 3.7、PageContext引入和跳转到其他资源 PageContext类中定义了一个forward方法(用来跳转页面)和两个include方法(用来引入页面)来分别简化和替代RequestDispatcher.forward方法和include方法。 范例:使用pageContext的forward方法跳转到其他页面 使用pageContext的forward方法跳转页面 Copy after login 运行结果如下: 1 pageContext.forward("/pageContextDemo05.jsp"); Copy after login 这种写法是用来简化和替代pageContext.getRequest().getRequestDispatcher("/pageContextDemo05.jsp").forward(request, response);这种写法的。在实际开发中,使用pageContext.forward(relativeUrlPath)方法跳转页面用得不多,主要是因为要在Jsp页面中嵌套java代码,所以这种做法简单了解一下即可,在开发中,要想从一个Jsp页面采用服务器端跳转的方式跳转到另一个Jsp页面,那么一般会使用 范例:使用pageContext的include方法引入资源 使用pageContext的include方法引入资源 Copy after login 运行结果: 在实际开发中,使用pageContext的include方法引入页面这种做法也很少用,一般都使用jsp:include标签引入资源,因此这种做法了解一下即可。 相关推荐: |
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.
