Use of JSP built-in objects and El expressions

Source: Internet
Author: User

first, the built-in object of JSP (9 JSP built-in objects)

JSP's built-in object reference name corresponds to the type

request HttpServletRequest
response HttpServletResponse
Session HttpSession (with 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 switch: 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, representing the domain scope is this page .

You can also manipulate three other domain objects (pagecontext, ServletRequest, Httpsession,servletcontext)

  

To 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 (the map in the PageContext itself, code page)

Pagecontext.request_scope: Range of requests (the map in ServletRequest, code request)

Pagecontext.session_scope: Request scope (is the map in HttpSession, code SESSION)

Pagecontext.application_scope: Request range (the map in ServletContext, code application)


Object Findattribute (String name): Searches for objects of the specified name, followed by Page,request, session, application scope, and so on.

An El expression is called this method (very useful)

2. Get the other 8 implicit objects

3, provide a convenient way to forward and include 

 If you do not use PageContext objects:

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

Rd.forward (Request,response);

 With PageContext object:
Pagecontext.forward ("url");

Pagecontext.include ("url");

Four domain objects (data transmitted between two resources)

J The name range name of an implicit object in the SP is of a 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)

Two, El expression

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

Basic syntax: ${el expression}


1. Get Data

An EL expression can only fetch data from four fields.

An El expression gets an object that if it is null, the page does not display data. Therefore, null pointer exceptions never appear in an El expression

". " operator :

${p.name}: Call the GetName method in the domain with the name P object, which is used to get the value of the property.

[] operator :

(. operator can do, [] also can do.) [] can do,. Not necessarily able to do

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

Excellence can be taken out of the Java naming conventions.


2. Mathematical Logic operations:

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

For collections, even if the collection object itself is not NULL, there is no element and returns TRUE.

An EL expression does not support string join operations.

El Built-in Objects (11 large El Built-in objects)

Get JSP built-in objects (11 large El Built-in objects): Difficult, do not mix with JSP's built-in object and scope name

11 Large El Implicit objects, one of which is the map structure that represents the rest of the object,

El Implicit object name Java type remarks

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


All that's left is a map collection of delegates.

Pagescope Java.util.Map represents the Map of the PageContext page range domain.

Requestscope Java.util.Map represents the ServletRequest request scope domain MAP.

Sessionscope Java.util.Map represents the httpsession session range domain Map.

Applicationscope Java.util.Map represents the Map of the ServletContext application area.


param Java.util.Map represents the request parameter. Key: The name of the request parameter. Value: The values of the request parameter, which is a string.

Paramvalues JAVA.UTIL.MAP represents the request parameter. Key: The name of the request parameter. Value: The values of the request parameter, which is an array of strings.


The 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.

The cookie Java.util.Map represents a Map of the cookie submitted by the client. The name of the Key: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

Use of JSP built-in objects and El expressions

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.