<%@ page contentType="text/html;charset=UTF-8" language="java" %>
表达式语言
表达式语言 - 算术运算符
表达式语言
计算结果
\${1}
${1}
\${1.2 + 2.3}
${1.2 + 2.3}
\${1.2E4 + 1.4}
${1.2E4 + 1.4}
\${-4 - 2}
${-4 - 2}
\${21 * 2}
${21 * 2}
\${3/4}
${3/4}
\${3 div 4}
${3 div 4}
\${3/0}
${3/0}
\${10%4}
${10%4}
\${10 mod 4}
${10 mod 4}
\${(1==2) ? 3 : 4}
${(1==2) ? 3 : 4}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
表达式语言 - 逻辑运算符
表达式语言 - 逻辑运算符
表达式语言
计算结果
\${A&&B}
${A&&B}
\${ A and B}
${ A and B}
\${ A || B}
${ A || B}
\${A or B}
${A or B}
\${ !A}
${ !A}
\${not A }
${not A }
具体数据实例比较:
表达式语言
计算结果
\${12 == 12&&12 < 11}
${12 == 12&&12 < 11}
\${ 12 == 12 and 12 < 11}
${ 12 == 12and 12 < 11}
\${ 12 == 12 || 12 < 11}
${ 12 == 12|| 12 < 11}
\${12 == 12 or 12 < 11}
${12 == 12or 12 < 11}
\${ !true}
${ !true}
\${not true}
${not true }
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%request.setAttribute("now",new java.util.Date());%>
user变量指向的对象为null,表达式\${user}的输出结果为: ${user}
user变量指向的对象为null,表达式\${user.name}的输出结果为: ${user.name}
now变量指向的对象类型为Date,表达式\${now.time}的输出结果为: ${now.time}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%request.setAttribute("now",new java.util.Date());%>
user变量指向的对象为null,表达式\${user}的输出结果为: ${user}
user变量指向的对象为null,表达式\${user.name}的输出结果为: ${user.name}
now变量指向的对象类型为Date,表达式\${now.time}的输出结果为: ${now.time}
<%@ 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]异常
--%>
PI
3.14
<%@page contentType="text/html;charset=utf-8"%>
EL隐式对象的使用
<%
if (request.getParameter("mysubmit") != null) {
%>
圆的半径:${param.radius}
圆的周长:${param.radius*2*initParam.PI}
圆的面积:${param.radius*param.radius*initParam.PI}
IP地址:${pageContext.request.remoteAddr}
<%
// out.println(request.getRemoteAddr());
}
%>
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 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"%>
显示所有学生信息
添加学号
<%
ArrayList studentArrayList = (ArrayList) request.getSession().getAttribute("studentArrayList");
%>
学号
姓名
操作
${student.num}
${student.name}
修改
删除
<%@page contentType="text/html;charset=utf-8"%>
课堂测试
课堂测试,请先输入学号和密码…
<%
//根据错误类型,显示相应信息
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:
}
}
%>
<%@ page contentType="text/html;charset=utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
set_out_if 测试
学号:
密码:
5、程序功能:输出1—100之间,分别输出能被3、5、7整除的数。通过本程序学习JSTL中
<%@ page contentType="text/html;charset=utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
foreach_choose 测试
3的倍数:
5的倍数:
7的倍数:
其它数字:
6、程序功能是使用
<%@ page contentType="text/html;charset=utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
统计单词个数
<%
String str="hello,no,tell book desk;table;chair;moon";
pageContext.setAttribute("str", str);
%>
<%--输出str --%>
${pageScope.str}
<%--使用forToken标签统计字符创str中所包含的单词个数,分隔符为", ;" --%>
<%
int a = 0;
pageContext.setAttribute("count", a);
%>
${item}
<%
int count = (Integer) pageContext.getAttribute("count");
count++;
pageContext.setAttribute("count", count);
%>
单词个数为:0