java-JSP01

1 篇文章 0 订阅

JSP

1. JSP技术概述
目前的问题:
	1. Java 程序和 HTML 页面没有分离
	2. HTML 页面在 Java 程序组装过程中,内存占用极高,效率极低
	3. 对于其他技术的兼容性,可行但不可取

JSP 可以简单解决当前问题
	JSP ==> Java Server Page Java 服务器页面。支持 HTML CSS JS JQuery 等一系列前端技术。。。
	JSP 是一个 Servlet 的程序。
	.jsp ==> .java ==> .class

JSP
	脚本,标签啊~~~不重要
	【重点】
		内置对象 => 四大域对象
		EL 表达式
		JSTL 表达式
2. 第一个 JSP 页面
<%--
  Created by IntelliJ IDEA.
  User: Anonymous
  Date: 2022/6/16
  Time: 10:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>第一个 JSP 页面</title>
  </head>
  <body>
  <h1>JSP 页面</h1>
  </body>
</html>

在这里插入图片描述

在这里插入图片描述

JSP文件
	index.jsp
Tomcat 解析:
	index.jsp ==> index_jsp.java
Tomcat 利用 Java 编译器编译得到 .class 字节码文件
	index_jsp.java ==> index_jsp.class
	
JSP ==> Java 程序,不是一门纯正的前端技术 ,Java + 前端的串儿
不是前后端分离技术。
3. JSP 脚本
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.HashMap" %>
<%--
  Created by IntelliJ IDEA.
  User: Anonymous
  Date: 2022/6/16
  Time: 10:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>01-JSPScript.jsp</title>
</head>
<body>

<!-- HTML 注释 -->
<%-- JSP 脚本 JSP 注释--%>
<%
    // Java 代码
    ArrayList<String> list = new ArrayList<>();
    HashMap<String, Object> map = new HashMap<>();  

    list.add("牛肉面");
    list.add("羊肉汤");
    list.add("羊杂汤");

    System.out.println(list);
    // out 是 JSP 内置对象 可以将数据内容发送到 HTML 页面
    out.write("<h1>" + list.toString() + "</h1>");
%>
<%-- JSP 输出脚本 --%>
<h1><%=list.toString()%></h1>
</body>
</html>

4. JSP 指令
4.1 page 页面配置
当前页面的 page 信息
<%--
  Created by IntelliJ IDEA.
  User: Anonymous
  Date: 2022/6/16
  Time: 11:09
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8"
         language="java"
         isErrorPage="false"
         session="false"
         isELIgnored="true"
         errorPage="error.jsp"
         import="java.util.ArrayList"
%>
<%--
contentType 页面内容类型和编码集问题

language 解析当前 JSP 页面对应的语句,默认 Java

isErrorPage 当前页面是否为错误信息页面,默认为 false ,false不能使用内置对象 exception
        如果为 true ,当前 JSP 页面可以使用内置对象 exception ==> java.lang.Throwable

session 标记当前 JSP 页面是否可以使用 Session 对象,默认 true 可以使用
        如果为 false 当前 JSP 页面无法使用 JSP 内置对象 session ==> javax.servlet.HttpSession

isELIgnored 是否支持 EL 表达式,默认为 true JSP 页面可以使用 EL 表达式
            如果赋值 false 当前 JSP 页面无法使用 EL 表达式【不推荐】

errorPage 指定当前页面出现错误之后,错误信息处理页面

import 可以用于导入 Java中或者第三方 Jar 包,不常用
--%>
<html>
<head>
    <title>02-JSPPage01</title>
</head>
<body>
<%
    Object[] arr = new Object[10];
    arr[0] = 10;
    ArrayList<String> list = new ArrayList<>();
%>
<%--<%=exception%>--%>
<%--<%=session%>--%>
</body>
</html>
4.2 include 页面包含
<%--
  Created by IntelliJ IDEA.
  User: Anonymous
  Date: 2022/6/16
  Time: 11:24
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>主页</title>
</head>
<body>
<%--
include 指令在当前 JSP 页面编译过程之前,首先将其他引入融合到当前 JSP 页面中
引入的 JSP 页面不会有独立的编译过程,每一次引入都是新的内容,会导致数据冗余
静态引入,资源浪费
 --%>
<%@ include file="03-top.jsp"%>
<h1 style="color: red">主页内容</h1>
<%@ include file="03-footer.jsp"%>
</body>
</html>
4.3 taglib 第三方 库引入
JSP 页面为解决完全脱离 Java 代码的格式,可以引入第三方的 JSTL 表达式,需要导入第三方 Jar 包,同时 JSP
页面需要引入对应的标签标记
第三方 Jar 包
	jstl-1.2.jar

tablib 引入标签前缀和对应标准规范
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: Anonymous
  Date: 2022/6/16
  Time: 11:35
  To change this template use File | Settings | File Templates.
--%>
<%@ 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>
<c:if test="${10 > 5}">
    <h1>你好</h1>
</c:if>
</body>
</html>

5. JSP 动作
5.1 jsp:include
<jsp:include page="03-top.jsp"/>
<h1 style="color: pink"> JSP:include JSP 动作引入 JSP 其他页面</h1>
<jsp:include page="03-footer.jsp"/>
5.2 jsp:forward
<%--JSP forward 转发操作,直接转发到对应资源目标,没有参数信息 --%>
<jsp:forward page="03-JSPInclude.jsp"/>
<%-- 转发到其他资源 JSP servlet 带有其他参数--%>
<jsp:forward page="06-target.jsp">
    <jsp:param name="name" value="Feri"/>
    <jsp:param name="age" value="65"/>
    <jsp:param name="gender" value="0"/>
</jsp:forward>
6. JSP 内置对象
6.1 内置对象名称和对应数据类型
内置对象数据类型
requestHttpServletRequest 请求对象
sessoinHttpSession Session 对象
applicationServletContext 整个 WEB Application 对象
pageContextthis/Object 当前 JSP 页面
exceptionThrowable Java 中的异常和错误的超类
outJspWriter ==> resp.getWriter(); 对 HTML 页面输出对象
page当前 JSP 页面
responseHttpServletResponse 响应对象
configServletConfig Servlet配置对象
6.2 域对象【重点】
6.2.1 request

在这里插入图片描述

6.2.2 session

在这里插入图片描述

6.2.3 Application 和 page

在这里插入图片描述

7. EL 表达式【重点】
7.1 EL 表达式运算符和判断操作
<%@ page import="java.util.ArrayList" %><%--
  Created by IntelliJ IDEA.
  User: Anonymous
  Date: 2022/6/16
  Time: 16:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>EL表达式运算符和判断使用</title>
    <style>
        ul {
        }
    </style>
</head>
<body>
<%--
算术运算符 + - * / % ()
逻辑运算符 || && !
关系运算符 > < >= <= != ==
三目运算符 ? :

重点:
    empty
    1. 可以判断集合,数组,Map 中是否有数据内容,size() length
    2. 可以判断指定元素是否为 null
    tips:
        not empty 是一个取反
--%>
<ul>
    <li>${1 + 5}</li>
    <li><%=1 + 5%></li>
    <li>${10 > 5 && 5 > 20}</li>
    <li>${10 > 5 ? "你好" : "我好"}</li>
    <li>${10 * 20}</li>
    <li>${10 / 20}</li>
</ul>
<hr>
<%
    ArrayList<String> list = null;
//
//    list.add("芬达");
//    list.add("可乐");
//    list.add("雪碧");
//    list.add("旺仔");
//    list.add("AD钙奶");

    request.setAttribute("list", list);
%>
<%-- EL 表达式当中是可以直接操作获取 域对象数据内容,根据属性名获取对应内容 --%>
<h1>List集合是空的吗? ${empty list}</h1>
<h1>List集合不是空的吗? ${not empty list}</h1>
</body>
</html>

7.2 EL 解析 JavaBean
<%@ page import="com.qfedu.entity.Student" %><%--
  Created by IntelliJ IDEA.
  User: Anonymous
  Date: 2022/6/16
  Time: 17:01
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    Student student = new Student();

    student.setId(1);
    student.setName("苟磊");
    student.setAge(16);
    student.setGender(false);

    request.setAttribute("student", student);
%>

<table width="500px" border="1" align="center">
    <tr>
        <th colspan="4">学生信息</th>
    </tr>
    <tr>
        <th>ID</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>性别</th>
    </tr>
    <tr>
        <%-- JavaBean对象.成员变量名称 --%>
        <td>${student.id}</td>
        <td>${student.name}</td>
        <td>${student.age}</td>
        <td>${student.gender ? "女" : "男"}</td>
    </tr>
</table>
</body>
</html>
7.3 EL 解析 Map
<%@ page import="java.util.HashMap" %><%--
  Created by IntelliJ IDEA.
  User: Anonymous
  Date: 2022/6/16
  Time: 17:09
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    HashMap<String, Object> map = new HashMap<>(4);

    map.put("id", 1);
    map.put("name", "大哥");
    map.put("age", 15);
    map.put("gender", true);

    request.setAttribute("map", map);
%>
<table width="500px" border="1" align="center">
    <tr>
        <th colspan="4">学生信息</th>
    </tr>
    <tr>
        <th>ID</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>性别</th>
    </tr>
    <tr>
        <%-- Map对象.key 【墙裂推荐】 --%>
        <td>${map.id}</td>
        <td>${map.name}</td>
        <td>${map.age}</td>
        <td>${map.gender ? "女" : "男"}</td>
    </tr>
    <tr>
        <%-- Map对象[字符串键名] --%>
        <td>${map["id"]}</td>
        <td>${map["name"]}</td>
        <td>${map["age"]}</td>
        <td>${map["gender"] ? "女" : "男"}</td>
    </tr>
</table>
</body>
</html>
7.4 EL 解析 List and Array
<%@ page import="java.util.ArrayList" %><%--
  Created by IntelliJ IDEA.
  User: Anonymous
  Date: 2022/6/16
  Time: 17:15
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    ArrayList<String> list = new ArrayList<>();

    list.add("钟薛高");
    list.add("梦龙");
    list.add("哈根达斯");
    list.add("DQ");
    list.add("KFC");
    request.setAttribute("list", list);

    Object[] array = {"雷碧", "李子园", "板蓝根", "王老吉", "Red mo~~~"};
    request.setAttribute("array", array);
%>

<ol>
    <%-- 集合[下标] --%>
    <li>${list[0]}</li>
    <li>${list[1]}</li>
    <li>${list[2]}</li>
    <li>${list[3]}</li>
    <li>${list[4]}</li>
</ol>
<hr>
<ol>
    <%-- 集合.get(下标) --%>
    <li>${list.get(0)}</li>
    <li>${list.get(1)}</li>
    <li>${list.get(2)}</li>
    <li>${list.get(3)}</li>
    <li>${list.get(4)}</li>
</ol>
<hr>
<ul>
    <%-- 数组[下标] --%>
    <li>${array[0]}</li>
    <li>${array[1]}</li>
    <li>${array[2]}</li>
    <li>${array[3]}</li>
    <li>${array[4]}</li>
</ul>

</body>
</html>
8. JSTL 表达式【重点】

JSP 页面为解决完全脱离 Java 代码的格式,可以引入第三方的 JSTL 表达式,需要导入第三方 Jar 包,同时 JSP
页面需要引入对应的标签标记
第三方 Jar 包
jstl-1.2.jar

tablib 引入标签前缀和对应标准规范
<%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>

8.1 JSTL if
<%@ page import="java.util.ArrayList" %><%--
  Created by IntelliJ IDEA.
  User: Anonymous
  Date: 2022/6/16
  Time: 17:29
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%-- 引入 JSTL 标签库 --%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%--
if 判断
if (10 > 5) {
}
--%>

<c:if test="${10 > 15}">
    <h1>路见不平一声吼,大哥没有男朋友</h1>
</c:if>
<c:if test="${10 > 5}">
    <h1>两只黄鹂鸣翠柳,大哥做饭全靠吼</h1>
</c:if>
<hr>
<%
    ArrayList<String> list = new ArrayList<>();
//    list.add("老邢");
//    list.add("阳哥");
//    list.add("琳哥");
//    list.add("东哥");
//    list.add("苟磊 Dog");

    request.setAttribute("list", list);
%>
<c:if test="${not empty list}">
    <h1>解析数据</h1>
</c:if>
<c:if test="${empty list}">
    <h1>没有数据</h1>
</c:if>
</body>
</html>

8.2 JSTL foreach
<%@ page import="java.util.ArrayList" %><%--
  Created by IntelliJ IDEA.
  User: Anonymous
  Date: 2022/6/16
  Time: 17:40
  To change this template use File | Settings | File Templates.
--%>
<%@ 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>
<%

    ArrayList<String> list = new ArrayList<>();
    list.add("老邢");
    list.add("阳哥");
    list.add("琳哥");
    list.add("东哥");
    list.add("苟磊 Dog");

    request.setAttribute("list", list);
%>
<ul>
    <c:if test="${not empty list}">
        <%--
            items=${集合/数组}
            var=循环使用的临时变量
        --%>
        <c:forEach items="${list}" var="name">
            <li>${name}</li>
        </c:forEach>
    </c:if>
</ul>
</body>
</html>

8.3 JSTL choose
<%--
  Created by IntelliJ IDEA.
  User: Anonymous
  Date: 2022/6/16
  Time: 19:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>03-jstl</title>
</head>
<body>
<%
    request.setAttribute("score", 60);
%>
<c:choose >
    <c:when test="${score >= 90}">
        <h1>秀儿</h1>
    </c:when>
    <c:when test="${score >= 80}">
        <h1>良儿</h1>
    </c:when>
    <c:when test="${score >= 70}">
        <h1>中儿</h1>
    </c:when>
    <c:when test="${score >= 60}">
        <h1>过儿</h1>
    </c:when>
    <c:when test="${score < 60}">
        <h1>让嫩爹穿着拖孩来学校一趟</h1>
    </c:when>
</c:choose>

</body>
</html>


html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>03-jstl</title>
</head>
<body>
<%
    request.setAttribute("score", 60);
%>
<c:choose >
    <c:when test="${score >= 90}">
        <h1>秀儿</h1>
    </c:when>
    <c:when test="${score >= 80}">
        <h1>良儿</h1>
    </c:when>
    <c:when test="${score >= 70}">
        <h1>中儿</h1>
    </c:when>
    <c:when test="${score >= 60}">
        <h1>过儿</h1>
    </c:when>
    <c:when test="${score < 60}">
        <h1>让嫩爹穿着拖孩来学校一趟</h1>
    </c:when>
</c:choose>

</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值