JSP && El expression __jsp

Source: Internet
Author: User
Tags arithmetic operators logical operators
At first by the JSP object messed up, learned the El expression after more chaos ~

But today spent the morning time, get through the (*^__^*) Xi hee ...

first of all, JSP built-in objects

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 here, this article a large number of searches. I'd like to mention the object of PageContext.
PageContext This object is special, by its type that it is JSP-specific, the servlet does not have this object. The PageContext object provides access to all the objects and namespaces within the JSP page, which means that he can access the session where the page is located, or take a property value of the application that is in the page, which is the epitome of all the features in the pages.
Let's talk about El. Built-in objects:
El in order to facilitate the output of some of the values of expressions, they also define some built-in objects, the use of these built-in objects to easily get the value of the output
The built-in objects of JSP El and the built-in objects of the JSP are two different things that cannot be used, that is, the JSP El Built-in object can only be used in an El expression.
The built-in objects of the JSP can be used only in the JSP and not directly in El
However, there are two kinds of corresponding relationship, that is, the built-in object of El can call JSP corresponding to the built-in object save data, so as to facilitate the 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 object associated with the scope contains the following four: Pagescope, Requestscope, Sessionscope and Applicationscope;
They are basically the same as JSP PageContext, request, session and application;
In El, these four suppressed objects can only be used to obtain the Range property value, that is, getattribute (String name), but no other relevant information can be obtained.
For example: We want to get a value that stores a property username in the session, and you can use the following methods:
Session.getattribute ("username") obtains the value of username,
The following methods are used in El
${sessionscope.username}
2. Implied objects associated with the input
There are two hidden objects associated with the input: param and paramvalues, which are the more specific suppressed objects in El.
For example, when we want to take a user's request parameter, we can use 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 implied objects
Cookies
JSTL does not provide a cookie-setting action,
For example: To obtain a value in the cookie that has a set name of Usercountry, you can use ${cookie.usercountry} to obtain it.
Header and Headervalues
Header stores data that the user's browser and server use to communicate
For example: To get the version of the user's browser, you can use the ${header["User-agent"]}.
In addition, in rare opportunities, it is possible to have different values for the same header name, and you must instead use Headervalues to get the values.
Initparam
Initparam to set the environment parameters for the 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 the user's requirements or pages.
${pagecontext.request.querystring} Gets the requested parameter string
${pagecontext.request.requesturl} Gets the requested URL, but does not include the requested parameter string
${pagecontext.request.contextpath} The name of the Web application service
${pagecontext.request.method} method to obtain HTTP (GET, POST)
${pagecontext.request.protocol} Obtain the protocol used (http/1.1, http/1.0)
${pagecontext.request.remoteuser} Get user name
${PAGECONTEXT.REQUEST.REMOTEADDR} Gets the user's IP address
${pagecontext.session.new} to determine if the session is new
${pagecontext.session.id} Gets the ID of the session
${pagecontext.servletcontext.serverinfo} to obtain service information on the host side

How, after looking at the sudden dawned not (*^__^*) Xi hee ... I think at least will let you have a lot of the built-in object clearly ~ This is my Life of the Virgin blog Oh ~ ~ Encourage it (*^__^*) Xi hee ...
=================================================================================
Add an El expression:
I. El Introduction
1). Grammatical structure
${expression}
2). [] with. Accessor
EL provides. And [] Two accessors to access the data.
You must use [] when you want to access a property name that contains special characters, such as. For example:
${user. My-name} should be changed to ${user["My-name"]}
If you want to dynamically take a value, you can do it with [], and. The dynamic value cannot be taken. For example:
${sessionscope.user[data]}, data is a variable
3). Variable
The method of El Access variable data is simple, for example: ${username}. It means to remove a variable whose name is username in a range.
Since we do not specify which range of username, it will be searched sequentially from page, Request, session, application range.
If the way to find username, direct return, no longer continue to find, but if all the scope is not found, it will return null.
Name of attribute range in El
Page Pagescope
Request Requestscope
Session Sessionscope
Application Applicationscope
4). operator
1. Arithmetic operators have five: + 、-、 * or $,/or div,%, or mod
2. There are six relational operators: = = or EQ,!= or NE, < or LT, > or GT, <= or LE, >=, or GE
3. Logical operators have three:&& or and, | | Or OR,!, or not
4. There are three other operators: empty operator, conditional operator, () operator
Example: ${empty param.name}, ${a? B:C}, ${a* (B+c)}
5) El function (functions)
Grammar: Ns:function (arg1, arg2, Arg3 ... argn)
Where NS is the predecessor name (prefix), it must be placed with the predecessor name of the TAGLIB directive
Ii. El Note points:
1. ${logininfo.username}
This indicates the Username property that references the Logininfo object. We can go through "." Operator Primer
Object properties, such as ${logininfo[username], can also be referenced using the object's properties.
has achieved the same effect as ${logininfo.username}.
The meaning of the [] reference method is that if special characters appear in the property name, such as "." Or "-",
You must use ' [] ' to get the value of the property to avoid grammatical conflicts (you should try to avoid it when developing the system)
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 coding.
Another question that arises here is where EL will find the Logininfo object, for
${logininfo.username} Such an expression, the first is to look up from the current page before the
No variable logininfo is defined, if not found, then to request, session,
Application in the range until found. If we don't find a match until the end
Variable, NULL is returned.
You can see: get a variable in El directly with the variable name, it will automatically call get () method
If we need to specify the search scope for a 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 indicates that the. False prevents
The default enabled El language in JSP2.0.
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.