jsp

一、概述

JSP:表面上看就是html,可以显示图片,css效果,但是jsp可以在html 中写局部的java 代码,动态的获取数据

hml + java代码块

jsp:本质jsp 就是servlet
jsp —> java ---->class
作用:简化前端界面的开发,可以动态加载数据----》动态网页
在这里插入图片描述
在这里插入图片描述
注意事项:
在这里插入图片描述
在这里插入图片描述

二、jsp语法

<h1>基本语法</h1>

<%--
    java 代码块
--%>

<h3>   java 代码块</h3>
<%
    int a  = 1;

    int b = 2;
    int c = a + b;
    // 控制台输出
    System.out.println("c:"+c);

    // 输出到前端界面
    // out jsp内置对象 输出 相当于   resp.getWriter()
    out.print("c:"+c);
%>

<%--
    java 声明代码块
    <%!  在当前代码块 只能声明变量,声明方法
            不可以执行  业务,调用方法
--%>

<h3>声明代码块</h3>
<%!
     int  d;
     int  f = 12;

     // 声明方法
     int add(int a,int b){
         return a + b;
     }

%>
<h3>输出表达式</h3>
<%
     d = add(a,b);
     out.print("d:"+d);
%><br>

<%--
<%=   也是输出到前端界面
--%>
d:<%=d%><br>
result:<%=add(100, 200)%><br>

三、jsp注释

<%--
  Created by IntelliJ IDEA.
  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>
    <h1>注释</h1>
    注释有两种
    第一种(ctrl  + /):  jsp 本身的注释,该注释,只会在jsp界面(源码)显示,前端的jsp响应 不显示
    <%--
       jsp 本身的注释
    --%>
    <br>
    第二种注释:html 注释   当前注释,在返回给前端响应的源码 也会有该注释

    <!--
     html 注释
     -->
    <h5>两种注释都可以使用,但是涉及到源码关键业务 注释 尽量使用< %--  ,相对来说比较安全 </h5>
    </body>
</html>

四、jsp指令

JSP指令用来设置与整个JSP页面相关的属性。

指令描述
<%@ page … %>定义页面的依赖属性,比如脚本语言、error页面、缓存需求等等
<%@ include … %>包含其他文件
<%@ taglib … %>引入标签库的定义,可以是自定义标签
<%--
  Created by IntelliJ IDEA.
  To change this template use File | Settings | File Templates.
--%>

<%--
默认添加的指令
contentType = response.setContentType
language="java" 代表当前页面使用java
isELIgnored="false" 是否忽略 EL 表达式
import  导入类(分开写,一般自动导入)
session="true"  当session为true时,如果访问当前的jsp界面服务器没有创建会话,则自动创建对话,如果为false,没有会话就不创建
                  默认只要访问jsp,服务器都会默认创建会话
isErrorPage="true"  标记当前jsp是一个错误处理的界面
errorPage="error.jsp"   如果当前界面有异常,就会显示异常界面,url不变
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java"
         isELIgnored="false" session="true"  errorPage="error.jsp"
%>
<%@ page import="com.qfedu.test.User" %>
<html>
<head>
    <title>指令</title>
</head>
<body>
<%
    User user = new User();

    /*500服务器异常*/

    // int i = 1/0;
%>

<%--
    在多个界面有共同的代码使用,可以将共同的代码进行抽离
    ,封装为一个界面
--%>
<h1>包含其他页面</h1>
<%@include file="header.jsp"%>
<br>

<%@include file="footer.jsp"%>




</body>
</html>

五、动作标签

动作标签简单理解 就是命令

格式:jsp:动作

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
    <body>
        <h1>动作标签</h1>

<%--            将其他jsp包含到当前jsp中 和<@include一样--%>
<%--        / 相当于web/            --%>
            <jsp:include page="header.jsp"></jsp:include>

            <jsp:include page="/footer.jsp"></jsp:include>
            <br>


<%--        创建User对象
            scope=""  作用域
--%>
        <jsp:useBean id="user" class="com.qfedu.test.User"></jsp:useBean>

        <jsp:setProperty name="user" property="id" value="1"></jsp:setProperty>
        <jsp:setProperty name="user" property="name" value="chenmotong"></jsp:setProperty>

        id:
        <jsp:getProperty name="user" property="id"/><br>
        name:
        <jsp:getProperty name="user" property="name"/>
        <br>

        
<%--        转发到其他jsp
            转发:自己接收到请求,可以在request增加属性,但是自己不直接响应给前端,而是交给其他servlet或jsp
--%>

        <%--                不要在动作标签内部写注释,会报错--%>
<%--        为转发到hello.jsp增加请求参数--%>
            <jsp:forward page="hello.jsp">
                <jsp:param name="username" value="xiaoming"/>
            </jsp:forward>--%>


    </body>
</html>

六、内置对象

内置对象:jsp 转化为servlet 类,tomcat 加载java 遍历实例,jsp 对应的实例中已经创建很多内置对象(九大内置对象),不需要自己new 对象,在jsp Java代码块可已直接使用 九大内置对象

对象名类型说明
requestjavax.servlet.http.HttpServletRequest
responsejavax.servlet.http.HttpServletResponse
sessionjavax.servlet.http.HttpSession由session=“true”开关
applicationjavax.servlet.ServletContext
configjavax.servlet.ServletConfig
exceptionjava.lang.Throwable由isErrorPage=“false”开关
outjavax.servlet.jsp.JspWriterjavax.servlet.jsp.JspWriter
pageContextjavax.servlet.jsp.PageContext
pagejava.lang.Object当前对象this当前servlet实例
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
    <body>
        <h1>四大作用域对象</h1>

    <%
        pageContext.setAttribute("code","111");
        session.setAttribute("code","222");
        request.setAttribute("code","333");
        application.setAttribute("code","444");


    %>
    <%=pageContext.getAttribute("code")%>
        <br>
        <%out.println("---------");%>
        <br>
    <%=session.getAttribute("code")%>
        <br>
        <%out.println("---------");%>
        <br>
    <%=request.getAttribute("code")%>
        <br>
        <%out.println("---------");%>
        <br>
    <%=application.getAttribute("code")%>
    </body>
</html>

七、EL表达式

概述:

el 表达式:${}

EL表达式${requestScope.name},用来获取隐式对象中的值(属性),显示到html

EL表达式:作用就是简化jsp 的界面的输出,更好的和html 兼容,不需要java代码块,直接在html 书写el表达式就可以获取 隐式对象中的值(属性)

requset.setAttribut("username","xiaoming")

显示在前端的三种方式:

<%
	out.print("username:"+requset.getAttribut("username"))
%>
username:<%=requset.getAttribut("username")%>

el表达式:
username:${username}

隐式对象

隐含对象**描述
pageScopepage 作用域 pageContext
requestScoperequest 作用域 requst
sessionScopesession 作用域 sesion
applicationScopeapplication 作用域 application
paramRequest 对象的参数,字符串
paramValuesRequest对象的参数,字符串集合
headerHTTP 信息头,字符串
headerValuesHTTP 信息头,字符串集合
initParam上下文初始化参数
cookieCookie值
pageContext当前页面的pageContext

el表达式获取对象中的值

<%@ page import="com.qfedu.test.User" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.HashMap" %><%--
  Created by IntelliJ IDEA.
  User: 薛云翔
  Date: 2021/4/14
  Time: 21:30
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>el</title>
</head>
    <body>
        <%
            request.setAttribute("username","chenmotong");
        %>
        使用%= 输出username:<%=request.getAttribute("username")%><br>

        <h2>获取 隐式对象 requestScope的属性</h2>

        使用el表达式的第一种方式输出username:${username}<br>
        使用el表达式的第一种方式输出username:${requestScope.username}<br>
        使用el表达式的第一种方式输出username:${requestScope["username"]}<br>


        <h2>通过 el 表达式获取内置对象中属性值 值为对象</h2>
        <%
            User user1 = new User(1,"chenmotong");
            User user2 = new User(2,"chuzihang");
            User user3 = new User(3,"lumingze");

            request.setAttribute("user1",user1);

            List<User> list = new ArrayList<>();

            list.add(user1);
            list.add(user2);
            list.add(user3);

            request.setAttribute("list",list);

            HashMap map = new HashMap();
            map.put("a","AAA");
            request.setAttribute("map",map);
        %>

        使用 el 表达式输出user.username:${user1.name}<br>
        使用 el 表达式输出user.username:${user1["name"]}<br>
        使用 el 表达式输出user.username:${requestScope.user1.name}<br>

        <h2>使用 el 表达式输出 集合中的对象</h2>
        <%-- el 表达式获取 数组中的数据必须使用  []--%>
        使用 el 表达式输出list[1].name:${list[1].name}
        使用 el 表达式输出list[1].name:${requestScope.list[1].name}

        使用 el 表达式输出map.a:${map.a}<br>
        使用 el 表达式输出map.a:${map["a"]}<br>

    </body>
</html>

User.java

package com.qfedu.test;

import java.io.Serializable;

/**
 * @author xue_yun_xiang
 * @create 2021-04-14-20:10
 */

/*
 * 创建实体类
 *   1.set get
 *   2.toString
 *   3.实现Serializable接口
 *           作用:1.将对象保存到文件中,读取文件中的对象信息,转化为对象
 *                2.便于对象在网络中传输,一个对象可以通过tcp/udp转发到另一个应用
 *   4.如果写了有参构造,必须写无参构造
 *
 * */
public class User implements Serializable {
    private int id;
    private String name;

    public User() {

    }

    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

el表达式运算

<H1>el 表达式运算</H1>
<%-- el 表达式运算--%>
1+1=${1+1}<br>

<%
    request.setAttribute("s1", "hello");
%>
el-s1:${s1} <br>
%=-s1:<%=request.getAttribute("s1")%> <br>
empty s1:${empty s1}  <br>

<h5>获取s2</h5>
<%--
    el 获取不存在的 属性为 空
    <%=  获取不存在的 属性为 null
    ${empty  可以用来验证 属性 是否存在
--%>
el-s2:${s2} <br>
%=-s2:<%=request.getAttribute("s2")%> <br>
empty s1:${empty s2}  <br>

八、 四大作用域优先级

<h1>四大作用域的 优先级</h1>

<h5>
    通过el 直接或 $  {属性 }  此时他会先去 pageScope ,---》 requestScope---》sessionScope--》applicationScope
                             优先级从左到右 ,越来越低
                              因为   pageScope 只针对当前 jsp 有效
                                    requestScope   在同一个请求中有效(转发也有效)
                                    sessionScope 在同一会话中有效    
</h5>
<%

//    pageContext.setAttribute("code", "1");
//    request.setAttribute("code", "2");
    session.setAttribute("code", "3");
    application.setAttribute("code", "4");
%>

获取code:${code}<br>
获取pageScope.code:${pageScope.code}<br>
获取requestScope.code:${requestScope.code}<br>
获取sessionScope.code:${sessionScope.code}<br>
获取applicationScope.code:${applicationScope.code}<br>

九、Cookie

<h1>cookie 获取值</h1>

<%
    Cookie cookie = new Cookie("age", "18");
    // 向浏览器写入cookie
    response.addCookie(cookie);
%>
<%--
    获取的 是浏览器提交的cookie
--%>
获取cookie--value:${cookie.age.value}<br>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值