备工作1:tomcat10.1.13版本,可去官网下载,不会下载的童靴看过来:如何正确下载tomcat???_明天更新的博客-CSDN博客
准备工作2:添加架包(需要三个架包)
jstl架包,下载地址:有道云笔记
准备好这三个架包后,创建一个普通的JavaWeb项目。
不太了解创建步骤的看这里:idea如何建立web项目???_明天更新的博客-CSDN博客
1.搭建servlet环境
详细步骤:servlet初体验之环境搭建!!!_明天更新的博客-CSDN博客
2.if标签库的使用
Java代码(Servlet):
/*
* Copyright (c) 2020, 2023, All rights reserved.
*
*/
package cn;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* <p>Project: servlet-web-jstl - UserListServlet</p>
* <p>Powered by scl On 2023-09-01 20:52:50</p>
* <p>描述:<p>
*
* @author 孙臣龙 [1846080280@qq.com]
* @version 1.0
* @since 17
*/
@WebServlet("/userListServlet")
public class UserListServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
req.setCharacterEncoding("utf-8");
String age = req.getParameter("age");
req.setAttribute("age",age);
req.getRequestDispatcher("user.jsp").forward(req,resp);
}
}
jsp代码:因为要使用到标签库,一定要加上这个:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 适用tomcat10以上的版本
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${age}
<c:if test="${age>18}">
<h1>年龄大于18岁</h1>
</c:if>
<c:if test="${age<18}">
<h1>年龄小于18岁</h1>
</c:if>
<c:if test="${age==18}">
<h1>年龄为18岁,恭喜你以成年!!</h1>
</c:if>
</body>
</html>
结果显示:
3.foreach标签库的使用
Java代码(user类):
/*
* Copyright (c) 2020, 2023, All rights reserved.
*
*/
package cn;
/**
* <p>Project: servlet-web-jstl - UserEntity</p>
* <p>Powered by scl On 2023-09-02 10:47:58</p>
* <p>描述:<p>
*
* @author 孙臣龙 [1846080280@qq.com]
* @version 1.0
* @since 17
*/
public class UserEntity {
/**
* 用户名称
*/
private String username;
/**
* 用户年龄
*/
private Integer age;
/**
* 状态 0:冻结状态 1:为冻结
*/
private Integer start;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getStart() {
return start;
}
public void setStart(Integer start) {
this.start = start;
}
public UserEntity(String username, Integer age, Integer start) {
this.username = username;
this.age = age;
this.start = start;
}
public UserEntity() {
}
}
Java代码(servlet):
/*
* Copyright (c) 2020, 2023, All rights reserved.
*
*/
package cn;
import com.sun.net.httpserver.HttpServer;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* <p>Project: servlet-web-jstl - UserListServletFor</p>
* <p>Powered by scl On 2023-09-02 10:45:16</p>
* <p>描述:<p>
*
* @author 孙臣龙 [1846080280@qq.com]
* @version 1.0
* @since 17
*/
@WebServlet("/userListServletFor")
public class UserListServletFor extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<UserEntity> list=new ArrayList<>();
list.add(new UserEntity("小明",14,0));
list.add(new UserEntity("小红",22,1));
list.add(new UserEntity("小绿",44,1));
list.add(new UserEntity("小王",18,0));
req.setAttribute("userList",list);
req.getRequestDispatcher("userList.jsp").forward(req,resp);
}
}
jsp页面:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: admin
Date: 2023/9/2
Time: 10:53
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>
<table border="1" align="center" style="border-collapse: collapse;width: 30%">
<tr align="center">
<th align="center">序号</th>
<th align="center">名称</th>
<th align="center">年龄</th>
<th align="center">冻结</th>
<th align="center">操作</th>
</tr>
<c:forEach items="${userList}" var="user" varStatus="m">
<tr align="center">
<td align="center">${m.index+1}</td>
<td align="center">${user.username}</td>
<td align="center">${user.age}</td>
<td align="center"><c:if test="${user.start==0}">冻结状态</c:if><c:if test="${user.start==1}">未冻结</c:if></td>
<td align="center"><a href="#">修改</a> <a href="#">删除</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
结果显示: