JavaWeb_17_Jsp基础

本文介绍了JSP的基础知识,包括JSP的简介,如何将HTML转换为JSP以及JSP的动态包含功能,展示了如何通过jsp:include将top.jsp和menu.jsp动态插入到index.jsp中。此外,还讲解了JSTL标准标签库的使用,提供了导入依赖、创建User实体类、使用JSTL在jstl.jsp中展示用户列表的示例,强调了JSTL在简化代码和提高可读性方面的优势。
摘要由CSDN通过智能技术生成

《Jsp基础》

目录

  • Jsp简介(了解)
  • Jsp动态包含(掌握)
  • JSTL标签库(掌握)

一、Jsp简介

JSP(全称Java Server Pages)是由[Sun Microsystems](https://baike.baidu.com/item/Sun Microsystems)公司主导创建的一种动态网页技术标准。JSP部署于网络服务器上,可以响应客户端发送的请求,并根据请求内容动态地生成HTMLXML或其他格式文档的Web网页,然后返回给请求者。JSP技术以Java语言作为脚本语言,为用户的HTTP请求提供服务,并能与服务器上的其它Java程序共同处理复杂的业务需求。

html转jsp

<%-- page指令指定内容类型 --%>
<%@ page contentType="text/html;charset=UTF-8" %>
<%-- html代码 --%>
<html>
<head>
    <title>首页</title>
</head>
<body>

</body>
</html>

二、Jsp动态包含

大多数网站中,同类模块网页的风格都是一样的(如有相同的顶栏跟导航栏),而jsp内容也是可以动态拼接的。

案例一

  1. webapp/top.jsp
<%@ page contentType="text/html;charset=UTF-8" %>
<div class="top">
    顶栏
</div>
  1. webapp/menu.jsp
<%@ page contentType="text/html;charset=UTF-8" %>
<div class="menu">
    导航栏
</div>
  1. webapp/index.jsp
<%-- page指令指定内容类型 --%>
<%@ page contentType="text/html;charset=UTF-8" %>
<%-- html代码 --%>
<html>
<head>
    <title>首页</title>
    <meta charset="utf-8">
    <style>
        .container {
            margin: auto;
            width: 1000px;
        }

        .top {
            width: 100%;
            height: 80px;
            background-color: lightblue;
            font-size: 30px;
            text-align: center;
            line-height: 80px;
        }

        .main {
            margin-top: 10px;
            font-size: 20px;
            height: 400px;
            text-align: center;
            line-height: 400px;
            display: flex;
            flex-direction: row;
        }

        .menu {
            width: 26%;
            height: 100%;
            background-color: lightsalmon;
        }

        .content {
            width: 74%;
            height: 100%;
        }
    </style>
</head>
<body>
<div class="container">
    <jsp:include page="top.jsp"/>
    <div class="main">
        <jsp:include page="menu.jsp"/>
        <div class="content">
            内容块
        </div>
    </div>
</div>
</body>
</html>

执行结果

请添加图片描述

三、JSTL标签库

JSTL(Java server pages standarded tag library,即JSP标准标签库)是由JCP(Java community Proces)所制定的标准规范,它主要提供给Java Web开发人员一个标准通用的标签库,并由Apache的Jakarta小组来维护。开发人员可以利用这些标签取代JSP页面上的Java代码,从而提高程序的可读性,降低程序的维护难度。

案例二

  1. pom.xml中导入jstl依赖包;
<dependencies>
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>taglibs</groupId>
        <artifactId>standard</artifactId>
        <version>1.1.2</version>
    </dependency>
</dependencies>
  1. 修改index.jsp内容块为测试链接;
<div class="content">
    <a href="/index.do">测试jstl标签库</a>
</div>
  1. 新建com.hpr.entity包,创建User实体类;
package com.hpr.entity;

public class User {
    //私有化变量
    private int id;
    private String name;
    private int age;
    private String info;

    //无参构造
    public User() {
    }

    //带参构造
    public User(int id, String name, int age, String info) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.info = info;
    }

    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;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", info='" + info + '\'' +
                '}';
    }
}
  1. IndexServlet
@WebServlet("/index.do")
public class IndexServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        List<User> userList = new ArrayList<>();
        userList.add(new User(1001, "Alice", 18, "美丽动人"));
        userList.add(new User(1002, "Bob", 20, "英俊潇洒"));
        userList.add(new User(1003, "Clover", 26, null));
        userList.add(new User(1004, "Divid", 25, null));
        request.setAttribute("userList", userList);
        request.getRequestDispatcher("./jstl.jsp").forward(request, response);
    }
}
  1. jstl.jsp
<%@ page contentType="text/html;charset=UTF-8" %>

<%-- 引入jstl标签库,并将引用前缀设置为c --%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>
    <title>jstl测试</title>
    <meta charset="utf-8">
    <style>
        table {
            margin: 80px auto;
            border: 1px solid black;
        }

        td {
            width: 100px;
            height: 30px;
            text-align: center;
            line-height: 30px;
            border: 1px solid black;
        }
    </style>
</head>
<body>
<table>
    <caption>JSTL标签测试</caption>
    <c:forEach items="${userList}" var="user">
        <tr>
            <td>${user.id}</td>
            <td>${user.name}</td>
            <td>${user.age}</td>
            <td>
                <c:if test="${user.info==null}">
                    暂无描述
                </c:if>
                <c:if test="${user.info!=null}">
                    ${user.info}
                </c:if>
            </td>
        </tr>
    </c:forEach>
</table>
</body>
</html>
  1. 启动测试

请添加图片描述

执行结果

请添加图片描述

总结

重点

  1. jsp动态包含的使用;
  2. jstl标签库的使用。

难点

  1. jstl标签库的使用。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值