Difference between EL and JSTL and how to use it?

一、THE

Case presentation

(1) Read saved information with EL

Create a Java Enterprise project - ELJSTLDemo Please add a picture description
Click the [Finish] button Please add a picture description
to modify the Artifact name Please add a picture description
Redeploy the project Modify Please add a picture description
the home page Please add a picture description
Start the server and view the results Please add a picture description
Create the net.huawei.servlet package and create the ServletDemo01 class in the packagePlease add a picture description

package net.huawei.servlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 功能:保存request属性,请求转发到指定页面
 * 作者:华卫
 * 日期:2023年04月20日
 */
@WebServlet(name = "ServletDemo01", urlPatterns = "/demo01")
public class ServletDemo01 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 保存request属性
        request.setAttribute("username", "无心剑");
        request.setAttribute("password", "903213");
        // 获取请求转发器,转发到`demo01.jsp`页面
        RequestDispatcher dispatcher = request.getRequestDispatcher("/demo01.jsp");
        // 请求转发
        dispatcher.forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }
}

Create the demo01.jsp page in the web directoryPlease add a picture description

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>demo01</title>
    </head>
    <body>
        使用JSP表达式获取属性:<br />
        用户名:<%=request.getAttribute("username")%> <br />
        密码:<%=request.getAttribute("password")%> <br />
        <hr />
        使用EL获取属性:<br />
        用户名:${username} <br />
        密码:${password} <br />
    </body>
</html>

Start the server and visit http://localhost:8080/ELJSTLDemo/demo01Please add a picture description

pageContext object

pageContext object case

Create a demo02.jsp file in the web directory to demonstrate the usage of the pageContext implicit object
Please add a picture description

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>demo02 - 演示pageContext对象</title>
    </head>
    <body>
        请求URI为:${pageContext.request.requestURI} <br />
        Content-Type响应头:${pageContext.response.contentType} <br />
        服务器信息:${pageContext.servletContext.serverInfo} <br />
        Servlet注册名:${pageContext.servletConfig.servletName} <br />
    </body>
</html>

Start the server and visit http://localhost:8080/ELJSTLDemo/demo02.jspPlease add a picture description

Web domain related object example

Create a demo03.jsp file in the web directory to demonstrate how these 4 implicit objects access the properties in the JSP domain objectPlease add a picture description

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>demo03 - 演示Web域相关对象</title>
    </head>
    <body>
        <% pageContext.setAttribute("userName", "无心剑"); %>
        <% request.setAttribute("bookName", "动态网站开发"); %>
        <% session.setAttribute("userName", "陈燕文"); %>
        <% application.setAttribute("bookName", "大数据实时处理"); %>
        表达式\${pageScope.userName}的值为:${pageScope.userName} <br />
        表达式\${requestScope.bookName}的值为:${requestScope.bookName} <br />
        表达式\${sessionScope.userName}的值为:${sessionScope.userName} <br />
        表达式\${applicationScope.bookName}的值为:${applicationScope.bookName} <br />
        表达式\${userName}的值为:${userName}
    </body>
</html>

Start the server and visit http://localhost:8080/ELJSTLDemo/demo03.jspPlease add a picture description

Implicit object for accessing environment information

Case demonstration Obtaining the request parameters passed by the client

Create a demo04.jsp file in the web directory to demonstrate the request parameters passed by the client
Please add a picture description

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>demo04 - 演示获取客户端传递的请求参数</title>
    </head>
    <body style="text-align: center;">
        <form action="${pageContext.request.contextPath}/demo04.jsp">
            num1:<input type="text" name="num1"><br />
            num2:<input type="text" name="num"><br />
            num3:<input type="text" name="num"><br /> <br />
            <input type="submit" value="提交" />&nbsp;&nbsp;
            <input type="submit" value="重置" /><hr />
            num1: ${param.num1} <br />
            num2: ${paramValues.num[0]} <br />
            num3: ${paramValues.num[1]} <br />
        </form>
    </body>
</html>

Start the server, visit http://localhost:8080/ELJSTLDemo/demo04.jsp, Please add a picture description
enter three numbers in the form above, which are 100, 200, and 300, and click the submit buttonPlease add a picture description

Cookie object

Cookie object case

Create a demo05.jsp file in the web directory to demonstrate how to obtain the information in the Cookie object.
Please add a picture description
Start the server and visit http://localhost:8080/ELJSTLDemo/demo05.jsp. Since it is the first time the browser visits the demo05.jsp page, the server has not received the cookie information named userName at this time, so the information of the Cookie object will not be displayed in the browser window.
Please add a picture description
Refresh the page to see the resultPlease add a picture description

initParam object

initParam object case

Set web application initialization parameters in web.xmlPlease add a picture description

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <context-param>
        <param-name>author</param-name>
        <param-value>无心剑</param-value>
    </context-param>
</web-app>

Create a demo06.jsp file in the web directory to demonstrate the acquisition of Web application initialization parameters through the initParam objectPlease add a picture description

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>demo06 - 演示通过initParam对象获取Web应用初始化参数</title>
    </head>
    <body>
        Web应用初始化参数author的值:${initParam.author}
    </body>
</html>

Start the server and visit http://localhost:8080/ELJSTLDemo/demo06.jspPlease add a picture description

3. JSTL

Import the JSTL package

Create a lib directory in WEB-INF, copy the two files jstl.jar and standard.jar to the lib directory,
Please add a picture description
add jstl.jar and standard.jar to the project as a library, Please add a picture description
Please add a picture description
and click the [OK] buttonPlease add a picture description

Case Demo Tag Library

Create demo07.jsp in the web directoryPlease add a picture description

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<html>
    <head>
        <title>demo07 - 演示标签库</title>
    </head>
    <body>
        <h1 style="color: red; text-align: center">
            <c:out value="欢迎访问泸州职业技术学院~"/>
        </h1>
        <jsp:useBean id="now" class="java.util.Date" />
        <h3 style="text-align: center">
            <fmt:formatDate value="${now}" pattern="yyyy年MM月dd日 HH:mm:ss" />
        </h3>
    </body>
</html>

Start the server and visit http://localhost:8080/ELJSTLDemo/demo07.jspPlease add a picture description

4. Core tag library in JSTL

<c:out> tag example

Case 1. Demo default attribute
Create demo08.jsp in the web directoryPlease add a picture description

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
    <head>
        <title>demo08 - 演示default属性</title>
    </head>
    <body>
        <%--第1个out标签 --%>
        userName属性的值:
        <c:out value="${param.username}" default="用户名未知"/> <br />
        <%--第2个out标签 --%>
        userName属性的值:
        <c:out value="${param.username}">
            用户名未知
        </c:out>
    </body>
</html>

Start the server and visit http://localhost:8080/ELJSTLDemo/demo08.jspPlease add a picture description
Please add a picture description

Case 2. Demonstration of the escapeXML attribute

The <c:out> tag has an important attribute escapeXml, which can convert special characters into HTML code before outputting. A case demonstrates how to use the escapeXml attribute to convert special characters.
Create demo09.jsp in the web directoryPlease add a picture description

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
    <head>
        <title>demo09 - 演示escapeXml属性</title>
    </head>
    <body>
        <c:out value="${param.username}" escapeXml="false">
            <meta http-equiv="refresh" content="0;url=http://www.lzy.edu.cn" />
        </c:out>
    </body>
</html>

Start the server, visit http://localhost:8080/ELJSTLDemo/demo09.jsp Please add a picture description
Please add a picture description
start the server, visit http://localhost:8080/ELJSTLDemo/demo09.jspPlease add a picture description

Example of <c:remove> tag

Create demo10.jsp in the web directoryPlease add a picture description

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
    <head>
        <title>demo10 - 演示移除标签</title>
    </head>
    <body>
        <%
            pageContext.setAttribute("username", "无心剑");
            session.setAttribute("username", "陈燕文");
        %>
        <c:remove var="username" scope="page"/>
        用户名:<c:out value="${username}"/>
    </body>
</html>

Start the server and visit http://localhost:8080/ELJSTLDemo/demo10.jspPlease add a picture description

<c:if> tag example

Create demo11.jsp in the web directory
Please add a picture description

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
    <head>
        <title>demo11 - 演示单分支标签</title>
    </head>
    <body>
        <c:set value="1" var="visitCount" />
        <c:if test="${visitCount==1}">
            <h3 style="text-align: center">你第一次访问~欢迎访问泸职院~</h3>
        </c:if>
    </body>
</html>

Start the server and visit http://localhost:8080/ELJSTLDemo/demo11.jspPlease add a picture description

) for three label cases

Create demo12.jsp in the web directoryPlease add a picture description

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
    <head>
        <title>demo12 - 演示多分支标签</title>
    </head>
    <body>
        <c:choose>
            <c:when test="${empty param.username}">
                未知用户~
            </c:when>
            <c:when test="${param.username=='无心剑'}">
                ${param.username}是教师~
            </c:when>
            <c:otherwise>
                ${param.username}是学生~
            </c:otherwise>
        </c:choose>
    </body>
</html>

Start the server, visit http://localhost:8080/ELJSTLDemo/demo12.jsp Please add a picture description
visit http://localhost:8080/ELJSTLDemo/demo12.jsp?username=Wuxinjian Please add a picture description
visit http://localhost:8080/ELJSTLDemo/demo12.jsp?username=Chen YanwenPlease add a picture description

<c:forEach> tag example

Create demo13.jsp in the web directoryPlease add a picture description

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<html>
    <head>
        <title>demo13 - 演示循环标签</title>
    </head>
    <body>
        <%
            String[] fruits = {"苹果", "橘子", "葡萄", "香蕉"};
        %>
        String数组中的元素:
        <c:forEach var="name" items="<%=fruits%>">
            ${name} &nbsp;&nbsp;
        </c:forEach>
        <br />

        <%
            Map userMap = new HashMap();
            userMap.put("01", "北京");
            userMap.put("02", "上海");
            userMap.put("03", "广州");
        %>
        HashMap集合中的元素:
        <c:forEach var="entry" items="<%=userMap%>">
            ${entry.key} : ${entry.value} &nbsp;&nbsp;
        </c:forEach>
    </body>
</html>

Start the server, visit http://localhost:8080/ELJSTLDemo/demo13.jsp Please add a picture description
and create demo14.jsp in the web directoryPlease add a picture description

<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
    <head>
        <title>demo14 - 演示循环标签</title>
    </head>
    <body>
        colorsList集合(指定迭代范围和步长):
        <%
            List colorsList=new ArrayList();
            colorsList.add("red");
            colorsList.add("yellow");
            colorsList.add("blue");
            colorsList.add("green");
            colorsList.add("black");
        %>
        <c:forEach var="color" items="<%=colorsList%>"
                   begin="1" end="3" step="2">
            ${color}&nbsp;
        </c:forEach>
    </body>
</html>

Start the server, visit http://localhost:8080/ELJSTLDemo/demo14.jspPlease add a picture description

Example of URL-related tags

Create demo15.jsp in the web directoryPlease add a picture description

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
    <head>
        <title>demo15 - 演示URL相关标签</title>
    </head>
    <body>
        使用绝对路径构造URL:
        <c:url var="myURL" value="http://localhost:8080/ELJSTLDemo/demo08.jsp">
            <c:param name="username" value="陈燕文" />
        </c:url>
        <a href="${myURL}">demo08.jsp</a><br />
        使用相对路径构造URL:
        <c:url var="myURL" value="demo08.jsp?username=无心剑" />
        <a href="${myURL}">demo08.jsp</a>
    </body>
</html>

Start the server, visit http://localhost:8080/ELJSTLDemo/demo15.jsp Please add a picture description
Please add a picture description
and click the first hyperlink Please add a picture description
Return to the previous page Please add a picture description
Return to the previous pagePlease add a picture description

Guess you like

Origin blog.csdn.net/qq_64505257/article/details/130982907