Example analysis of various applications of EL expression in JSP

  • 2021-10-11 19:15:23
  • OfStack

EL operator:


<%@page language="java" contentType="text/html;charset=gb2312"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html>
<html>
  <head>
    <title>El Arithmetic operator </title>
  </head>
  <body>
    <table border=1>
      <tr>
        <td> Arithmetic operator </td>
        <td> Results </td>
      </tr>
      <tr>
        <td> Addition operator \${1 + 1}</td>
        <td> Results ${1+1}</td>
      </tr>
      <tr>
        <td> Subtraction operator \${1 - 1}</td>
        <td> Results ${1-1}</td>
      </tr>
      <tr>
        <td> Multiplication operator \${1 * 2}</td>
        <td> Results ${1 * 1}</td>
      </tr>
      <tr>
        <td> Division operator \${3 / 2}</td>
        <td> Results ${3 / 2}</td>
      </tr>
      <tr>
        <td> Coherence operator \${3 % 2}</td>
        <td> Results ${3 % 2}</td>
      </tr>
    </table>
    <table border=1>
      <tr>
        <td> Relational operator </td>
        <td> Results </td>
      </tr>
      <tr>
        <td> Equal operator \${1 == 1}</td>
        <td> Results ${1==1}</td>
      </tr>
      <tr>
        <td> Not equal to operator \${1 != 1}</td>
        <td> Results ${1!=1}</td>
      </tr>
      <tr>
        <td> Less than operator \${1 &lt; 2}</td>
        <td> Results ${1 < 2}</td>
      </tr>
      <tr>
        <td> Greater than operator \${3 &gt; 2}</td>
        <td> Results ${3 > 2}</td>
      </tr>
      <tr>
        <td> Less than or equal to operator \${1 &lt;= 2}</td>
        <td> Results ${1 <= 2}</td>
      </tr>
      <tr>
        <td> Greater than or equal to operator \${3 &gt;= 2}</td>
        <td> Results ${3 >= 2}</td>
      </tr>
    </table>
    <table border=1>
      <tr>
        <td> Logical operator </td>
        <td> Results </td>
      </tr>
      <tr>
        <td colspan="2"> Logical operator &&</td>
      </tr>
      <tr>
        <td>\${true && true}</td>
        <td> Results ${true && true}</td>
      </tr>
      <tr>
        <td>\${true && false}</td>
        <td> Results ${true && false}</td>
      </tr>
      <tr>
        <td>\${false && false}</td>
        <td> Results ${false && false}</td>
      </tr>
      <tr>
        <td colspan="2"> Logical operator ||</td>
      </tr>
      <tr>
        <td>\${true || true}</td>
        <td> Results ${true || true}</td>
      </tr><tr>
        <td>\${true || false}</td>
        <td> Results ${true || false}</td>
      </tr><tr>
        <td>\${false || false}</td>
        <td> Results ${false || false}</td>
      </tr>
      <tr>
        <td colspan="2"> Logical operator !</td>
      </tr>
      <tr>
        <td>\${!true}</td>
        <td> Results ${!true}</td>
      </tr><tr>
        <td>\${!false}</td>
        <td> Results ${!false}</td>
      </tr>
    </table>
    <table border=1>
      <tr>
        <td> Conditional operator </td>
        <td> Results </td>
      </tr>
      <tr>
        <td>\${2>1?"Yes":"No"}</td>
        <td> Results ${2>1?"Yes":"No"}</td>
      </tr>
      <tr>
        <td>\${2<1?"Yes":"No"}</td>
        <td> Results ${2<1?"Yes":"No"}</td>
      </tr>
    </table>
    <table border=1>
      <tr>
        <td> Validation operator </td>
        <td> Results </td>
      </tr>
      <tr>
        <td>\${empty param.username}</td>
        <td> Results ${empty param.username}</td>
      </tr>
      <tr>
        <td>\${empty param.password}</td>
        <td> Results ${empty param.password}</td>
      </tr>
    </table>
    <table border=1>
      <tr>
        <td> Parenthesis Operator and Operator Priority </td>
        <td> Results </td>
      </tr>
    </table>
  </body>
</html>

EL handles built-in objects:
The "." and "[]" symbols are used to access data


${applicationScope.user.username}

${applicationScope.user[0].username}

${applicationScope.user["user-name"]}

To pass a value through a variable, you must use []


${applicationScope.user["data"]}

Built-in objects related to storage


<!DOCTYPE html>
<html>
  <head>
    <title> Built-in objects related to storage </title>
  </head>
  <body>
    <%
      //application Range setting properties name With a value of application_name
      application.setAttribute("name","application_name");
      //session Range setting properties name With a value of session_name
      session.setAttribute("name","session_name");
      //request Range setting properties name With a value of request_name
      request.setAttribute("name","request_name");
      //page Range setting properties name With a value of page_name
      pageContext.setAttribute("name","page_name");
    %>
    <%-- Get page Within the scope name Attribute --%>
    page The value of the property within the scope is: ${pageScope.name}<br />
    <%-- Get request Within the scope name Properties of --%>
    request The value of the property within the scope is: ${requestScope.name}<br />
    <%-- Get session Within the scope name Properties of --%>
    session The value of the property within the scope is: ${sessionScope.name}<br />
    <%-- Get application Within the scope name Attribute --%>
    application The value of the property within the scope is: ${applicationScope.name}<br />
  </body>
</html>

Built-in objects related to input


<!DOCTYPE html>
<html>
  <head>
    <title> User form </title>
  </head>
  <body>
    <form action="UserDemo.jsp" method="post">
       User name: <input type="text" name="username" /><br />
       Password: <input type="password" name="password" /><br />
       Name: <input type="text" name="name" /><br />
       Gender: <input type="radio" name="sex" value=" Male "> Male <input type="radio" name="sex" value=" Female "> Female <br />
       Hobbies: <br />
      <input type="checkbox" name="interest" value=" Play basketball "> Play basketball <br />
      <input type="checkbox" name="interest" value=" Read a book "> Read a book <br />
      <input type="checkbox" name="interest" value=" Travel "> Travel <br />
      <input type="checkbox" name="interest" value=" Programming "> Programming <br />
      <input type="submit" value=" Submit ">
      <input type="reset" value=" Reset ">
    </form>
  </body>
</html>


<!DOCTYPE html>
<html>
  <head>
    <title> Receive user parameters </title>
  </head>
  <body>
    <%
      // Format page encoding 
      request.setCharacterEncoding("gb2312");
    %>
    <%-- Receive user parameters --%>
     User name: ${<span style="color:#003399;">param</span>.username}<br />
     Password: ${<span style="color:#003399;">param</span>.password}<br />
     Name: ${<span style="color:#003399;">param</span>.name}<br />
     Gender: ${<span style="color:#003399;">param</span>.sex}<br />
     Hobbies: ${<span style="color:#003399;">paramValues</span>.interest[0]}
        ${<span style="color:#003399;">paramValues</span>.interest[1]}
  </body>
</html>

cookie built-in object


<!DOCTYPE html>
<html>
  <head>
    <title> Settings cookie Value of </title>
  </head>
  <body>
    <%
      // Settings cookie Value of 
      Cookie c = new Cookie("username","root");
      // Add cookie To the client 
      response.addCookie(c);
    %>
    <a href="getCookieDemo.jsp"> Display cookie Value of </a>
  </body>
</html>

<span style="color:#000000;"><%@page language="java" contentType="text/html;charset=gb2312"%>
<!DOCTYPE html>
<html>
  <head>
    <title> Acquire cookie Value of </title>
  </head>
  <body>
    cookie In username The value of is: ${cookie.username.value}
  </body>
</html>

header Built-in Object


<!DOCTYPE html>
<html>
  <head>
    <title> Acquire header Value of </title>
  </head>
  <body>
    ${header["host"]}<br />
    ${header["user-agent"]}<br />
  </body>
</html>

initParam Built-in Object: Gets the environment variables set in the web site


<%@page language="java" contentType="text/html;charset=gb2312"%>
<!DOCTYPE html>
<html>
  <head>
    <title> Obtain environmental parameters </title>
  </head>
  <body>
    username Parameter values: ${initParam.username}<br />
  </body>
</html>

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 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"
  version="2.5">
  <context-param>
    <param-name>username</param-name>
    <param-value>Zhangdapeng</param-value>
  </context-param>
</web-app>

pageContex Built-in Object
Used to get detailed information about user requests and pages


<%@page language="java" contentType="text/html;charset=gb2312"%>
<!DOCTYPE html>
<html>
  <head>
    <title>pageContext Demo </title>
  </head>
  <body>
    <table border="1">
      <tr>
        <td> Gets the string of the requested parameters </td>
        <td>${pageContext.request.queryString}</td>
      </tr><tr>
        <td> Acquisition request URL</td>
        <td>${pageContext.request.requestURL}</td>
      </tr><tr>
        <td> Acquire web Application name </td>
        <td>${pageContext.request.contextPath}</td>
      </tr><tr>
        <td> Acquire HTTP Request mode ( POST/GET ) </td>
        <td>${pageContext.request.method}</td>
      </tr><tr>
        <td> Obtain the agreement for use </td>
        <td>${pageContext.request.protocol}</td>
      </tr><tr>
        <td> Obtain users IP Address </td>
        <td>${pageContext.request.remoteAddr}</td>
      </tr><tr>
        <td> Judge session Is it new </td>
        <td>${pageContext.session.new}</td>
      </tr><tr>
        <td> Acquire session Adj. id</td>
        <td>${pageContext.session.id}</td>
      </tr>
    </table>
  </body>
</html>

EL accessor:
Read data in JavaBean using accessor


package com.javaweb.ch08;
import java.util.*;
//1 A simple one JavaBean Example 
public class Person{
  //name Attribute 
  private String name;
  //age Attribute 
  private int age;
  //sex Attribute 
  private String sex;
  //friends Attribute 
  private ArrayList friends;
  // Parametric construction method 
  public Person(){
  }
  // Custom constructed methods 
  public Person(String name,int age,String sex){
    this.name = name;
    this.age = age;
    this.sex = sex;
  }
  // Get name Attribute value of 
  public String getName(){
    return name;
  }
  // Get age Attribute value 
  public int getAge(){
    return age;
  }
  // Get sex Attribute value of 
  public String getSex(){
    return sex;
  }
  // Settings name Properties of 
  public void setName(String name){
    this.name = name;
  }
  // Settings age Attribute value of 
  public void setAge(int age){
    this.age = age;
  }
  // Settings sex Attribute value of 
  public void setSex(String sex){
    this.sex = sex;
  }
  // Settings friends Value of 
  public void setFriends(ArrayList friends){
    this.friends = friends;
  }
  // Get friends Attribute value of 
  public ArrayList getFriends(){
    return friends;
  }
}


${applicationScope.user.username}

${applicationScope.user[0].username}

${applicationScope.user["user-name"]}

0

Using memory to read data in Map


${applicationScope.user.username}

${applicationScope.user[0].username}

${applicationScope.user["user-name"]}

1

Using memory to read data from an array


${applicationScope.user.username}

${applicationScope.user[0].username}

${applicationScope.user["user-name"]}

2

Complex application of memory


<%@page language="java" contentType="text/html;charset=gb2312"%>
<%@page import="java.util.*,com.javaweb.ch08.*" %>
<!DOCTYPE html>
<html>
  <head>
    <title> Settings Array Page </title>
  </head>
  <body>
    <%
      ArrayList<Person> persons = new ArrayList<Person>();
       
      Person person1 = new Person("wang wu",24," Male ");
      Person person2 = new Person("wang liu",24," Female ");
       
      persons.add(person1);
      persons.add(person2);
       
      session.setAttribute("persons",persons);
       
    %>
    <a href="GetMapDemo.jsp"> Jump to GetArrayDemo.jsp</a>
  </body>
</html>

<%@page language="java" contentType="text/html;charset=gb2312"%>
<!DOCTYPE html>
<html>
  <head>
    <title> Read using memory map Data in </title>
  </head>
  <body>
     User name 1 : ${sessionScope.persons[0].name},${sessionScope.persons[0].age},${sessionScope.persons[0].sex}<br />
     User name 2 : ${sessionScope.persons[1].name},${sessionScope.persons[1].age},${sessionScope.persons[1].sex}<br />
  </body>
</html>


Related articles: