1、熟悉EL的常量、变量、运算符的基本用法,以及对NullPointerException异常的处理
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>表达式语言</title>
</head>
<body>
<h2>
表达式语言 - 算术运算符
</h2>
<hr>
<table border="1" bgcolor="aaaadd">
<tr>
<td>
<b>表达式语言</b>
</td>
<td>
<b>计算结果</b>
</td>
</tr>
<!-- 直接输出常量 -->
<tr>
<td>
\${1}
</td>
<td>
${1}
</td>
</tr>
<!-- 计算加法 -->
<tr>
<td>
\${1.2 + 2.3}
</td>
<td>
${1.2 + 2.3}
</td>
</tr>
<!-- 计算加法 -->
<tr>
<td>
\${1.2E4 + 1.4}
</td>
<td>
${1.2E4 + 1.4}
</td>
</tr>
<!-- 计算减法 -->
<tr>
<td>
\${-4 - 2}
</td>
<td>
${-4 - 2}
</td>
</tr>
<!-- 计算乘法 -->
<tr>
<td>
\${21 * 2}
</td>
<td>
${21 * 2}
</td>
</tr>
<!-- 计算除法 -->
<tr>
<td>
\${3/4}
</td>
<td>
${3/4}
</td>
</tr>
<!-- 计算除法 -->
<tr>
<td>
\${3 div 4}
</td>
<td>
${3 div 4}
</td>
</tr>
<!-- 计算除法 -->
<tr>
<td>
\${3/0}
</td>
<td>
${3/0}
</td>
</tr>
<!-- 计算求余 -->
<tr>
<td>
\${10%4}
</td>
<td>
${10%4}
</td>
</tr>
<!-- 计算求余 -->
<tr>
<td>
\${10 mod 4}
</td>
<td>
${10 mod 4}
</td>
</tr>
<!-- 计算三目运算符 -->
<tr>
<td>
\${(1==2) ? 3 : 4}
</td>
<td>
${(1==2) ? 3 : 4}
</td>
</tr>
</table>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>表达式语言 - 逻辑运算符</title>
</head>
<body>
<h2>
表达式语言 - 逻辑运算符
</h2>
<hr>
<table border="1" bgcolor="aaaadd">
<tr>
<td>
<b>表达式语言</b>
</td>
<td>
<b>计算结果</b>
</td>
</tr>
<tr>
<td>
\${A&&B}
</td>
<td>
${A&&B}
</td>
</tr>
<!-- 使用&&比较运算符 -->
<tr>
<td>
\${ A and B}
</td>
<td>
${ A and B}
</td>
</tr>
<!-- 使用and比较运算符 -->
<tr>
<td>
\${ A || B}
</td>
<td>
${ A || B}
</td>
</tr>
<!-- 使用||比较运算符 -->
<tr>
<td>
\${A or B}
</td>
<td>
${A or B}
</td>
</tr>
<!-- 使用or比较运算符 -->
<tr>
<td>
\${ !A}
</td>
<td>
${ !A}
</td>
</tr>
<!-- 使用!比较运算符 -->
<tr>
<td>
\${not A }
</td>
<td>
${not A }
</td>
</tr>
<!-- 使用not比较运算符 -->
</table>
具体数据实例比较:
<table border="1" bgcolor="aaaadd">
<tr>
<td>
<b>表达式语言</b>
</td>
<td>
<b>计算结果</b>
</td>
</tr>
<tr>
<td>
\${12 == 12&&12 < 11}
</td>
<td>
${12 == 12&&12 < 11}
</td>
</tr>
<!-- 使用&&比较运算符 -->
<tr>
<td>
\${ 12 == 12 and 12 < 11}
</td>
<td>
${ 12 == 12and 12 < 11}
</td>
</tr>
<!-- 使用and比较运算符 -->
<tr>
<td>
\${ 12 == 12 || 12 < 11}
</td>
<td>
${ 12 == 12|| 12 < 11}
</td>
</tr>
<!-- 使用||比较运算符 -->
<tr>
<td>
\${12 == 12 or 12 < 11}
</td>
<td>
${12 == 12or 12 < 11}
</td>
</tr>
<!-- 使用or比较运算符 -->
<tr>
<td>
\${ !true}
</td>
<td>
${ !true}
</td>
</tr>
<!-- 使用!比较运算符 -->
<tr>
<td>
\${not true}
</td>
<td>
${not true }
</td>
</tr>
<!-- 使用not比较运算符 -->
</table>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%request.setAttribute("now",new java.util.Date());%>
user变量指向的对象为null,表达式\${user}的输出结果为: ${user}
<br />
user变量指向的对象为null,表达式\${user.name}的输出结果为: ${user.name}
<br />
now变量指向的对象类型为Date,表达式\${now.time}的输出结果为: ${now.time}
<br />
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%request.setAttribute("now",new java.util.Date());%>
user变量指向的对象为null,表达式\${user}的输出结果为: ${user}
<br />
user变量指向的对象为null,表达式\${user.name}的输出结果为: ${user.name}
<br />
now变量指向的对象类型为Date,表达式\${now.time}的输出结果为: ${now.time}
<br />
<!--以上结果都为空-->
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%request.setAttribute("now",new java.util.Date());%>
now变量指向的对象类型为Date,表达式\${now.address}的输出结果为: ${now.address}
<%--对象存在,属性不存在,怎么处理异常
抛出jakarta.el.PropertyNotFoundException: 类型[java.util.Date]上找不到属性[address]异常
--%>
<br />
2、用EL隐式对象编写程序
<context-param>
<param-name>PI</param-name>
<param-value>3.14</param-value>
</context-param>
<%@page contentType="text/html;charset=utf-8"%>
<html>
<head>
<title>EL隐式对象的使用</title>
</head>
<body>
<form method=post action=circle.jsp>
请输入圆的半径:
<input type="text" name="radius" value="${param.radius}">
<p>
<input type="reset" value="重置">
<input type="submit" value="提交" name="mysubmit">
</form>
<%
if (request.getParameter("mysubmit") != null) {
%>
圆的半径:${param.radius}
<p>
圆的周长:${param.radius*2*initParam.PI}
<p>
圆的面积:${param.radius*param.radius*initParam.PI}
<p>
<p>
IP地址:${pageContext.request.remoteAddr}
<%
// out.println(request.getRemoteAddr());
}
%>
</body>
</html>
3、使用JSTL显示所有学生信息
package servlet;
import dao.studentDAO;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import pojo.Student;
import java.io.IOException;
import java.util.ArrayList;
@WebServlet(name = "ShowAllStudentsServlet", value = "/ShowAllStudentsServlet")
public class ShowAllStudentsServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
studentDAO studentDAO = new studentDAO();
ArrayList<Student> studentArrayList = new ArrayList<>();
String sql = "select * from java2022";
studentArrayList = studentDAO.select(sql);
request.getSession().setAttribute("studentArrayList", studentArrayList);
request.getRequestDispatcher("showAllStudents.jsp").forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
<%@ page import="pojo.Student" %>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>显示所有学生信息</title>
</head>
<body>
<a href="addNum.jsp">添加学号</a>
<%
ArrayList<Student> studentArrayList = (ArrayList<Student>) request.getSession().getAttribute("studentArrayList");
%>
<table border="1">
<tr>
<td>学号</td>
<td>姓名</td>
<td>操作</td>
</tr>
<c:forEach var="student" items="${studentArrayList}">
<tr>
<td>${student.num}</td>
<td>${student.name}</td>
<td>
<a href="">修改</a>
<a href="">删除</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
4、JSTL中<c:out>、<c:set>、<c:if>的用法
<%@page contentType="text/html;charset=utf-8"%>
<html>
<title>课堂测试</title>
<body>
<center>
<font color="blue" size="5"> <i><b>课堂测试,请先输入学号和密码…</b> </i> </font>
<hr>
<form action="check.jsp" method="post">
<table border="1">
<tr>
<th bgcolor="yellow">
学号
</th>
<td>
<input type="text" size="12" name="id">
</td>
</tr>
<tr>
<th bgcolor="yellow">
密码
</th>
<td>
<input type="password" size="12" name="password">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="测试开始">
</td>
</tr>
</table>
</form>
<font color="red"> <%
//根据错误类型,显示相应信息
String error = request.getParameter("errortype");//由get返回错误参数值
if (error != null) {
int errortype = Integer.parseInt(error);//转换成整数
switch (errortype)
{
case 1:
out.println("学号为空!!");
break;
case 2:
out.println("密码为空!!");
break;
default:
}
}
%> </font>
</center>
</body>
</html>
<%@ page contentType="text/html;charset=utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>set_out_if 测试</title>
</head>
<body>
<c:out value="<h2>c:out、c:set、c:if用法</h2>" />
<c:out value="<h2>c:out、c:set、c:if用法</h2>" escapeXml="false" />
<c:set var="id" value="${param.id}" />
<c:set var="password" value="${param.password}" />
<c:if test="${id==''}">
<c:set var="errortype" value="1" />
<c:redirect url="enter.jsp">
<c:param name="errortype" value="${errortype}" />
</c:redirect>
</c:if>
<c:if test="${password==''}">
<c:set var="errortype" value="2" />
<c:redirect url="enter.jsp">
<c:param name="errortype" value="${errortype}" />
</c:redirect>
</c:if>
<p>
学号:
<c:out value="${id}" default="未输入" />
<p>
密码:
<c:out value="${password}" default="未输入" />
<p>
</body>
</html>
5、程序功能:输出1—100之间,分别输出能被3、5、7整除的数。通过本程序学习JSTL中<c:forEach>、<c:choose>、<c:when>、<c:otherwise>的用法
<%@ page contentType="text/html;charset=utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>foreach_choose 测试</title>
</head>
<body>
<c:set var="str3" value="" />
<c:set var="str5" value="" />
<c:set var="str7" value="" />
<c:set var="str" value="" />
<c:forEach var="i" begin="1" end="100">
<c:choose>
<c:when test="${i%3==0}">
<c:set var="str3" value="${str3} ${i}" />
</c:when>
<c:when test="${i%5==0}">
<c:set var="str5" value="${str5} ${i}" />
</c:when>
<c:when test="${i%7==0}">
<c:set var="str7" value="${str7} ${i}" />
</c:when>
<c:otherwise>
<c:set var="str" value="${str} ${i}" />
</c:otherwise>
</c:choose>
</c:forEach>
<p>
3的倍数:
<c:out value="${str3}"/>
<p>
5的倍数:
<c:out value="${str5}" />
<p>
7的倍数:
<c:out value="${str7}" />
<p>
其它数字:
<c:out value="${str}" />
<p>
</body>
</html>
6、程序功能是使用<c:forTokens>标签实现获取一个字符串中所有单词的功能
<%@ page contentType="text/html;charset=utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>统计单词个数</title>
</head>
<body>
<%
String str="hello,no,tell book desk;table;chair;moon";
pageContext.setAttribute("str", str);
%>
<%--输出str --%>
${pageScope.str}<br>
<%--使用forToken标签统计字符创str中所包含的单词个数,分隔符为", ;" --%>
<%
int a = 0;
pageContext.setAttribute("count", a);
%>
<c:forTokens var="item" items="${str}" delims=", ;">
${item}<br>
<%
int count = (Integer) pageContext.getAttribute("count");
count++;
pageContext.setAttribute("count", count);
%>
</c:forTokens>
单词个数为:<c:out value="${pageScope.count}">0</c:out>
</body>
</html>