代码仓库地址+文档:https://gitee.com/DerekAndroid/JspDemo.git
效果(左边普通jsp,右边JSTL模式jsp):
案例一:查询所有商品信息
sql
-- 简易商品表
CREATE TABLE product(
id INT PRIMARY KEY AUTO_INCREMENT,
pname VARCHAR(20),
price DOUBLE,
pdesc VARCHAR(20)
);
INSERT INTO product VALUES (NULL,'电视机',3200,'液晶曲面大电视');
INSERT INTO product VALUES (NULL,'韭菜盒子',3,'味重请小心食用');
INSERT INTO product VALUES (NULL,'益达',10,'韭菜伴侣');
INSERT INTO product VALUES (NULL,'十三香',12,'守义牌');
核心代码:servlet请求转发到jsp
package com.itheima.servlet;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.itheima.bean.Product;
import com.itheima.service.ProductService;
public class ProductServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//创建ProductService
ProductService ps = new ProductService();
//调用查询所有商品的方法
List<Product> list=ps.findAllProducts();
//把list放入reuqest域中
request.setAttribute("list", list);
//请求转发到list.jsp中
request.getRequestDispatcher("/list.jsp").forward(request, response);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
list.jsp
<%@page import="com.itheima.bean.Product"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
List<Product> list = (List<Product>)request.getAttribute("list");
%>
<table border="1px" align="left">
<tr>
<th>商品id</th>
<th>商品名称</th>
<th>商品价格</th>
<th>商品描述</th>
</tr>
<%
if(list==null){
%>
<tr>
<td colspan="4">暂无商品</td>
</tr>
<%
}else{
for(Product pro:list){
%>
<tr>
<td><%=pro.getId() %></td>
<td><%=pro.getPname() %></td>
<td><%=pro.getPrice() %></td>
<td><%=pro.getPdesc() %></td>
</tr>
<%
}
}
%>
</table>
</body>
</html>
使用list-jstl.jsp
<%@page import="com.itheima.bean.Product"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1px" align="left">
<tr>
<th>商品id</th>
<th>商品名称</th>
<th>商品价格</th>
<th>商品描述</th>
</tr>
<c:if test="${empty list}">
<tr>
<td colspan="4">暂无商品,请添加</td>
</tr>
</c:if>
<c:if test="${not empty list}">
<c:forEach items="${list}" var="pro">
<tr>
<td>${pro.id}</td>
<td>${pro.pname}</td>
<td>${pro.price}</td>
<td>${pro.pdesc}</td>
</tr>
</c:forEach>
</c:if>
</table>
</body>
</html>