pageContext
object
pageContext
Objects are one JSP
of the most important objects in technology.
PageContext
Instances provide access to allJSP
namespaces associated with a page , provide access to multiple page properties , and provide a layer of implementation detail. Instances are obtained by the implementing class by calling a method , and released by calling.
PageContext
JspFactory.getPageContext()
JSP
JspFactory.releasePageContext()
pageContext
Representing JSP
the running environment of the page, this object not only encapsulates references8
to other large implicit objects , it is also a domain object ( ) itself, which can be used to store data . 1. Domain object functions 2. Proxy other domain objects 3. Get other built-in objects容器
PageContext
The class mainly describes the JSP
context environment of the page, servlet
the information that can be obtained, and the current JSP
context environment can also be passed to the specified class to realize JSP
the operation of the page.
This object also encapsulates web
some common operations that are often involved in development
For example: importing and jumping to other resources, retrieving properties in other domain objects, etc.
by pageContext
getting other objects
method | return |
---|---|
getException() |
return exception implicit object |
getPage() |
return page implicit object |
getRequest() |
return request implicit object |
getResponse() |
return response implicit object |
getServletConfig() |
method returns config implicit object |
getServletContext() |
method returns application implicit object |
getSession() |
return session implicit object |
getOut () |
return out implicit object |
Implicit objects are automatically added to pageContext
.
<%
ServletRequest request2 = pageContext.getRequest() ;
ServletResponse response2 = pageContext.getResponse();
HttpSession session2 = pageContext.getSession();
ServletConfig config2 = pageContext.getServletConfig();
//servletConfig.getServletName();
ServletContext pageContext2 = pageContext.getServletContext();
Object page2 = pageContext.getPage();
Exception exception = pageContext.getException();
JspWriter out2 = pageContext.getOut();
%>
pageContext
Encapsulates the essential function 8
of an object
It is convenient to interact with the browser. When you need a common java
class to process JSP
page data, you can directly pass the PageContext
class to it.
which class to pageContext
pass to, the class will become a dynamic web
resource
In more formal development, code jsp
is not allowed on the page java
. If there is code on the jsp
page java
, then you should find a way to java
remove the code.
We can develop a custom tag to remove the code
jsp
on the pagejava
java
First write a class around the custom tag , the engine will call the class written around the custom tagjsp
when executing the custom tagjava
- Objects are passed to this class when
java
the class is calledpageContext
java
- Since the
pageContext
object encapsulates8
references to other large implicit objects, the large implicit objects in the pagejava
can be used in this classjsp
8
request,response,config,application,exception,Session,page,out
- Can do all the basic operations on the
jsp
page
pageContext
Objects jsp
are especially important in custom label development.
Commonly used in the use of custom labels.
pageContext
when used for storage
pageContext
Objects can be used as containers, so some data can be stored in pageContext
objects.
pageContext
Common methods of 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)
domain object
Objects that can storesetAttribvute()/getAttribute()
and retrieve data using methods are called domain objects.
domain object | The life cycle |
---|---|
page | valid on the current page |
request | request forwarding |
session | Default half an hour |
application | when the server shuts down |
3. Set and get different domain properties (emphasis)
<%-- 给不同的域对象设置属性-pageContext --%>
<%
pageContext.setAttribute("page","this is current page");
pageContext.setAttribute("name","lucy",PageContext.REQUEST_SCOPE);
pageContext.setAttribute("age","38",PageContext.SESSION_SCOPE);
String likes[] = {"football","sleep","basketball"};
pageContext.setAttribute("likes",likes,PageContext.APPLICATION_SCOPE);
%>
<%-- 获取域属性 --%>
<%= pageContext.getAttribute("page") %><br/>
<%= pageContext.getAttribute("name",PageContext.REQUEST_SCOPE) %><br/>
<%= pageContext.getAttribute("age",PageContext.SESSION_SCOPE) %><br/>
<%= Arrays.toString( (String[])pageContext.getAttribute("likes",PageContext.APPLICATION_SCOPE) ) %><br/>
Summarize:
The specified domain that can be displayed when pageContext
setting and getting the domain attribute is used. If no domain is specified, the default domain is page域
.
In actual development,
设置属性
and获取属性
are programs developed by different
programmers , so what should be done if the developer does not know which domain such attribute names are stored in when obtaining them?
Solution: findAttribute
method, this method is used to find attributes in each domain
When looking for an attribute, the findAttribute
method searches page→request→session→application
in these four objects according to the search order, and returns the attribute value as long as it is found . If none of the four objects finds the attribute to be looked for, it returns one null
.
Example: Using pageContext
a findAttribute
method to find a property value
<%= pageContext.findAttribute("test") %>
By default, the statement page``request``session``application
searches the required properties one by one, and returns directly if found.
Therefore, the statement is EL
the underlying implementation principle of the expression.
Code example:
<%@page contentType="text/html;charset=UTF-8"%>
<%@page import="java.util.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<head>
<title>属性值查找</title>
</head>
<%
pageContext.setAttribute("n1", "n1");
request.setAttribute("n2", "n2");
session.setAttribute("n3", "n3");
application.setAttribute("n4", "n4");
%>
<%
// 通过 pageContext 来进行 全属性 的查找
//使用pageContext的findAttribute方法查找属性,由于取得的值为Object类型,因此必须使用String强制向下转型,转换成String类型
//查找n1属性,按照顺序"page→request→session→application"在这四个对象中去查找
String n1 = (String)pageContext.findAttribute("n1");
String n2 = (String)pageContext.findAttribute("n2");
String n3 = (String)pageContext.findAttribute("n3");
String n4 = (String)pageContext.findAttribute("n4");
//查找一个不存在的属性
String n5 = (String)pageContext.findAttribute("null");
%>
<h1>pageContext.findAttribute方法查找到的属性值:</h1>
<h3>pageContext对象的n1属性:<%=n1%></h3>
<h3>request对象的n2属性:<%=n2%></h3>
<h3>session对象的n3属性:<%=n3%></h3>
<h3>application对象的n4属性:<%=n4%></h3>
<h3>不存在的n5属性:<%=n5%></h3>
<hr/>
<h1>使用EL表达式进行输出:</h1>
<h3>pageContext对象的n1属性:${n1}</h3>
<h3>request对象的n2属性:${n2}</h3>
<h3>session对象的n3属性:${n3}</h3>
<h3>application对象的n4属性:${n4}</h3>
<h3>不存在的n5属性:${n5}</h3>