EL expressions and el expressions

Source: Internet
Author: User

EL expressions and el expressions

EL Expression. The full name is Expression Language. It was originally a language customized by JSTL 1.0 to facilitate data access. At that time, EL could only be used in JSTL labels. After JSP2.0, EL became part of the JSP specification and added new features.


You can use EL expressions on the JSP page to obtain and display page data and simplify access to variables and objects.


EL has the following features:

1. The PageContext attribute value can be obtained.

2. You can directly access the built-in JSP objects, such as page, request, session, and application.

3. Rich operators, Relational operators, logical operators, arithmetic operators, and other extended functions

4. It can correspond to static methods of JAVA classes


All EL expressions start with "$ {" and end with "}", for example, $ {EL Expression }.


EL uses [] and. to access the data, where $ {exupa. identifier} is equivalent to $ {exupa ["identifier"]}. The attribute name to be accessed contains some special characters, such. or-if it is not a letter or number, you must use [

]. EL expressions can be used directly on JSP pages or as element attribute values. They can also be customized, but cannot be used in script elements.


When an EL expression statement is executed, the pageContext. findAttribute method is called. The identifier is used as the keyword to find the corresponding objects in the four fields of page, request, session, and application.

Return the corresponding object. If no value is found, return "(Note: It is not null, but a null String ).


EL has 11 retained identifiers, corresponding to 11 EL implicit objects. The so-called reserved identifier means that when a variable is named, the above name should be avoided to avoid errors during program compilation. The specific reserved identifier is as follows:

Table:


The source code is as follows:

El. java source code:

Package com. el; import java. io. IOException; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; @ SuppressWarnings ("serial") public class el extends HttpServlet {public el () {super ();} public void destroy () {super. destroy (); // Put your code here} public void doGet (HttpServletRequest request, Response) throws ServletException, IOException {doPost (request, response);} public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// set the page encoding format request. setCharacterEncoding ("UTF-8"); response. setCharacterEncoding ("UTF-8"); request. setAttribute ("req1", "request container in Servlet"); request. getSession (). setAttribute ("sess1", "Here Is the session container in servlet "); this. getServletContext (). setAttribute ("app1", "application container in servlet"); request. getRequestDispatcher ("jsp1/ElJsp. jsp? Name1 = here is the parameter "). forward (request, response);} public void init () throws ServletException {}}

Source code of MyJsp. jsp:

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

ElJsp. jsp Source Code:

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

Web. xml source code:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>    <welcome-file>jsp1/MyJsp.jsp</welcome-file>  </welcome-file-list>    <servlet>    <servlet-name>el</servlet-name>    <servlet-class>com.el.el</servlet-class>  </servlet>    <servlet-mapping>    <servlet-name>el</servlet-name>    <url-pattern>/el</url-pattern>  </servlet-mapping>  </web-app>

My project name is Test, so the direct access path is as follows:

Http: // localhost: 8080/Test/jsp1/MyJsp. jsp


The result is displayed as follows:



When the access path is http: // localhost: 8080/Test/el, the running result is as follows:



From this we can see that, whether or not the session in the corresponding page is accessed, the session can still be directly used by other pages, unless the browser is closed or the corresponding session is closed.


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.