EL expression in the use of jsp

sponsored links
EL expressions, called Expression Language EL-wide

EL syntax is very simple, it's biggest feature is very convenient for use. Next comes the main grammatical structures EL:

$ (SessionScope.user.sex)

EL are all starting with $ (to) for the end. EL means the above example: the range from the Session to obtain

The user's gender. If according to the wording before the JSP Scriptlet as follows:

User user = (User) session.getAttribute ("user");

String sex = user.getSex ();

Comparison of the two below can be found in EL syntax is more convenient than the traditional JSP Scriptlet, simple.

. With the [] operator

EL provide. And [] of two operators to navigate the data. The following represents the meaning of both is the same:

$ (SessionScope.user.sex) is equal to $ (sessionScope.user ["sex"])

. And [] can be also mixed, as follows:

$ (SessionScope.shoppingCart [0]. Price)

Return result shoppingCart the first item in the price.

However, the following two conditions are different between the two:

(1) When you want to access the property name contains special characters, such as. Or - such as letters or numbers are not symbols, they have to use [], for example: $ (user.My-Name)

These are not the right way, it should be changed to: $ (user ["My-Name"])

(2) We consider the following:

$ (SessionScope.user [data])

At this point, data is a variable, if the data value of "sex" when it is equal to the above example of $ (sessionScope.user.sex);

If the data value of "name" when it is equal to $ (sessionScope.user.name). Therefore, if the dynamic value, it is possible to do with the above method, but. Unable to do dynamic values.

EL Variables

EL access the variable data is simple, for example: $ (username). It means that out of a range variable named username. Because we did not specify which of a range of username, so it will start with the default search Page scope, if not found, then sequentially to the Request, Session, Application scope. If the way to find the username, the direct return, not to look down, but if all of the range are found, would return null, of course, will make the optimization of EL expression, a blank page instead of printed output NULL.

Property range (jstl name)
EL in the name of the

Page
PageScope

Request
RequestScope

Session
SessionScope

Application
ApplicationScope

We can also specify which of a range of variables out:

Sample Description

$ (PageScope.username)
Page out the scope of username variable

$ (RequestScope.username)
Request username out the scope of the variable

$ (SessionScope.username)
Remove the Session variable username range

$ (ApplicationScope.username)
Remove the Application scope variable username

Which, pageScope, requestScope, sessionScope and applicationScope are implicit EL objects by their name can easily guess the meaning they represent, for example: $ (sessionScope.username) is the Session scope out the username variable. The wording is not the wording than the previous JSP:

String username = (String) session.getAttribute ("username"); easy, simple and many.

Automatic shift type

EL addition to providing convenient access to variables outside the syntax, which is another handy feature: automatically change the type, we look at this example below:

$ (Param.count + 20)

Came the count value if the form 10, then the results above 30. JSP not contacted before the reader might think that the example above is a matter of course, but in JSP 1.2 being unable to do so, because the value coming from the form that they are invariably the type String, so when you receive after , then it must be converted to other types, such as: int, float, etc., and then to perform some mathematical operations, the following is the practice before:

String str_count = request.getParameter ("count");

int count = Integer.parseInt (str_count);

count = count + 20;
So, be careful not to and java syntax (when strings and numbers with "+" link will convert a number string), confused woven.

EL implicit objects

9 of JSP implicit object, which has its own implicit EL objects. EL implicit object a total of 11

Hidden object type description

PageContext
javax.servlet.ServletContext
That the JSP's PageContext

PageScope
java.util.Map
Page range to obtain the corresponding value of the property name

RequestScope
java.util.Map
Request scope attribute name to obtain the corresponding value of

sessionScope
java.util.Map
Session scope attribute names to obtain the corresponding value of

applicationScope
java.util.Map
Application scope attribute names to obtain the corresponding value of

param
java.util.Map
As ServletRequest.getParameter (String name). Return value of type String

paramValues
java.util.Map
As ServletRequest.getParameterValues (String name). Return String [] type value

header
java.util.Map
As ServletRequest.getHeader (String name). Return value of type String

headerValues
java.util.Map
As ServletRequest.getHeaders (String name). Return String [] type value

cookie
java.util.Map
As HttpServletRequest.getCookies ()

initParam
java.util.Map
As ServletContext.getInitParameter (String name). Return value of type String

But there is one thing to note is that if you use the words of EL output a constant string to double quotes, otherwise EL will default to a constant as you think of a variable, then if the variable is declared in the four areas does not exist, will output empty, if the output value of the variable.

Properties (Attribute) and scope (Scope)

And scope of the EL implicit object contains the following four: pageScope, requestScope, sessionScope and

applicationScope, and JSP are essentially the pageContext, request, session and application as well, so I note here only a little bit. Although it must be noted that the four implicit object attribute values can only be used to get range, that JSP in getAttribute (String name), but can not access other relevant information, such as: JSP request object in addition to access to property but also can obtain a user's request parameters or header information, etc.. But in the EL in, it can simply be used to obtain the corresponding range of property values, for example: we want to store an attribute in the session, its name for the username, in the JSP using session.getAttribute ("username") to get username value, but in the EL, then using the $ (sessionScope.username) to obtain its value.

cookie

The so-called cookie is a small text file that is key, value approach to Session Tracking the contents of the documents recorded in this text, this text file is usually found in the browser's temporary storage area. JSTL and set the cookie does not provide the action, because this action is usually the back-end developers have to do things, not the front-end developer. If we set a name in cookie value for the userCountry, you can use $ (cookie.userCountry) to get it.

header and headerValues

header store the user's browser and server to communicate with the data, when the user requests a Web page server will send a written request for information in the header file, for example: the user's browser version, the user's computer and other areas set data. If want to get the user's browser version, that is, $ (header ["User-Agent"]). Another chance at a little bit under the same header name may have different values, this time must be used instead headerValues to get these values.

Note: Because the User-Agent contains "-," the special characters, it must not be written using the "[]",

$ (Header.User-Agent).

initParam

Like other properties, we can draw up a web site of the environmental parameters (Context), when we wish to obtain these parameters initParam as other properties, we can draw up a web site of the environmental parameters (Context), when we want to obtain these parameters

<? Xml version = "1.0" encoding = "ISO-8859-1"?>

<Web-App xmlns = " http://java.sun.com/xml/ns/j2ee "

xmlns: xsi = " http://www.w3.org/2001/XMLSchema-instance "

xsi: schemaLocation = " http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "

version = "2.4">:

<context-param>

<param-name> userid </ param-name>

<param-value> mike </ param-value>

</ Context-param>:

</ Web-app>

Then we can use $ (initParam.userid) to get the name for the userid, the value of mike parameters. The following is the practice before: String userid = (String) application.getInitParameter ("userid");

param and paramValues
User parameters in obtaining what is commonly used methods:

request.getParameter (String name)
request.getParameterValues (String name)

You can use in the EL in both param and paramValues to obtain the data.
$ (Param.name)
$ (ParamValues.name)

Here param functions and request.getParameter (String name) the same, but paramValues and
request.getParameterValues (String name) the same. If the user fill a form, form name username, then we can use $ (param.username) to get the user fill in value.

pageContext

We can use $ (pageContext) to obtain user requirements or other relevant page for more information. The following table lists some commonly used parts

Expression
Explain

$ (PageContext.request.queryString)
Parameter string to obtain the requested

$ (PageContext.request.requestURL)
To obtain the requested URL, but does not include the parameters of the request string, the HTTP address of the servlet.

$ (PageContext.request.contextPath)
The name service webapplication

$ (PageContext.request.method)
HTTP access method (GET, POST)

$ (PageContext.request.protocol)
Made use of the protocol (HTTP/1.1, HTTP/1.0)

$ (PageContext.request.remoteUser)
Get username

$ (PageContext.request.remoteAddr)
To obtain the user's IP address

$ (PageContext.session.new)
Determine whether the new session, the so-called new session, said that while the client has just generated by the server not used

$ (PageContext.session.id)
To obtain the ID session

$ (PageContext.servletContext.serverInfo)
Host service to obtain information

This object can effectively improve the code hard-coding issues, such as page one A tag links to access a SERVLET, if coded the SERVLET the HTTP address then, if when the SERVLET the SERVLET-MAPPING time for a change need to modify the source code, This maintenance will be greatly reduced.

EL Arithmetic

Expression language supports arithmetic and logic operators are very much in the Java language in support of all the arithmetic operators, expression language can be used; even the Java language does not support some of the arithmetic operators and logical operators, the expression language also supports.

Code
<% @ Page contentType = "text / html; charset = gb2312"%>
<html>
<head>
<title> expression language - Arithmetic Operators </ title>
</ Head>
<body>
<h2> expression language - Arithmetic Operators </ h2>
<hr>
<table border="1" bgcolor="aaaadd">
<tr>
<td> <b> expression language </ b> </ td>
<td> <b> results </ b> </ td>
</ Tr>
<! - Direct output constant ->
<tr>
<td> \ $ (1) </ td>
<td> $ (1) </ td>
</ Tr>
<! - Calculation of the addition ->
<tr>
<td> \ $ (1.2 + 2.3) </ td>
<td> $ (1.2 + 2.3) </ td>
</ Tr>
<! - Calculation of the addition ->
<tr>
<td> \ $ (1.2E4 + 1.4) </ td>
<td> $ (1.2E4 + 1.4) </ td>
</ Tr>
<! - Calculation of subtraction ->
<tr>
<td> \ $ (-4 - 2) </ td>
<td> $ (-4 - 2) </ td>
</ Tr>
<! - Calculation of multiplication ->
<tr>
<td> \ $ (21 * 2) </ td>
<td> $ (21 * 2) </ td>
</ Tr>
<! - Computing division ->
<tr>
<td> \ $ (3 / 4) </ td>
<td> $ (3 / 4) </ td>
</ Tr>
<! - Computing division ->
<tr>
<td> \ $ (3 div 4) </ td>
<td> $ (3 div 4) </ td>
</ Tr>
<! - Computing division ->
<tr>
<td> \ $ (3 / 0) </ td>
<td> $ (3 / 0) </ td>
</ Tr>
<! - Calculated to request the ->
<tr>
<td> \ $ (10% 4) </ td>
<td> $ (10% 4) </ td>
</ Tr>
<! - Calculated to request the ->
<tr>
<td> \ $ (10 mod 4) </ td>
<td> $ (10 mod 4) </ td>
</ Tr>
<! - Calculation 3 project operator ->
<tr>
<td> \ $ ((1 == 2)? 3: 4) </ td>
<td> $ ((1 == 2)? 3: 4) </ td>
</ Tr>
</ Table>
</ Body>
</ Html>

Demonstrated the page above expression language supported by the add, subtract, multiply, divide, seeking more than other arithmetic function, the reader may also find the expression language also supports the div, mod and other operators. And the expression language as all values are floating point, so I 3 / 0 in essence, 3.0/0.0, get the results should be Infinity.

If you need the support of regular expression language of the page output, "$" symbol in the "$" symbol preceded by the escape character "\", otherwise the system that the "$" is the expression language specific tags.

EL relational operators

Shows examples of results of relational operators

== Or eq
Equal
$ (5 == 5) or $ (5eq5)
true

! = Or ne
Not equal
$ (5! = 5) or $ (5ne5)
false

<Or lt
Less than
$ (3 <5) or $ (3lt5)
true

> Or gt
Greater than
$ (3> 5) or (3gt5)
false

<= Or le
Less than or equal
$ (3 <= 5) or $ (3le5)
true

> = Or ge
Greater than or equal
5) or $ (3ge5)
false

Expression language not only in the comparison between figures and figures, but also in the comparison between the characters and the characters, the string comparison is based on its corresponding UNICODE values to compare the size.

Note: When using EL relational operators can not be written:
$ (Param.password1) = = $ (param.password2)
Or
$ ($ (Param.password1) = = $ (param.password2))
Should be written as
$ (Param.password1 = = param.password2)

EL logical operator

Logical example of the results

& & Or and
Intersection $ (A & & B) or $ (A and B)
true / false

| | Or or
And set $ (A | | B) or $ (A or B)
true / false

! Or not
Non-$ (! A) or $ (not A)
true / false

Empty operator
$ (Empty A)
Empty operator A is mainly used to determine whether the null value or empty.

Conditional operator

$ (A? B: C)
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of EL expression in the use of jsp

  • js function in the operation of cookie

    <script type="text/javascript"> function setCookie (sName, sValue, oExpires, sPath, sDomain, bSecure) ( var sCookie = sName + "=" + encodeURIComponent (sValue); / / Failure time if (oExpires) ( sCookie + = "; expires ="

  • Play flash page 3 (javascript script swfobject.js1)

    / ** * SWFObject v1.5: Flash Player detection and embed - http://blog.deconcept.com/swfobject/ * * SWFObject is (c) 2007 Geoff Stearns and is released under the MIT License: * Http://www.opensource.org/licenses/mit-license.php * * / if (typeof deconc ...

  • Struts + Spring + Hibernate practice

    Tools: Eclipse3.1, MyEclipse4.03, Tomcat5.5.9, Properties Editor plug-ins, MySql4.1.13 New construction: the name for the login Create Struts framework Create index.jsp, add a link to login.jsp Press Ctrl + N, to create login.jsp, LoginAction, the use of

  • fck pages

    <% @ Page contentType = "text / html; charset = UTF-8"%> <% @ Include file = "/ commons / taglibs.jsp"%> <% @ Taglib uri = "/ FCKeditor" prefix = "FCK"%> <script language = "javascript

  • Msxml2.XMLHTTP version problem

    Projects with an import feature prototype.js of Ajax functionality to update the prompt, the code is very simple, do not have the framework of the background on a jsp to output Text, future use of timers and to update the page Ajax.request encountere ...

  • Object-oriented design principles 11

    What is object-oriented design? Contains what? The benefits of it are what? Required to pay what you do? In today's age, ask these questions seem to be foolish, because this year almost every software developer knows how to use some sort of objec ...

  • js page Jump implementation of a number of ways

    The first is: <script language="javascript" type="text/javascript"> window.location.href = "login.jsp? backurl =" + window.location.href; </ script> The second: <script language="javascript"> alert

  • Dynamic loading JS script four kinds of methods

    To achieve dynamic loading JS script has four kinds of methods: 1, direct document.write <script language="javascript"> document.write ( "<script src='test.js'> <\ / script>"); </ script> 2, dynamic scri

  • RoR explained

    ROR is Ruby on Rails. Ruby is a well-known has been very good dynamic language It's dynamic language. Simple and easy. Dynamic languages are interpreted, but the performance may make a discount, but not absolute, because the application is complex, th

  • Some interview questions java

    The first is the company give you a chance to meet, it is necessary to know to meet from time to equal the interview, and have a lot of companies to see you at the first time will give you a ready point of doing something trivial, these questions, althoug

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries