文章目录
一、JSP
Java Server Pages:java服务器端页面,也和servlet一样,用于动态web技术。JSP页面中可以嵌入java代码,为用户提供动态的数据
1.1 JSP基础知识语法
JSP表达式:
<%= 变量或者表达式%>
<%= new java.util.Date() %>
JSP脚本片段
<% jsp脚本片段 %>
<%
int sum = 0;
for(int i = 1 ; i <= 100 ; i++)
sum+=i;
out.println(sum);
%>
JSP声明:
<%! 声明块 %>
<%!
static{
System.out.println("Loading Servlet!");
}
private int globalvar = 0;
%>
JSP声明会被编译到JSP生成的Java的类中
JSP的注释:
<%--注释--%>
1.2 JSP指令
<%@page args...%>
<%@include file="" %>
<jsp:include page="/header.jsp"/>
网页主体
<jsp:include page="/footer.jsp"/>
1.3 九大内置对象
- PageContext
- Request
- Response
- Session
- Application[ServletContext]
- config[ServletConfig]
- out
- page
- exception
pageContext.setAttribute("name1","秦疆1号"); //保存的数据只在一个页面中有效
request.setAttribute("name2","秦疆2号"); //保存的数据只在一次请求中有效,请求转发会携带这个数据
session.setAttribute("name3","秦疆3号"); //保存的数据只在一次会话中有效,从打开浏览器到关闭浏览器
application.setAttribute("name4","秦疆4号"); //保存的数据只在服务器中有效,从打开服务器到关闭服务器,可以所有的用户共享
二、JSP标签、JSTL标签、EL表达式
添加依赖项:
<!-- JSTL表达式的依赖 -->
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/taglibs/standard -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl-api -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.web/jstl-impl -->
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jstl-impl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency>
将standard依赖包中的c.tld文件放到WEB-INF目录下
在页面中添加如下标签:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page isELIgnored="false" %>
EL表达式:${}
- 获取数据
- 执行运算
- 获取web开发常用对象
JSP标签的使用:
<!--转发页面并且传入参数-->
<jsp:forward page="/jsptag2.jsp">
<jsp:param name="name" value="fan"></jsp:param>
<jsp:param name="age" value="12"></jsp:param>
</jsp:forward>
<!--在转发的页面取参数-->
<%=request.getParameter("name")%>
<%=request.getParameter("age")%>
JSTL标签使用:使用之前需要引入核心标签
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
获取页面传送到服务器的url中的参数:
request.getParameter("username");
获取从服务器传来的结果:
${requestScope.username}
el表达式测试:
@WebServlet(name = "TestServlet", value = "/TestServlet")
public class TestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = "fan";
request.setAttribute("name",name);
request.getRequestDispatcher("/test.jsp").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
test.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
你好
${requestScope.name}
</body>
</html>
c:choose c:when测试:
<body>
<c:set var="score" value="55"/>
<c:choose>
<c:when test="${score >= 90}">
你的成绩为优秀
</c:when>
<c:when test="${score >= 80}">
你的成绩为一般
</c:when>
<c:when test="${score >= 70}">
你的成绩为良好
</c:when>
<c:when test="${score <= 60}">
你的成绩为不及格
</c:when>
</c:choose>
</body>
c:forEach测试
<%
ArrayList<String> people = new ArrayList<>();
people.add(0,"张三");
people.add(1,"李四");
people.add(2,"王五");
people.add(3,"赵六");
people.add(4,"田六");
request.setAttribute("list",people);
%>
<c:forEach var="people" items="${list}">
<c:out value="${people}" />
</c:forEach>
三、练习
1、用 JDBC + JSP + JSTL 实现:在浏览器页面显示已注册的用户列表(如果数据表中 有年龄低于 18 岁的用户就不显示),通过点击用户名能编辑用户信息并保存修改
数据库中:
User类:
package Pojo;
public class User {
private String username;
private String password;
private String gender;
private int age;
public User(String username, String password, String gender, int age) {
this.username = username;
this.password = password;
this.gender = gender;
this.age = age;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}