• JSP简介


    目录

    JSP

    JSP脚本片段

    面试题:JSP和Servlet的区别

    JSP表达式

    JSP声明片段

    JSP的指令标识

    page指令中的属性:

    includ指令:

    JSP标签

    1.内置标签

    2.JSTL标签,需要导入JSTL标签库

    3.自定义标签

    使用JSP的小练习

    在web.xml中设置的错误页面

    EL表达式

    EL表达式的内置作用域对象

    EL表达式的缺陷:


    JSP

    JSP脚本片段

    用于在JSP页面写java代码(可以当成一个类来写)

    1. <%
    2. int num = 0;
    3. num++;
    4. //在控制台打印输出
    5. System.out.println(num);
    6. //向页面打印输出
    7. out.print(num);
    8. %>

    注意事项:

    • JSP脚本片段中只能出现java代码,不能出现HTML元素。在访问JSP时,JSP引擎会编译JSP页面中的脚本片段(可能会导致有些卡顿,若数据过多,会很卡)。

    • JSP脚本片段中的java代码必须严格遵守java的规则

    • 一个JSP页面是可以有多个脚本片段的

    • 多个脚本片段中的代码可以相互访问

    淘汰原因

    • 必须有java环境

    • 必须有Tomcat环境

    • 需要解析编译,再渲染页面(会卡顿)

    面试题:JSP和Servlet的区别

    • JSP本质上就是一个Servlet类

    • JSP更侧重与视图的展示,Servlet更侧重于逻辑处理

    • 先有的Servlet,后有的JSP

    JSP表达式

    <%= num %>

    JSP声明片段

    1. <%!
    2. int x = 10;
    3. //声明静态代码块
    4. static{
    5.        
    6.   }
    7. //声明方法
    8. public void fun(){
    9.        
    10.   }
    11. %>

    JSP的指令标识

    1. <%-- 导入包(自动导入,使用import) --%>
    2. <%@ page import="java.util.List" %>
    3. <%@ page import="java.util.ArrayList" %>
    4. <%
    5. <%--需要导入包 --%>
    6. List list = new ArrayList();
    7. %>
    8. <%--
    9. JSP的指令标识
    10. <%@ 指令名 属性1="值1" 属性2="值2" ......%>
    11. page指令:定义整个JSP页面的相关属性
    12. include指令:引入其他的JSP页面,先把两个页面结合,再去编译成servlet
    13. taglib指令:引入页面上需要用到的标签库
    14. --%>

    page指令中的属性:

    errorPage属性:错误页面

    出现错误时会跳转到该页面,地址栏并不会改变

    1. <%--
    2.  Created by IntelliJ IDEA.
    3.  User: Administrator
    4.  Date: 2022/8/31
    5.  Time: 10:45
    6.  To change this template use File | Settings | File Templates.
    7. --%>
    8. <%-- errorPage:错误页面,出现错误时会跳转到该页面,地址栏并不会改变 --%>
    9. <%@ page errorPage="error.jsp" contentType="text/html;charset=UTF-8" language="java" %>
    10. <html>
    11. <head>
    12.    <title>Title</title>
    13. </head>
    14. <body>
    15. <%
    16.    int i = 10 / 0;
    17. %>
    18. <h1>JSP02 Page!!!</h1>
    19. </body>
    20. </html>

    isErrorPage属性:是否为错误页面

    1. <%--
    2.  Created by IntelliJ IDEA.
    3.  User: Administrator
    4.  Date: 2022/8/31
    5.  Time: 10:51
    6.  To change this template use File | Settings | File Templates.
    7. --%>
    8. <%-- isErrorPage:是否为错误页面,只有为是时才可以调用exception --%>
    9. <%@ page isErrorPage="true" contentType="text/html;charset=UTF-8" language="java" %>
    10. <html>
    11. <head>
    12.    <title>Title</title>
    13. </head>
    14. <body>
    15.    <h1>Error Page!!!</h1>
    16.   <%=
    17.   <%-- 将错误打印在页面上 --%>
    18.       exception.getMessage()
    19.   %>
    20. </body>
    21. </html>

    includ指令:

    1. <%-- hello.jsp文件中的内容,其余略 --%>
    2. <h1>hello Page!!!</h1>
    3. <%-- jsp02.jsp文件 --%>
    4. <%--
    5.  Created by IntelliJ IDEA.
    6.  User: Administrator
    7.  Date: 2022/8/31
    8.  Time: 10:45
    9.  To change this template use File | Settings | File Templates.
    10. --%>
    11. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    12. <%@include file="hello.jsp"%>
    13. <html>
    14. <head>
    15.    <title>Title</title>
    16. </head>
    17. <body>
    18. <h1>JSP02 Page!!!</h1>
    19. </body>
    20. </html>
    ​
    会打印出
    hello Page!!!
    JSP02 Page!!!

    JSP标签

    1.内置标签

    • jsp:include——引入指定的页面

    1. <%-- hello.jsp文件中的内容,其余略 --%>
    2. <h1>hello Page!!!</h1>
    3. <%-- jsp03.jsp文件中的内容,其余略 --%>
    4. <h1>JSP03 Page!!!</h1>
    5. <jsp:include page="hello.jsp"></jsp:include>
    输出;
    hello Page!!!
    JSP02 Page!!!
    • jsp:forward:转发页面

    当访问该页面时转发到其余页面,地址栏不会改变

    <jsp:forward page="hello.jsp"></jsp:forward>
    • jsp:param:传参数

    可与forward标签配合使用,转发的同时传递一些参数

    1. <jsp:forward page="hello.jsp">
    2.   <%-- 可以使用request.getAttribute() --%>
    3. <jsp:param name="num1" value="10"/>
    4. </jsp:forward>

    面试题:jsp:include标签和include指令的区别

    include标签:先把要引入的页面编译,再合并

    include指令:先把要引入的页面合并,再编译

    2.JSTL标签,需要导入JSTL标签库

    3.自定义标签

    使用JSP的小练习

    1. import com.jsoft.entity.Student;
    2. import javax.servlet.*;
    3. import javax.servlet.http.*;
    4. import javax.servlet.annotation.*;
    5. import java.io.IOException;
    6. import java.io.PrintWriter;
    7. import java.util.ArrayList;
    8. import java.util.List;
    9. import java.util.Objects;
    10. @WebServlet(name = "LoginJspServlet", value = "/loginjsp.do")
    11. public class LoginJspServlet extends HttpServlet {
    12.    @Override
    13.    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    14.        request.setCharacterEncoding("utf-8");
    15.        String username = request.getParameter("username");
    16.        String password = request.getParameter("password");
    17.        PrintWriter out = response.getWriter();
    18.        List<Student> students = new ArrayList<>(5);
    19.        students.add(new Student(1001,"aaa",25,"Man"));
    20.        students.add(new Student(1002,"bbb",26,"Man"));
    21.        students.add(new Student(1003,"ccc",28,"Man"));
    22.        students.add(new Student(1004,"ddd",27,"Man"));
    23.        students.add(new Student(1005,"eee",24,"Man"));
    24.        if(Objects.equals(username,"admin") && Objects.equals(password,"123456")){
    25.            // 将集合传入
    26.            request.setAttribute("students",students);
    27.            request.getRequestDispatcher("jsp/welcome.jsp").forward(request,response);
    28.       }else {
    29.            out.write("username or password wrong");
    30.       }
    31.   }
    32. }
    1. <%@ page import="com.jsoft.entity.Student" %>
    2. <%@ page import="java.util.List" %><%--
    3.  Created by IntelliJ IDEA.
    4.  User: Administrator
    5.  Date: 2022/8/31
    6.  Time: 13:37
    7.  To change this template use File | Settings | File Templates.
    8. --%>
    9. <%@ page pageEncoding="utf-8" contentType="text/html;charset=UTF-8" language="java" %>
    10. <html>
    11. <head>
    12.    <meta charset="UTF-8">
    13.    <title>Title</title>
    14. </head>
    15. <body>
    16. welcome:<%= request.getAttribute("username")%>
    17. <%
    18.    // 使用request.getAttribute("students")获取students集合
    19.    List<Student> students = (List<Student>) request.getAttribute("students");
    20.    // 遍历集合并显示在页面上,table表格展示
    21.    // 1.在JSP脚本片段中只能写java   2.在body中只能写html
    22. %>
    23. <table border="1" cellpadding="10" cellspacing="0">
    24.    <tr>
    25.        <th>ID</th>
    26.        <th>NAME</th>
    27.        <th>AGE</th>
    28.        <th>GENDER</th>
    29.    </tr>
    30.   <%
    31.   // 遍历students集合,并将信息输入到表格之中
    32.        for (Student student : students) {
    33.   %>
    34.        <tr>
    35.            <td><%= student.getId()%></td>
    36.            <td><%= student.getName()%></td>
    37.            <td><%= student.getAge()%></td>
    38.            <td><%= student.getGender()%></td>
    39.        </tr>
    40.   <%
    41.       }
    42.   %>
    43. </table>
    44. </body>
    45. </html>
     
    
    1. /*
    2. 对象 类
    3. pageContext--PageContext
    4.    request------HttpServletRequest
    5.    session------HttpSession
    6.    application--ServletContext
    7. */
    8. <%
    9. pageContext.setAttribute("pageContext","pageContext");
    10. request.setAttribute("request","request");
    11. session.setAttribute("session","session");
    12. application.setAttribute("application","application");
    13. %>
    14. <%-- 获取值 --%>
    15. pageContext:<%= pageContext.getAttribute("pageContext")%>
    16. request:<%= request.getAttribute("request")%>
    17. session:<%= session.getAttribute("session")%>
    18. application:<%= application.getAttribute("application")%>

    在web.xml中设置的错误页面

    不论哪里出了该类型的错误,都会跳转到该页面

    1. "1.0" encoding="UTF-8"?>
    2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4.         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    5.         version="4.0">
    1. <!-- 页面中所有出现404错误的都会跳转到404.html中 -->
    2.    <error-page>
    3.        <error-code>404</error-code>
    4.        <location>/404.html</location>
    5.    </error-page>
    6.    
    7. <!-- 页面中所有出现空指针错误的都会跳转到npe.html中 -->
    8.    <error-page>
    9.        <exception-type>java.lang.NullPointerException</exception-type>
    10.        <location>/npe.html</location>
    11.    </error-page>
    12. </web-app>

    EL表达式

    EL表达式的内置作用域对象

    EL表达式的内置作用域对象(由小到大)

    1. pageContext

    2. requestScope

    3. sessionScope

    4. applicationScope

    1.   <%
    2.        request.setAttribute("name","zhangsan");
    3.        session.setAttribute("name","lisi");
    4.        pageContext.setAttribute("age",30);
    5.   %>
    1. <%-- ${sessionScope.name}可获取属性为name的值,还可处理null,若为空,则文本框中不会显示 --%>
    2. <input type="text" value="${sessionScope.name}">
    3. <%-- 若为表明是哪一个内置对象的属性,则会先从范围小的内置对象找起 --%>
    4. <%-- 输出zhangsan --%>
    5. ${name}

    EL表达式的缺陷:

    1. 只能读,不能写

    2. 不支持流程控制语句

    1. <%--
    2.  Created by IntelliJ IDEA.
    3.  User: Administrator
    4.  Date: 2022/8/31
    5.  Time: 16:01
    6.  To change this template use File | Settings | File Templates.
    7. --%>
    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    3. <html>
    4. <head>
    5.    <title>Title</title>
    6. </head>
    7. <body>
    8.    
    9.   <%--  向指定的作用域中设置值  --%>
    10.    <c:set scope="session" var="name" value="zhangsan"></c:set>
    11.    <c:set scope="session" var="age" value="20"></c:set>
    12.    
    13.   <%--  取出session中的name的值  --%>
    14.   ${sessionScope.name}
    15.    <hr>
    16.   <%-- 判断 --%>
    17.    <c:if test="${sessionScope.age >= 18}">可以观看!</c:if>
    18.    <c:if test="${sessionScope.age < 18}">禁止观看!</c:if>
    19.    <hr>
    20.   <%-- 类似于switch...case --%>
    21.    <c:choose>
    22.        <c:when test="${sessionScope.age eq 18}">
    23.           你已经年满18岁,可以签署劳动合同了!
    24.        </c:when>
    25.        <c:when test="${sessionScope.age lt 18}">
    26.           你好没有满18岁!
    27.        </c:when>
    28.        <c:otherwise>
    29.           你已经是大人了!!!
    30.        </c:otherwise>
    31.    </c:choose>
    32.    <hr>
    33.    
    34.   <%-- begin="1"从多少开始 end="10"到哪里结束 step="2"一次增加多少 var="i"变量名 varStatus="stat"变量的状态,一般情况均为索引(index) --%>
    35.    
    36.    <c:forEach begin="1" end="10" step="2" var="i" varStatus="stat">
    37.       <%-- 判断是否为第一个,值为truefalse --%>
    38.       ${i} ----- ${stat.first} <br>
    39.    </c:forEach>
    40. </body>
    41. </html>
  • 相关阅读:
    bash for循环
    配置cri-docker使kubernetes1.24以docker作为运行时
    法国博士后招聘|国家健康与医学研究院(INSERM)-计算化学
    Java反射基础
    2022-08-22 第六小组 瞒春 学习笔记
    【刷题笔记】动态规划
    消费者需求疲软,京东第二季度业绩好于预期,618“立大功”
    ELK分布式日志
    《向量数据库指南》——押注向量数据库 挑战颇多
    机器学习作业3____决策树(CART算法)
  • 原文地址:https://blog.csdn.net/Dshuai7191/article/details/126681091