JSP built-in objects and El expressions and related issues to explain

Source: Internet
Author: User
Tags naming convention

one, JSP built-in objects (9 JSP built-in objects)

The type of the JSP's built-in object reference name

Request HttpServletRequest
Response HttpServletResponse
Session HttpSession (with a switch: The value of the session property of the page instruction)
Application ServletContext
Config ServletConfig
Page this (current Servlet object)
Exception java.lang.Throwable (with switches: The Iserrorpage property of the page directive is changed to true)
Out JspWriter
PageContext Javax.servlet.jsp.PageContext is very important.

PageContext objects have three main functions:

1, itself is a domain object, represents the domain scope is this page.

It also operates three other domain objects (PageContext, ServletRequest, Httpsession,servletcontext)

Set properties:

void setattribute (String name,object value)

void RemoveAttribute (String name)

Object getattribute (String name)

To manipulate the other three domain objects, set the properties:

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 defined by the static variables provided by the PageContext class.

Pagecontext.page_scope: Page range (is the map in the PageContext itself, code page)

Pagecontext.request_scope: The scope of the request (is the map in ServletRequest, code request)

Pagecontext.session_scope: The scope of the request (is the map in the HttpSession, code session)

Pagecontext.application_scope: The scope of the request (is servletcontext in the map, code-named application)

Object Findattribute (String name): In turn, follow the Page,request, session, application scope to search for the object of the specified name, as found.

The EL expression calls this method (very useful)

2. Get 8 Other implicit objects

3, provides the forwarding and contains the convenient method

If you do not pagecontext the object:

RequestDispatcher rd = request.getrequestdispatcher ("/url");

Rd.forward (Request,response);

Using the PageContext object:
Pagecontext.forward ("url");

Pagecontext.include ("url");

four domain objects (data transfer between two resources)

Implicit object's name range name specific type in JSP

PageContext page Javax.servlet.jsp.PageContext
Request Request Javax.servlet.ServletRequest
Session Session Javax.servlet.http.HttpSession
Application Application Javax.servlet.ServletContext (must be synchronized if used)

Second, El expression

It's just an expression in a JSP, not a development language.

Basic syntax: ${el expression}


1. Access to Data

An El expression can only get data from the four fields.

An El expression gets an object that is null, and the page does not display data. As a result, null pointer exceptions never appear in an El expression

"." Operator:

${p.name}: The GetName method with the name P object in the calling domain, the dot operator is used to get the value of the property.

[] Operator:

(. operator can do, [] can also do. [] What can be done is not necessarily possible.

For example, ${p.name}===${p[' name ']}==${p[' name '}

Excellent in can take not conform to the Java naming convention Dongdong.

2. Mathematical logic operation:

Empty operator: Returns true if the object being judged is null or an empty string.

For a collection, returns true even if the collection object itself is not null and has no elements.

The EL expression does not support string concatenation operations.


three, El built-in objects (11 large El built-in objects)

Get the built-in objects for the JSP (11 El built-in objects): difficulty, do not mix with the built-in object and range name of the JSP

11 Large El Implicit object, one of which represents the object of itself, the rest is represented by the map structure

El Implicit object name Java type remarks

PageContext Javax.servlet.jsp.PageContext is exactly the same as a built-in object in a JSP

The rest is represented by the map collection

Pagescope Java.util.Map represents the PageContext page range domain Map.
Requestscope Java.util.Map represents the ServletRequest request range domain Map.
Sessionscope Java.util.Map represents the httpsession session-wide domain MAP.
Applicationscope Java.util.Map represents the Map of the ServletContext application domain.
The Param JAVA.UTIL.MAP represents the request parameter. Key: The name of the request parameter. Value: Request parameter values, which is a string.
The Paramvalues JAVA.UTIL.MAP represents the request parameter. Key: The name of the request parameter. Value: Request parameter values, which is an array of strings.
Header Java.util.Map represents the request message header. Key: Header name. Value: The header value, which is a string.
Headervalues JAVA.UTIL.MAP represents the request message header. Key: Header name. Value: The header value, which is an array of strings.
A cookie Java.util.Map a Map that represents a cookie submitted by the client. Name of the Key:cookie. Value:cookie object itself
The Initparam JAVA.UTIL.MAP represents the global initialization parameters (Context-param in Web.xml). Key: Parameter name. Value: Parameter values


el expression with JSP built-in object problems
 
Q:

${pagecontex.request} and ${ Does the request} have a difference? Is the request here a HttpServletRequest class?
I have Request.setattribute ("key", key) in action; The
key variable is a collection when the JSP can get it with ${key}, but the key is a string ${key} is null.
What's the difference between attribute and parameter? Can I have the same name separately? (For example, there is a "key" in parameter, attribute also has a "key")
can, El in the distinction? Which one do you want first?

reply one:

What's the difference? The
Request object has several internal information, one of which is the attribute (key, value) queue.
${pagecontex.request} represents the request object itself and can obtain all the information for the request.
${request} represents the attribute (key, value) queue within the request object and can only obtain information within that queue

reply two:

Key variable is a collection when the JSP with ${key} can get, but the key is a string ${key} is null, why?
You can get it on page <%request.getparameter (' key ') >! What is the difference between

attribute and parameter:
GetParameter (string  name) gets the parameter value that the client passes to the server, which is specified by name, and is usually a parameter in the form.
GetAttribute (string  name): Returns the property value specified by name, and returns a null value if the specified property value does not exist.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.