JSP && el Expressions

Source: Internet
Author: User
Tags arithmetic operators

At first, the JSP object made a mess, learned the El expression more chaotic ~

But it took me a whole morning to get through (*^__^*) hehe ...

first, the JSP built-in object

JSP built-in Object type mapping table

Object Name Type scope

Request Javax.servlet.ServletRequest Requestscope

Response Javax.servlet.ServletResponse Pagescope

PageContext Javax.servlet.jsp.PageContext Pagescope

Session Javax.servlet.http.HttpSession Sessionscope

Application Javax.servlet.ServletContext Applicationscope

Out Javax.servlet.jsp.JspWriter Pagescope

Config Javax.servlet.ServletConfig pagescope

Page Java.lang.Object Pagescope

Exception javax.lang.Throwable Pagescope

As for the nine built-in objects (also known as implicit objects) how to apply, I think I do not need to say that this aspect of the article search a large. I'd like to mention the object of PageContext.
PageContext This object is special, by its type it is a JSP-specific, the servlet does not have this object. The PageContext object provides access to all objects and namespaces within the JSP page, which means that he can access the session where the page is located, or a property value of the application that is located on the pages, which is the equivalent of all the features in the page.
say it again. El Built-in objects:
El in order to facilitate the output of some of the values of expressions, they also defined some built-in objects, the use of these built-in objects to easily get the value you want to output
The built-in objects of the JSP El and the built-in objects of the JSP are two different things and cannot be interoperable, that is, the JSP El Built-in object can only be used in an El expression.
JSP built-in objects can only be used in JSP and not directly in the El
However, there is a correspondence between the two, El's built-in object can invoke the JSP corresponding to the built-in object of the saved data, so as to easily display

Built-in objects for JSP el:
PageContext
Pagescope
Requestscope
Sessionscope
Applicationscope
Param
Paramvalues
Header
Headervalues
Cookies
Initparam
Can be divided into the following three categories:
1. Implied objects related to the scope
The El implied objects associated with the range contain the following four: Pagescope, Requestscope, Sessionscope and Applicationscope;
They are basically the same as JSP PageContext, request, session and application;
In El, these four hidden objects can only be used to obtain the range attribute value, which is getattribute (String name), but cannot obtain other related information.
For example: We want to get the value of storing an attribute username in the session, you can use the following methods:
Session.getattribute ("username") obtains the value of username,
In El, the following methods are used
${sessionscope.username}
2. Implied objects related to the input
There are two hidden objects associated with the input: param and paramvalues, which are the more specific hidden objects in El.
For example, when we want to get the user's request parameters, we can take advantage of the following methods:
Request.getparameter (String name)
Request.getparametervalues (String name)
In El, you can use both Param and paramvalues to get the data.
${param.name}
${paramvalues.name}
3. Other hidden Objects
Cookies
JSTL does not provide a cookie-setting action,
Example: To get a value in a cookie that has a setting name of Usercountry, you can use ${cookie.usercountry} to get it.
Headers and Headervalues
The header stores the data that the user's browser and server use to communicate
Example: To get the version of the user's browser, you can use ${header["User-agent"}.
In addition, under rare opportunities, it is possible to have different values for the same header name, and you must instead use Headervalues to obtain these values.
Initparam
Initparam get environment parameters for setting Web site (context)
Example: General method String UserID = (string) application.getinitparameter ("userid");
You can use ${initparam.userid} to get the name UserID
PageContext
PageContext obtain additional information about user requirements or pages.
${pagecontext.request.querystring} Gets the requested argument string
${pagecontext.request.requesturl} Gets the requested URL, but does not include the request's argument string
${pagecontext.request.contextpath} The name of the Web application of the service
${pagecontext.request.method} Method of getting HTTP (get, POST)
${pagecontext.request.protocol} Get the protocol used (http/1.1, http/1.0)
${pagecontext.request.remoteuser} Get user name
${PAGECONTEXT.REQUEST.REMOTEADDR} Get the IP address of the user
${pagecontext.session.new} to determine if the session is a new
${pagecontext.session.id} Gets the ID of the session
${pagecontext.servletcontext.serverinfo} access to host-side service information

How, after seeing a sudden suddenly did not (*^__^*) hehe ... I think at least it will make you clear on the built-in objects a lot of it ~ this is my life for a virgin blog Oh ~ ~ Encourage it (*^__^*) hehe ...
=================================================================================
To add an El expression:
First, El Introduction
1). Grammatical structure
${expression}
2). [] with. accessors
EL provides. And [] Two accessors to access data.
When you want to access a property name that contains some special characters, such as. or? Other than letters or numbers, be sure to use []. For example:
${user. My-name} should be changed to ${user["My-name"}
If you want to dynamically fetch values, you can do it with [], and. cannot be dynamically evaluated. For example:
${sessionscope.user[data]} in data is a variable
3). Variables
The EL Access variable data method is simple, for example: ${username}. It means to take out a variable whose name is username in a range.
Because we do not specify a range of username, it will be searched sequentially from page, Request, Session, application range.
If the way to find username, direct callback, no longer continue to find, but if all the scope is not found, the return null.
Name of the attribute range in El
Page Pagescope
Request Requestscope
Session Sessionscope
Application Applicationscope
4). operator
1. There are five arithmetic operators: + 、-、 * or $,/or div,% or mod
2. There are six relational operators: = = or EQ,! = or NE, < or LT, > or GT, <= or LE, >=, or GE
3. The logical operator has three:&& or an and, | | Or,!, or not
4. There are three other operators: the empty operator, the condition operator, the () operator
Example: ${empty param.name}, ${a? B:C}, ${a* (B+c)}
5) El function (functions)
Syntax: Ns:function (arg1, arg2, Arg3 .... ArgN)
Where NS is the predecessor name (prefix), and it must be placed with the predecessor name of the TAGLIB directive
Second, El Points of attention:
1.${logininfo.username}
This indicates that the Username property of the Logininfo object is referenced. We can pass the "." Operator Primer
Object properties, such as ${logininfo[username]}, can also be referenced with "[]" using the object's properties.
and ${logininfo.username} achieved the same effect.
The meaning of the "[]" reference is that if a special character appears in the property name, such as "." Or "-",
At this point, you must use "[]" to get the property values to avoid grammatical conflicts (system development should be avoided as far as possible
The emergence of this phenomenon).
The equivalent JSP script is roughly as follows:
Logininfo Logininfo =
(logininfo) Session.getattribute ("Logininfo");
String username = logininfo.getusername ();

As you can see, El greatly saves the amount of code.
Another question that arises here is where EL will find the Logininfo object, for
${logininfo.username}, the first thing to look for in the current page is the
The variable logininfo is defined, and if it is not found then the request, Session,
Application in the range until it is found. If the match is not found until the end
Variable, NULL is returned.
Can be seen:get a variable in El directly with the variable name, it will automatically call the Get () method
If we need to specify a scope for the variable, you can specify the search scope in the EL Expression:
${pagescope.logininfo.username}
${requestscope.logininfo.username}
${sessionscope.logininfo.username}
${applicationscope.logininfo.username}
2.<%@ page iselignored= "true"%>
Indicates whether the El language is disabled, and true to suppress. False indicates that
The default enabled El language in JSP2.0.

JSP && 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.