Servlet & JSP (17th)

Source: Internet
Author: User

In this article, we will discuss Expression Language (EL ). Let's first discuss its syntax.

El syntax

The El syntax is simple. All El expressions start with $ {and end. For example, $ {expr }.

"[]" And "." Operators

El uses the "[]" and "." operators to access data. $ {expr. identifier} is equivalent to $ {expr [I "dentifier"]}. For example, the attribute name of the user accessing the JavaBean object can be written in the following two forms.

${user.name};${user["name"]};

If the "." operator is used, a map or Javabean is required on the left of the dot. For example

Map names = new HashMap();names.put("one","shan");names.put("tow","dong");request.setAtrribute("names",names);

In JSP, the first name is $ {names. One}. If "[]" is used, MAP, JavaBean, list, or array can be used on the left. For example, in servlet,

String names = {"shan","dong"};requset.setAttribute("names",names);

In JSP, the first name is $ {Names [0]}, or $ {Names ["0"]}. In El, indexes of the string type in the array and list are forcibly converted to int.

Arithmetic Operators

In El, there are five Arithmetic Operators +,-, *,/(or Div), % (or MoD ). They represent addition, subtraction, multiplication, division, and modulo.

Note: For Division operations, such as A/B. If A and B are null, (long) 0 is returned. If a and B are bigdecimal or biginteger, It is forcibly converted to bigdecimal, and A is returned. divide (B, bigdecimal. round_half_up). In other cases, A and B are forcibly converted to double and then divided.

Relational operators

In El, there are 6 Relational operators equal to (= or eq), not equal (! = Or NE), less than (<or LT), greater than (> or Gt), less than or equal to (<= or Le), greater than or equal to (> = or GE ).

Logical operators

There are three logical operators in El. Logic and (& or and), logic or (| or), logic is not (! Or not ).

Empty Operator

The empty operator is a prefix operator used to detect whether a value is null or empty. For example, if variable A does not exist, $ {empty a} returns true.

Conditional Operators

The conditional operator in El is "? : ", Which is equivalent to"? "in Java "? :".

Parentheses

Used to change the execution priority.

Operator priority (from high to low, from right)

[].
()
-(Unary) not! Empty
*/Div % mod
+-(Binary)
<> <=> = Lt gt le Ge
=! = EQ ne
& And
| Or
? :

Implicit object

The El expression defines 11 implicit objects. Using these 11 objects, you can easily read sessions, cookies, httpheader, and user-submitted forms (PARAM. The specific content is as follows.

Implicit object Content
Applicationscope A collection of scoped variables within the application scope.
Cookie A set of all cookies.
Header HTTP Request Header, string
Headervalues HTTP Request Header, string set
Initparam A set of all application parameter names
Pagecontext Javax. servlet. jsp. pagecontext object on the current page
Pagination Set of all objects in the page range
Param A set of all request parameter strings
Paramvalues All Request Parameters Used as a string set
Requestscope Set of objects in all request ranges
Sessionscope Set of objects in all session ranges

The following is an example of retrieving HTTP access header data and user-submitted data.

$ {Header ["host"]} to obtain the host value of the HTTP connection Header

$ {Header ["accept"]} to obtain the HTTP header's accept Value

$ {Header ["User-Agent"]} to obtain the User-Agent value of the HTTP Header

Note that only the "[]" operator can be used for strings containing the hyphen "-" and other special characters, rather than the "." operator. Therefore, you can use $ {header. Host} For the above two examples, but the last example can only use the above method.

Assume that the information submitted by the user is? Name = Shan & alies = shan.net.cn (two access methods are available here. One is to use "[]" for access, and the other is to use ". "To access, the two access effects are the same .)
$ {Param ["name"]} J: Shan
$ {Param. name} returns Shan
$ {Param ["alies"]} returns shan.net.cn
$ {Param. alies} returns shan.net.cn

Name variable

In El, you can use the pagecontext. findattribute (string) method to search for named variable values. For example, $ {user }. This expression searches for the named property user in the order of page, request, session, and application. If no user is found, null is returned. We can also use pagination, requestscope, sessionscope, and applicationscope to specify a range, for example, $ {sessionscope. User }.

Function

In El, functions can be defined and used. The syntax of the function is as follows:

NS: func (A1, A2,...,)

The prefix NS must match the prefix of the tag library containing the function. func is the function name, N1, N2, and so on are the function parameters.

The Function Definition and usage mechanism is similar to the label library. For example, you can compile a function that converts the encoding format and converts a string from a character set to GBK encoding. First, write a Java class and declare the function as a static function. The Code is as follows:

package com.shan.el;import java.io.*;public class MyFunc {public static toGBK(String str, String charset) throws UnsupportedEncodingException {return new String(str.getBytes(charset),"GBK")}}

Then declare the function in the tag library descriptor file.

<?xml version="1.0" encoding="ISO-8859-1" ?><!DOCTYPE taglib        PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"        "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd"><taglib>  <tlib-version>1.0</tlib-version>  <jsp-version>1.2</jsp-version>  <uri>myfunc</uri>  <function>    <name>toGBK</name>    <function-class>com.shan.el.MyFunc</function-class>    <function-signature>      java.lang.String toGBK(java.lang.String, java.lang.String)    </function-signature></taglib>

Finally, write a test page.

<% @ Page contenttype = "text/html; charset = gb2312" %> <% @ taglib uri = "/myfunc" prefix = "myfn" %> welcome, $ {myfn: togbk (Param. username, "ISO-8859-1 ")}!

Finally, enter the corresponding URL (such as http: // localhost: 8080/test. jsp? Username = "Zhang San") to view the final result.

Reprinted please indicate the source: http://blog.csdn.net/iAm333

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.