JSP details-EL expressions (2)

Source: Internet
Author: User

JSP details-EL expressions (2)
EL implicit object

To obtain relevant data in a Web application, EL provides 11 implicit objects. These objects are similar to the built-in JSP objects and are operated directly by the object name. In EL's implicit object, except that PageContext is a JavaBean object and corresponds to the javax. servlet. jsp. PageContext type, other implicit objects correspond to the java. util. Map type. These implicit objects can be divided into three types: Page Context objects, implicit objects in the scope of access scope, and hidden objects in the access environment information. The following describes in detail.

1. Page Context object

The page context object is pageContext, which is used to access the built-in JSP objects (request/response/out/session/exception and page, but cannot be used to obtain application/config/pageContext) and servletContext. After obtaining these built-in objects, you can obtain their attributes. These attributes correspond to the getXXX () method of the object. In use, remove get from the method name and change the first letter to lowercase.

Access request object

You can use the following statement to obtain the request object in the built-in JSP object through pageContext:

$ {PageContext. request}

After obtaining the request object, you can obtain information related to the client through this object. For example, HTTP header information, client information submission method, Client IP address, and port number.

Example:

To access the getServerPort () method, use the following code:

$ {PageContext. request. serverPort}

The above code returns the port number.

<% @ Page language = "java" import = "java. util. *" pageEncoding = "ISO-8859-1" %>

<%

String path = request. getContextPath ();

String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";

%>

My JSP 'request. jsp 'starting page

$ {PageContext. request. serverName}

$ {PageContext. request. serverPort}

$ {PageContext. request. servletPath}

Access response object

You can use the following statement to obtain the response object in the JSP built-in object through pageContext:

$ {PageContext. response}

Example:

<% @ Page language = "java" import = "java. util. *" pageEncoding = "ISO-8859-1" %>

<%

String path = request. getContextPath ();

String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";

%>

My JSP 'response. jsp 'starting page

$ {PageContext. response. bufferSize}

$ {PageContext. response. characterEncoding}

Access out object

Use the following syntax to access the out object through pageContext:

$ {PageContext. out}

Example:

<% @ Page language = "java" import = "java. util. *" pageEncoding = "ISO-8859-1" %>

<%

String path = request. getContextPath ();

String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";

%>

My JSP 'out. jsp 'starting page

$ {PageContext. out. bufferSize}

$ {PageContext. out. remaining}

Access session object

The syntax format for accessing the session object is:

$ {PageContext. session}

Example:

<% @ Page language = "java" import = "java. util. *" pageEncoding = "ISO-8859-1" %>

<%

String path = request. getContextPath ();

String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";

%>

My JSP 'session. jsp 'starting page

$ {PageContext. session. id}

Access exception object

The syntax format for getting the exception object of the JSP built-in object through pageContext is:

$ {PageContext. exception}

Example:

<% @ Page language = "java" import = "java. util. *" pageEncoding = "ISO-8859-1" %>

<%

String path = request. getContextPath ();

String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";

%>

My JSP 'exception. jsp 'starting page

$ {PageContext. exception. localizedMessage}

Access page Object

The syntax format for accessing a page object through pageContext is:

$ {PageContext. page}

Example:

<% @ Page language = "java" import = "java. util. *" pageEncoding = "ISO-8859-1" %>

<%

String path = request. getContextPath ();

String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";

%>

My JSP 'page. jsp 'starting page

$ {PageContext. page. class}

Access the servletContext object

The syntax format is as follows:

$ {PageContext. servletComtext}

Example:

<% @ Page language = "java" import = "java. util. *" pageEncoding = "ISO-8859-1" %>

<%

String path = request. getContextPath ();

String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";

%>

My JSP 'servletcontext. jsp 'starting page

$ {PageContext. servletContext. contextPath}

2. Implicit objects within the scope of access

In EL, four types of implicit objects are provided for accessing the scope, namely pagination/requestScope/sessionScope/applicationScope. After the four hidden objects are used to specify the scope of the identifiers to be searched, the system no longer searches for the corresponding identifiers in the default Order (page/request/session/application. They are similar to the built-in page/request/session/application objects in JSP. However, these four implicit objects can only be used to obtain attributes within the specified range, rather than other related information.

Pagination implicit object

The implicit pagination object is used to return a set of property values that contain the page range. The returned value is a java. util. Map object.

Example:

<% @ Page language = "java" import = "java. util. *" pageEncoding = "ISO-8859-1" %>

<%

String path = request. getContextPath ();

String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";

%>

My JSP 'pagination. jsp 'starting page

$ {Pagination. user. name}

RequestScope implicit object

The implicit requestScope object is used to return a set of attributes within the request range. The returned value is a java. util. Map object.

Example:

<% @ Page language = "java" import = "java. util. *" pageEncoding = "ISO-8859-1" %>

<%

String path = request. getContextPath ();

String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";

%>

My JSP 'requestscope. jsp 'starting page

$ {RequestScope. user. name}

SesssionScope implicit object

SessionScope implicit object is used to return the set of attribute values within the session range. The returned value is a java. util. Map object.

Example:

<% @ Page language = "java" import = "java. util. *" pageEncoding = "ISO-8859-1" %>

<%

String path = request. getContextPath ();

String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";

%>

My JSP 'sessionscope. jsp 'starting page

$ {SessionScope. user. name}

ApplicationScope implicit object

ApplicationScope implicit object is used to return a set of property values containing the application range. The returned value is a java. util. Map object.

Example:

<% @ Page language = "java" import = "java. util. *" pageEncoding = "ISO-8859-1" %>

<%

String path = request. getContextPath ();

String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";

%>

My JSP 'applicationscope. jsp 'starting page

$ {ApplicationScope. user. name}

3. hidden objects for accessing Environment Information

Six hidden objects for accessing environment information are provided in EL.

A. param object

The param object is used to obtain the value of the request parameter, which is only applicable when the parameter value is one. When the param object is applied, the returned result is a string.

Example:

On the JSP page, place a text box named user:

After the form is submitted, you can use the following method to obtain the value of the name text box:

$ {Param. name}

PS:

If you have entered Chinese characters in the name text box, you must use the request when using EL to output the content. setCharacterEncoding ("GBK"); the statement sets the Request Encoding to support Chinese encoding, otherwise garbled characters may occur.

B. paramValues object

If a request parameter name corresponds to multiple values, you must use the paramValues object to obtain the request parameter value. When the paramValues object is applied, the returned result is an array.

Example:

On the JSP page, place a check box named affect.

Mountaineering

Swimming

Slow walk

Morning run

To obtain the value of affect after a form is submitted, use the following format:

<% Request. setCharacterEncoding ("UTF-8"); %>

Hobbies: $ {paramValues. affect [0] }$ {paramValues. affect [1] }$ {paramValues. affect [2] }$ {paramValues. affect [3]}

When param and paramValues objects are used, if the specified parameter does not exist, an empty string is returned instead of null.

C. header and headerValues object

The header object is used to obtain a specific header value of an HTTP request. However, in some cases, the same header may have multiple different values, this requires the headerValues object

Example:

To obtain the connection attribute of the HTTP Request header, you can use the following form:

$ {Header. connection} or $ {header ["connection]}

However, to obtain the user-agent attribute of the HTTP Request header, you must use the following EL expression:

$ {Header ["user-agent"]}

D. initParam object

The initParam object is used to obtain the initialization parameter value of the Web application.

Example:

Set an initialization parameter author in Web. xml of the web application to specify the author.

Author

Mr

Use the EL expression to obtain the author parameter:

$ {InitParam. author}

F. cookie object

In EL, no method is provided to save the value to the cookie, but the cookie method set by the request is provided, which can be implemented through the cookie implicit object. If a value named username has been set in the cookie, you can get the cookie object through $ {cookie. username. To obtain the value in the cookie, you must use the value Attribute of the cookie object.

Example:

Use the response object to set a valid cookie object for the request, and then use EL to obtain the value of the cookie object. You can use the following code:

<%

Cookie cookie = new Cookie ("user", "zhangsan ");

Response. addCookie (cookie );

%>

$ {Cookie. user. value}


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.