《Jsp基础》
目录
一、Jsp简介
JSP(全称Java Server Pages)是由[Sun Microsystems](https://baike.baidu.com/item/Sun Microsystems)公司主导创建的一种动态网页技术标准。JSP部署于网络服务器上,可以响应客户端发送的请求,并根据请求内容动态地生成HTML、XML或其他格式文档的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内容也是可以动态拼接的。
案例一
- webapp/top.jsp
<%@ page contentType="text/html;charset=UTF-8" %>
<div class="top">
顶栏
</div>
- webapp/menu.jsp
<%@ page contentType="text/html;charset=UTF-8" %>
<div class="menu">
导航栏
</div>
- 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代码,从而提高程序的可读性,降低程序的维护难度。
案例二
- 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>
- 修改index.jsp内容块为测试链接;
<div class="content">
<a href="/index.do">测试jstl标签库</a>
</div>
- 新建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 + '\'' +
'}';
}
}
- 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);
}
}
- 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>
- 启动测试
执行结果
总结
重点
- jsp动态包含的使用;
- jstl标签库的使用。
难点
- jstl标签库的使用。