Java Web Review Summary (6) - jsp - built-in object - the most important object - pageContext (to be added)

pageContextobject

  pageContextObjects are one JSPof 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.
  PageContextJspFactory.getPageContext()JSPJspFactory.releasePageContext()

  pageContextRepresenting JSPthe 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容器


PageContextThe class mainly describes the JSPcontext environment of the page, servletthe information that can be obtained, and the current JSPcontext environment can also be passed to the specified class to realize JSPthe operation of the page.

  
This object also encapsulates websome common operations that are often involved in development

For example: importing and jumping to other resources, retrieving properties in other domain objects, etc.

by pageContextgetting other objects

method return
getException() return exceptionimplicit object
getPage() return pageimplicit object
getRequest() return requestimplicit object
getResponse() return responseimplicit object
getServletConfig() method returns configimplicit object
getServletContext() method returns applicationimplicit object
getSession() return sessionimplicit object
getOut() return outimplicit 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();

%>

pageContextEncapsulates the essential function 8of an object

It is convenient to interact with the browser. When you need a common javaclass to process JSPpage data, you can directly pass the PageContextclass to it.

which class to pageContextpass to, the class will become a dynamic webresource

In more formal development, code jspis not allowed on the page java. If there is code on the jsppage java, then you should find a way to javaremove the code.

We can develop a custom tag to remove the code jspon the pagejava

  1. javaFirst write a class around the custom tag , the engine will call the class written around the custom tag jspwhen executing the custom tagjava
  2. Objects are passed to this class when javathe class is calledpageContextjava
  3. Since the pageContextobject encapsulates 8references to other large implicit objects, the large implicit objects in the page javacan be used in this classjsp8request,response,config,application,exception,Session,page,out
  4. Can do all the basic operations on the jsppage
pageContextObjects jspare especially important in custom label development.

Commonly used in the use of custom labels.


pageContextwhen used for storage

  pageContextObjects can be used as containers, so some data can be stored in pageContextobjects.

pageContextCommon 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 pageContextsetting 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: findAttributemethod, this method is used to find attributes in each domain

  When looking for an attribute, the findAttributemethod searches page→request→session→applicationin 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 pageContexta findAttributemethod to find a property value

<%=  pageContext.findAttribute("test") %>

By default, the statement page``request``session``applicationsearches the required properties one by one, and returns directly if found.

Therefore, the statement is ELthe 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>

Later, it will be explained in depth in the development of custom tags.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325807731&siteId=291194637