JSP基础知识点(详细总结)

目录

1、JSP

1.1、JSP概述

1.1.1、什么是JSP

1.1.2、JSP的本质

1.2、脚本和注释

1.2.1、JSP脚本

1.2.2、JSP和Servlet的区别

1.2.3、JSP注释

1.3、JSP指令

1.3.1、什么是指令?

1.3.2、指令格式

1.3.3、page指令

1.3.4、include指令

1.3.5、taglib指令

1.4、JSP内置对象

1.4.1、九大内置对象

1.4.2、常用内置对象

1.4.3、pageContext

1.5、JSP标签介绍

1.5.1、jsp:useBean标签

1.5.2、jsp:include标签

1.5.3、jsp:forward标签

2、EL表达式

2.1、EL取值

2.1.1、pageContext域

2.1.2、数组

2.1.3、list集合

2.1.4、map集合

2.1.5、java对象

2.1.6、集合对象

2.2、EL运算

2.2.1、常见的运算符

3、JSTL的核心标签库

3.1、什么是JSTL

3.2、JSTL的作用

3.3、c:if标签

3.4、c:choose、c:when、c:otherwise标签

3.5、c:set标签

3.6、c:out标签

3.7、c:forEach标签


 

1、JSP

1.1、JSP概述

1.1.1、什么是JSP

在很多动态网页中,绝大部分内容都是固定不变的,只有局部内容需要动态产生和改变。 为了弥补Servlet的缺陷,SUN公司在Servlet的基础上推出了JSP(Java Server Pages)页面服务器。

JSP是简化Servlet编写的一种技术,它将Java代码和HTML语句混合在同一个文件中编写,页面动态资源使用java代码,页面静态资源使用html标签。

简单来说:可以在html标签中嵌套java代码。

作用:简化书写,展示动态页面。

1.1.2、JSP的本质

JSP本质上就是一个Servlet。

JSP页面,运行的时候 ,会先生成一个Java文件,必须进行编译,再去执行。

C:\Users\Administrator\.IntelliJIdea2018.3\system\tomcat\Tomcat_9_0_54_day03_JDBCTemplate_MVC\work\Catalina\localhost\ROOT\org\apache\jsp

login.jsp 会被翻译成login_jsp.java,再编译时期被编译为login_jsp.class文件,jsp页面的名字加上下划线加上jsp。


每一段html的内容,都会被翻译成一个out.write("")这样的一段代码。service方法中。 

1.2、脚本和注释

1.2.1、JSP脚本

<% 代码 %>

脚本片段,生成在service方法中,每次请求的时候都会执行

<%! 代码 %>

声明片段,在java代码中声明成员,放在jsp生成java文件中的成员位置

<%=代码 %>

输出脚本片段,相当于out.print("代码") 方法,输出到jsp页面

    <%!
        //成员变量
        int a = 10;
        //成员方法
        public int sum(int a ,int b ){
            return a+b;
        }
        //内部类
        class A{ }
    %>
    <%    
         //普通代码-任意代码
        for (int i = 1; i <=10 ; i++) {
            // System.out.println(i);
            out.print(i);
        }
    %>
    
    <%= a %>

1.2.2、JSP和Servlet的区别

jsp主要以页面为主,尽量不要书写java代码。
servlet主要以业务逻辑为主,尽量不要书写页面。

1.2.3、JSP注释

html注释

<!-- 注释静态资源 -->

JSP注释

<%-- 注释所有 --%>

Java注释(JSP脚本内使用)

// 单行
/* 多行 */
/**文档 */

JSP注释的作用范围 

html注释可以通过查看网页源代码可以查看

jsp注释可以通过查看网页源代码不可以查看

1.3、JSP指令

1.3.1、什么是指令?

用于配置JSP页面,导入资源文件。

1.3.2、指令格式

<%@ 指令名称 属性名1="属性值1" 属性名2="属性值2" ...%>

1.3.3、page指令

主要用于配置当前jsp页面的一些信息

属性名说明
pageEncoding编码方式
language目前仅支持java语言
import导入jar包
errorPage当前页面报错后,跳转指定错误提示页面
isErrorPage声明当前jsp页面是一个异常处理页面,打开异常开关 false:(默认) true:可以操作exception异常对象
autoFlush它是指在使用out方法输出的时候,是否自动刷新. 默认是true。会自动刷新
buffer="8kb"当前页面的输出流的缓冲区大小。如果设置buffer=0 ,表示没有缓冲区
isELIgnored是否运行使用el表达式 ,false允许使用 true是不允许使用。
session指定一个http会话中是否使用会话

1、配置单个友好报错页面

errorPage属性

<%@ page contentType="text/html;charset=UTF-8" language="java" errorPage="/500error.jsp" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        int a = 1/0;//当页面发生错误,跳转到指定的错误页面(配置友好页面)
    %>
</body>
</html>

2、配置全局友好报错页面

    <error-page>
        <!--配置错误状态码-->
        <error-code>404</error-code>
        <!--配置错误友好页面-->
        <location>/404error.jsp</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/500error.jsp</location>
    </error-page>

3、isErrorPage属性获取异常信息

<%@ page contentType="text/html;charset=UTF-8" language="java"  isErrorPage="true" %>
<html>
<head>
    <title>当前是一个错误的友好页面</title>
</head>
<body>
    <%--只有isErrorPage="true" 才可以使用exception对象 --%>
    <%= exception.getMessage()%>
</body>
</html>

1.3.4、include指令

页面包含的。导入页面的资源文件

<%@include file="xxx.jsp"%>
<h2>我是头部</h2>
<h2>我是脚部</h2>
<%@include file="header.jsp"%>
	<h2>我是身体</h2>
<%@include file="fooder.jsp"%>

1.3.5、taglib指令

导入资源, 在jstl中我们会使用该指令导入标签库

属性名说明
prefix自定义前缀
<%@ page contentType="text/html;charset=UTF-8" language="java"  %>
<%--引入jstl核心标签库,前提得导入jstl相关的jar包--%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%--引入taglib指令,就可以使用相关的标签库--%>
</body>
</html>

1.4、JSP内置对象

作用:在JSP页面中不需要获取和创建,可以直接使用的对象,查看jsp底层源码service方法。

1.4.1、九大内置对象

变量名说明作用
pageContextPageContext当前页面中共享数据(域对象)
requestHttpServletRequest一次请求中共享数据(域对象)
session         HttpSession一次会话中共享数据(域对象)
applicationServletContext整个web应用共享数据(域对象)
responseHttpServletResponse响应对象
page(this)Object当前页面(servlet)对象
outJSPWriter输出对象
configServletConfigservlet配置对象
exceptionThrowable异常对象(默认关闭...)

1.4.2、常用内置对象

变量名说明
pageContext当前页面的域对象和获取其他八个内置对象。
request接收用户请求(参数)和 获取一次请求中域对象。
response设置响应:字节流和字符流。
out

专门在jsp中处理字符流。

print(): 可以输出一切类型。

write():只能输出字符类型。

1.4.3、pageContext

向四大域中存取数据

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>Title</title>
    </head>
    <body>
        <%
            pageContext.setAttribute("username","tom");
            pageContext.setAttribute("username","tom", PageContext.PAGE_SCOPE);
            pageContext.setAttribute("username","jack",PageContext.REQUEST_SCOPE);
            pageContext.setAttribute("username","rose",PageContext.SESSION_SCOPE);
            pageContext.setAttribute("username","yiyan",PageContext.APPLICATION_SCOPE);
        %>

        page:<%= pageContext.getAttribute("username")%><br>
        page:<%= pageContext.getAttribute("username",PageContext.PAGE_SCOPE)%><br>
        request:<%= pageContext.getAttribute("username",PageContext.REQUEST_SCOPE)%><br>
        session:<%= pageContext.getAttribute("username",PageContext.SESSION_SCOPE)%><br>
        application:<%= pageContext.getAttribute("username",PageContext.APPLICATION_SCOPE)%><br>

        <hr>
        <%--findAttribute  page request session application 范围查找--%>
        find:<%= pageContext.findAttribute("username") %>
        
    </body>
</html>

四个域对象作用范围

page  <  request  <  session  < ServletContext(application)

1.5、JSP标签介绍

1.5.1、jsp:useBean标签

代替java代码在jsp中的书写,用标签即可完成

    <%
        User user = new User();
        user.setUsername("jack");
        user.getUsername();
    %>
    <%--相当于User user = new User()--%>
    <jsp:useBean id="user" class="cn.itssl.bean.User" scope="page"/>
    
    <%--user.setUsername("jack")--%>
    <jsp:setProperty name="user" property="username" value="jack"/>

    <%--user.getUsername()--%>
    <jsp:getProperty name="user" property="username"/>

1.5.2、jsp:include标签

    <jsp:include page="header.jsp"/>
    <h2>我是内容</h2>
    <jsp:include page="foot.jsp"/>

1.5.3、jsp:forward标签

可以实现转发

 <jsp:forward page="login.html"/>

2、EL表达式

EL 全名为Expression Language,翻译成中文表达式语言
语法:${标识符} 作用就是输出标识符的所代表的值。
el出现目的:代替<%= %>,代替out.print();
常用功能:
获取各种域(各种数据范围,servletContext(全局的),request(一次请求)中存储的数据

2.1、EL取值

2.1.1、pageContext域

<%
    pageContext.setAttribute("username","tom");
    pageContext.setAttribute("username","tom", PageContext.PAGE_SCOPE);
    pageContext.setAttribute("username","jack",PageContext.REQUEST_SCOPE);
    pageContext.setAttribute("username","rose",PageContext.SESSION_SCOPE);
    pageContext.setAttribute("username","yiyan",PageContext.APPLICATION_SCOPE);
%>	

    ${pageScope.username}
    ${requestScope.username}
    ${sessionScope.username}
    ${applicationScope.username}
	
	<hr>
	<%--也可不指定范围,从page reqeust session application 找--%>
    ${username}

2.1.2、数组

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //业务层   dao
        //把查到的数据返回给web层
        int[] arr = {11,22,33,44,55,66};
        request.setAttribute("arr",arr);
        request.getRequestDispatcher("show.jsp").forward(request,response);

    }

show.jsp 

<body>    
	${requestScope.arr[0]}
    ${requestScope.arr[1]}
    ${requestScope.arr[2]}
    ${requestScope.arr[3]}
    ${requestScope.arr[4]}
    ${requestScope.arr[5]}

    <c:forEach items="${requestScope.arr}" var="i">
        ${i}
    </c:forEach>
 </body>

2.1.3、list集合

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //web service  dao
        //把查询的结果放在list集合
        List<String> list = new ArrayList<>();
        list.add("tom");
        list.add("jack");
        list.add("rose");
        list.add("yiyan");
        request.setAttribute("list",list);
        request.getRequestDispatcher("show.jsp").forward(request,response);

    }

show.jsp 

<body>

    <c:forEach items="${requestScope.list}" var="list">
        ${list}
    </c:forEach>

</body>

2.1.4、map集合

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //web service  dao
        //把查询的结果放在map集合
        Map<String,String> map = new HashMap<>();
        map.put("a","aaa");
        map.put("b","bbb");
        map.put("c","ccc");
        map.put("d","ddd");

        request.setAttribute("map",map);
        request.getRequestDispatcher("show.jsp").forward(request,response);
    }

show.jsp 

<body>

    ${requestScope.map.a}
    ${requestScope.map.b}
    ${requestScope.map.c}
    ${requestScope.map.d}

    <c:forEach items="${map}" var="map">
        key:${map.key}
        value:${map.value}
    </c:forEach>

</body>
<body>
	<%--如果map中的key数据,不能直接取出来,需要[]--%>
    ${requestScope.map["1"]}
    ${requestScope.map["2"]}
    ${requestScope.map["3"]}
    ${requestScope.map["4"]}

    <c:forEach items="${map}" var="map">
        key:${map.key}
        value:${map.value}
    </c:forEach>

</body>

2.1.5、java对象

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

        //web service  dao
        //把查询的结果放在user对象
        User user = new User();
        user.setUsername("jack");
        user.setAddr("郑州");

        request.setAttribute("user",user);
        request.getRequestDispatcher("show.jsp").forward(request,response);

    }
<body>

    <%--直接对象.属性名--%>
    ${requestScope.user.username}
    ${requestScope.user.addr}

</body>

2.1.6、集合对象

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

        //web service  dao
        //把查询的结果放在list集合
        List<User> list = new ArrayList<>();

        User user = new User();
        user.setUsername("jack");
        user.setAddr("郑州");
        
        list.add(user);

        request.setAttribute("list",list);
        request.getRequestDispatcher("show.jsp").forward(request,response);

    }
<body>

    <c:forEach items="${requestScope.list}" var="user">
        ${user.username}
        ${user.addr}
    </c:forEach>

</body>

2.2、EL运算

2.2.1、常见的运算符

运算符说明
算术运算符+-*/(div)%(mod)
比较运算符><>=<===!=
逻辑运算符&&(and)||(or)、 !(not)
空运算符empty:用于判断字符串、集合、数组对象是否为null或者长度是否为0
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>Title</title>
    </head>
    <body>
        <%
           int a = 4;
           int b = 3;

           request.setAttribute("a",a);
           request.setAttribute("b",b);
        %>

        <h3>算数运算符</h3>
        ${a / b} | ${a div b} <br>
        ${a % b} | ${a mod b} <br>

        <h3>比较运算符</h3>
        ${a == b} | ${a eq b} <br>
        ${a != b} | ${a ne b} <br>

        <h3>三元运算符</h3>
        ${a == b ? "a等于b" : "a不等于b"} <br>

        <h3>非空判断</h3>
        <%
            User user= null;
            request.setAttribute("user",user);

            List<String> list = new ArrayList<>();
            request.setAttribute("list",list);
        %>

        <%--
            相当于 if(user != null)
        --%>
        user对象: ${not empty user} <br>
        user对象: ${empty user} <br>

        <%--
             相当于 if(list != null && list.size() > 0)
        --%>
        list对象: ${not empty list} <br>
        list对象: ${empty list} <br>

    </body>
</html>

3、JSTL的核心标签库

3.1、什么是JSTL

JavaServer Pages Tag Library JSP标准标签库 是由Apache组织提供的开源的免费的jsp标签<标签>

3.2、JSTL的作用

用于简化和替换jsp页面上的java代码的编写

注意事项:需要手动添加jstl的jar包

3.3、c:if标签

相当于java代码中if语句

使用c:if 标签,在JSP页面上可以完成if判断。注意在JSTL核心标签库中没有c:else.只有c:if

<body>

    <%
        int a = 10;
        request.setAttribute("a",a);
    %>

    <c:if test="${requestScope.a > 10 }" var="a">
        登录失败!
    </c:if>

    <c:if test="${requestScope.a == 10}" var="a">
        登录成功!
    </c:if>


</body>

3.4、c:choose、c:when、c:otherwise标签

c:choose标签用于指定多个条件选择的组合边界,它必须与c:when和c:otherwise标签一起使用。

使用c:choose,c:when和c:otherwise三个标签,可以构造类似 “if-else if-else” 的复杂条件判断结构。

c:when ,c:otherwise 属于同一级别标签。同时是c:choose的子标签

它可以理解成switch结构。 ​ c:choose 相当于switch ​ c:when 相当于 case ​ c:otherwise 相当于 default

<body>

    <%
        int a = 10;
        request.setAttribute("a",a);
    %>

    <c:choose>
        <c:when test="${a==1}">星期一</c:when>
        <c:when test="${a==2}">星期二</c:when>
        <c:when test="${a==3}">星期三</c:when>
        <c:otherwise>星期八</c:otherwise>
    </c:choose>

</body>

3.5、c:set标签

<body>

    <%--
        var:变量名
        value: 值
        scope: 范围
    --%>
    <c:set var="name" value="张三" scope="page"/>

    ${pageScope.name}

    <%
        User user = new User();
        user.setUsername("jack");
        request.setAttribute("user",user);
    %>

    原始数据:
    ${requestScope.user.username}

    <%--修改数据--%>
    <c:set target="${user}" property="username" value="rose"/>

    修改后数据:
    ${requestScope.user.username}
</body>

3.6、c:out标签

<body>

    <c:set var="name" value="张三" scope="page"/>

    <%--out是输出标签, value: 要取的值 default:如果你没有取到值给出默认值--%>
    <c:out value="${name123}" default="法外狂徒"/>

    <%
        String str ="<a href='https://www.baidu.com'>百度一下</a>";
        request.setAttribute("str",str);
    %>

    <%--
        escapeXml:表示输出的内容是html相关的,是否按照html格式输出
        true 默认:不会按html格式输出
        false: 按html格式输出
    --%>
    <c:out value="${str}" escapeXml="false"/>

</body>

3.7、c:forEach标签

<body>

    <%
        int[] arr ={11,22,33,44,55,66};
        request.setAttribute("arr",arr);
    %>
    <c:forEach items="${arr}" var="i">
        ${i}
    </c:forEach>

    <hr>
    <c:forEach begin="0" end="4" step="2" var="i">
        ${arr[i]}
    </c:forEach>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值