[Jsp] Lesson 9 learning and using EL expressions

What is EL?

EL is Expression Language

jsp's built-in expression language can be used directly from jsp 2.0

Functions of EL

Used instead of <% =..% >

EL syntax

${EL expression}

1. Obtain data in the four scopes

Basic Usage

Get data in the domain: ${pageScope|requestScope|sessionScope|applicationScope. Property name}

${attribute name} (common)   Equivalent to     pageContext.findAttribute("name")         "Returns" "if not found."

Four of the nine built-in objects can store data
From small to large:
1.pageContext: the data stored in this object can only be used in this page
2.request: the data stored in this object can only be used by other pages when forwarding the jump page
3.session: the data stored in this object can be shared without switching browsers
4.application: the data stored in this object can be shared arbitrarily without stopping the server

pageContext.setAttribute("a", "apple");
      request.setAttribute("b", "watermelon");
      session.setAttribute("c", "navel orange");
      application.setAttribute("d", "banana");

Method 1 for obtaining data of four domains:

<!-- Method 1 -- >
 <%=pageContext.getAttribute("a") %><br>
 <%=request.getAttribute("b") %><br>
 <%=session.getAttribute("c") %><br>
 <%=application.getAttribute("d") %><br> 

 <!-- Mode 2 -- >
 <%=pageContext.getAttribute("a") %><br>
 <%=pageContext.getAttribute("b", PageContext.REQUEST_SCOPE) %><br>
 <%=pageContext.getAttribute("c", PageContext.SESSION_SCOPE) %><br>
 <%=pageContext.getAttribute("d", PageContext.APPLICATION_SCOPE) %><br>

 <!-- Mode 3 -- >
 <!-- Quick find method
  This method will find the four scopes one by one from small to large until they are found
  -->
 <%= pageContext.findAttribute("a")  %>< br><!-- Once -- >
 <%= pageContext.findAttribute("b") %><br><!-- Twice -- >
 <%= pageContext.findAttribute("c") %><br><!-- Three times -- >
 <%= pageContext.findAttribute("d") %><br><!-- Four times -- >

 <!-- Mode 4 -- >
 <!-- El expression -- >
 $ {a}<br><!-- Once -- >
 $ {b}<br><!-- Twice -- >
 $ {c}<br><!-- Three times -- >
 $ {d}<br><!-- Four times -- >

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- Four of the nine built-in objects can store data
 From small to large:
1.pageContext: The data stored in this object can only be used in this page
2.request:The data stored in this object can only be used by other pages when forwarding jump pages is requested
3.session:The data stored in this object can be shared without switching browsers
4.application:The data stored in this object can be shared arbitrarily without stopping the server
 -->
 
 <%
 	pageContext.setAttribute("a", "Apple");
 	request.setAttribute("b", "watermelon");
 	session.setAttribute("c", "Navel orange");
 	application.setAttribute("d", "Banana");
 %>
 
 <!-- Mode 1 -->
 <%=pageContext.getAttribute("a") %><br>
 <%=request.getAttribute("b") %><br>
 <%=session.getAttribute("c") %><br>
 <%=application.getAttribute("d") %><br>
 <br><br><br><br>
 <!-- Mode II -->
 <%=pageContext.getAttribute("a") %><br>
 <%=pageContext.getAttribute("b", PageContext.REQUEST_SCOPE) %><br>
 <%=pageContext.getAttribute("c", PageContext.SESSION_SCOPE) %><br>
 <%=pageContext.getAttribute("d", PageContext.APPLICATION_SCOPE) %><br>
 <br><br><br><br>
 <!-- Mode III -->
 <!-- Quick find method
 This method will find the four scopes one by one from small to large until they are found
  -->
 <%=pageContext.findAttribute("a")  %><br><!-- Once -->
 <%=pageContext.findAttribute("b") %><br><!-- Twice -->
 <%=pageContext.findAttribute("c") %><br><!-- Three times -->
 <%=pageContext.findAttribute("d") %><br><!-- Four times -->
 <br><br><br><br>
 <!-- Mode 4 -->
 <!-- el expression -->
 ${a}<br><!-- Once -->
 ${b}<br><!-- Twice -->
 ${c}<br><!-- Three times -->
 ${d}<br><!-- Four times -->
</body>
</html>

Value from entity class

  Create an entity class:

package com.abc;

public class Dog {
	private String name;//name
	private int age;//Age
	private String color;//colour
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	public Dog() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Dog(String name, int age, String color) {
		super();
		this.name = name;
		this.age = age;
		this.color = color;
	}
	@Override
	public String toString() {
		return "Dog [name=" + name + ", age=" + age + ", color=" + color + "]";
	}
	
	
	
	
}

  Create a jsp, save the data to the entity class, store the entity class in the four scopes, and learn the el expression to obtain the data:

<%@page import="com.abc.Dog"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	
	<!-- Encapsulate multiple data in classes and store classes in four domains -->
	<%
		Dog dog=new Dog("Wangcai",2,"gules");
		request.setAttribute("d1", dog);
	%>
	
	<!-- Mode 1 -->
	<%
		Dog d=(Dog)request.getAttribute("d1");
	%>
	name:<%=d.getName() %><br>
	Age:<%=d.getAge() %><br>
	colour:<%=d.getColor() %><br>
	<br><br><br><br>
	<!-- el Expression gets the data in the object -->
	<!-- el When an expression obtains data, it refers to the name of the definition stored in the four fields -->
	name:${d1.name}<br>
	Age:${d1.age }<br>
	colour:${d1.color}<br>
	
</body>
</html>

Get data from arrays and Collections:

<%@page import="com.abc.Dog"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.Collections"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Arrays"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		int[] ages={20,18,22,25,27};
		application.setAttribute("a1", ages);
		
		List<String> oList=new ArrayList();
		oList.add("yellow");
		oList.add("gules");
		oList.add("green");
		oList.add("blue");
		oList.add("white");
		session.setAttribute("ol", oList);
		
		
		
	%>
	
	<!-- Method 1 -->
	<%
		int[] ages1=(int[])application.getAttribute("a1");
		List<String> oList1=(List<String>)session.getAttribute("ol");
	%>
	<%=Arrays.toString(ages1) %>
	<!-- landam expression -->
	<%-- <%=oList1.forEach(s->System.out.print(s)) %><br>
	<%=oList1.forEach(System.out::println) %><br> --%>
	<br>
	<br>
	<br>
	<!-- Method 2 use EL Get data from expression-->
	${a1[0]},${a1[1]},${a1[2]},${a1[3]},${a1[4]}<br>
	${ol[0] },${ol[1] },${ol[2] },${ol[3] },${ol[4] }<br>
	
</body>
</html>

Get the data in the map and the usage of mixed use:

<%@page import="com.abc.Dog"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.Collections"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Arrays"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		
		Map<String,String> oMap=new HashMap<>();
		oMap.put("book1", "Java Programming");
		oMap.put("book2", "MySQL sql server ");
		oMap.put("book3", "Jsp Network programming");
		pageContext.setAttribute("books", oMap);
		
		Map<String,String> oMap1=new HashMap<>();
		oMap1.put("dog1", "Tibetan Mastiff");
		oMap1.put("dog2", "Siberian Husky");
		oMap1.put("dog3", "Labrador");
		
		List<Map<String,String>> oList2=new ArrayList<>();
		oList2.add(oMap);
		oList2.add(oMap1);
		application.setAttribute("o", oList2);
		
		
		List<Dog> oDogs=new ArrayList<>();
		oDogs.add(new Dog("Xiao Huang",2,"white"));
		oDogs.add(new Dog("Big white",1,"yellow"));
		oDogs.add(new Dog("Big star",4,"brown"));
		session.setAttribute("g", oDogs);
	%>
	
	${books.book1 },${books["book2"] },${books.book3 }<br>
    <!-- Get the name of the second dog -->
	${o[1].dog2}<br>
	<!-- Take out the name of the third dog -->
	${g[2].name }
</body>
</html>

  This is the end of the exercise. Beginners need to master the mixed EL expressions between various sets, entity classes and arrays to store data, and obtain data in a flexible way.

2. Perform operations

  1. Arithmetic operator

        (1)+ - * /

         (2) % and mod:% (or equivalent mod) operators are used to calculate modulus (remainder).

<!-- el Expressions use arithmetic operators -->
Mod :${3 mod 2 }
<br><br>
<%
	request.setAttribute("age", 23);
%>
subtraction:${age - 5}
<br><br>

2. Relational operators

<!-- el Expressions use comparison operators -->
<!-- Age/5 Greater than 5 -->
${age/5 gt 5 }
<br><br>

3. Logical operators

<!-- el Expressions use logical operators -->
<!-- Age%6>5   &&  Age/4+1<6 -->
${age%6>5 && age/4+1<6}
<br><br>

4. Air transport operator

  1.  empty
  2. If the parameter of this operator is null, empty string, empty array, empty Map or empty collection, it returns true; otherwise, it returns false.
<!-- Air transport operator -->
<% int[] nums=null;
	application.setAttribute("n", nums);
%>
<!-- Determine whether a data is null -->
${empty(n) }

5. Ternary operator

<!-- Ternary operator  -->
<!-- Judge age-5 Whether it is greater than 18. If it is greater than 18, it shows adult, otherwise it is minor -->
${age-5>=18?"adult":"under age" }

6.EL operation example

3. Get common web objects

  This part of the learning code is supplemented later

Tags: Java JSP Tomcat Back-end

Posted on Mon, 22 Nov 2021 11:53:55 -0500 by budimir