Explanation of JSP built-in objects and EL expressions and related issues

The built-in objects of SP are for convenience. When developing JSP, some objects are set as built-in objects, and developers can use them directly without declaring these objects when programming JSP pages.
In this article, we will specifically explain the use of Jsp built-in objects and EL expressions.

1. JSP built-in objects (9 JSP built-in objects)
JSP built-in objects refer to the type corresponding to the name
request HttpServletRequest
response HttpServletResponse
session HttpSession (with switch: the value of the session attribute of the page instruction)
application ServletContext
config ServletConfig
page this (currently Servlet object)
exception java.lang.Throwable (with switch: the isErrorPage attribute of the page instruction is changed to true)
out JspWriter
pageContext javax.servlet.jsp.PageContext is very important The
pageContext object has three functions:
1. It is a domain object, representing The domain scope is this page.
At the same time, it can also manipulate other three domain objects (PageContext, ServletRequest, HttpSession, ServletContext)
to set attributes:
  void setAttribute(String name,Object value)
  void removeAttribute(String name)
  Object getAttribute(String name)
Operate the other three domain objects and set attributes:
  void setAttribute(String name,Object value,int scope)
  void removeAttribute(String name,int scope)
  Object getAttribute(String name,int scope)
  The parameter int scope is provided by the PageContext class Static variables are specified.
  PageContext.PAGE_SCOPE: page scope (it is the Map in PageContext itself, codenamed page)
  PageContext.REQUEST_SCOPE: request scope (is the Map in ServletRequest, codenamed request)
  PageContext.SESSION_SCOPE: request scope (is the Map in HttpSession, codenamed session)
  PageContext.APPLICATION_SCOPE: The request scope (it is the Map in the ServletContext, codenamed application)
Object findAttribute(String name): Search for the object with the specified name according to the scope of page, request, session and application in turn, until you find it.
EL expression is to call this method (very useful)
2. Get other 8 implicit objects
3. Provide convenient methods for forwarding and inclusion 
 If you do not use the pageContext object:
  RequestDispatcher rd = request.getRequestDispatcher("/url");
  rd.forward(request,response);
 use pageContext object:
  pageContext.forward("url");
  pageContext.include("url");
four domain objects (two Data transfer between resources)
Implicit object name scope name in JSP Specific type
pageContext page javax.servlet.jsp.PageContext
request request javax.servlet.ServletRequest
session session javax.servlet.http.HttpSession
application application javax.servlet. ServletContext (if used, must be processed synchronously)
Second, EL expression
It is just an expression in JSP, not a development language.
Basic syntax: ${EL expression}
1. Obtaining data
EL expressions can only obtain data in the four major fields.
If the object obtained by the EL expression is null, the page will not display the data.
Therefore, the null pointer exception "." operator will never occur in EL expressions :
${p.
[] operator:
(. operator can do, [] can also do. [] can do, . may not do)
such as ${p.name}===${p['name']} ==${p["name"]}
is excellent in that you can take things that do not conform to the Java naming convention.
2. Mathematical logic operation:
empty operator:
If the judged object is null or empty string, it will return true.
For collections, returns true even if the collection object itself is not null and has no elements.
EL expressions do not support string concatenation operations.

3. EL built-in objects (11 major EL built-in objects)
to obtain JSP built-in objects (11 major EL built-in objects): Difficulty, do not confuse with JSP's built-in objects and scope names
Among the 11 major EL implicit objects, one of which is to represent In addition to its own object, the rest are represented by the Map structure
EL implicit object name Java type remarks
pageContext javax.servlet.jsp.PageContext is exactly the same as the built-in object in JSP The
rest are the representative Map collection
pageScope java.util.Map represents The Map requestScope java.util.Map representing the page scope of the PageContext represents the Map
of the ServletRequest request scope
sessionScope java.util.Map represents the Map of the HttpSession session scope
applicationScope java.util.Map represents the ServletContext application scope that Map
param java.util.Map represents the request parameters. key: The name of the request parameter. value: The value of the request parameter, which is a string.
paramValues ​​java.util.Map represents request parameters. key: The name of the request parameter. value: The value of the request parameter, which is an array of strings.
The header java.util.Map represents the request header. key: header name. value: The header value, which is a string.
headerValues ​​java.util.Map represents the request headers. key: header name. value: The header value, which is an array of strings.
cookie java.util.Map Represents a Map of cookies submitted by the client. key: The name of the cookie. value: cookie object itself
initParam java.util.Map represents the global initialization parameter (context-param in web.xml).key: parameter name. value: parameter value


EL Expressions and JSP Built-in Objects Question

Q:
Is there a difference between ${pageContex.request} and ${request}? Are the requests here all of the HttpServletRequest class?
I have request.setAttribute("key",key) in Action;
when the key variable is a collection, I can get it with ${key} in jsp, but when the key is a string, ${key} is null, why?
Also what is the difference between Attribute and Parameter? Can they have the same name? (For example, there is a "key" in Parameter, and there is also a "key" in Attribute.) If
possible, can it be distinguished in EL? Which one to find first?
Reply 1:
There is a difference .
The request object has several internal information, one of which is the attribute(key, value) queue.
${pageContex.request} represents the request object itself, which can get all the information of the request.
${request} represents the attribute(key, value) queue inside the request object, and only the information in the queue can be obtained.
Reply 2:
When the key variable is a collection, it can be obtained with ${key} in jsp, but the key is a character String ${key} is null, why?
You can get it on the page <%request.getparameter('key')>!
What is the difference between Attribute and Parameter:
getParameter(String name) Gets the parameter value sent by the client to the server, the parameter is specified by name, usually the parameter in the form.
getAttribute(String name): Returns the attribute value specified by name, if the specified attribute value is not If it exists, it will return a null value.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326944697&siteId=291194637