编写一个servlet,在servlet中查询goods表中的记录,并在showMoveGoods.jsp页面中逆序显示偶数行的记录。在本例中需要使用实体类goods,但需要为Goods添加一个属性rowno封装行号。
时间: 2024-01-22 08:21:07 浏览: 73
首先,我们需要在实体类Goods中添加一个属性rowno来封装行号:
```java
public class Goods {
private int id;
private String name;
private double price;
private int rowno;
// 构造方法和getter/setter方法省略
}
```
接下来,我们编写Servlet来查询goods表并设置rowno属性:
```java
public class QueryGoodsServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Goods> goodsList = new ArrayList<>();
// TODO: 查询goods表并将结果保存到goodsList中,同时设置rowno属性
// 将goodsList保存到request对象中,以便在showMoveGoods.jsp中使用
request.setAttribute("goodsList", goodsList);
request.getRequestDispatcher("/showMoveGoods.jsp").forward(request, response);
}
}
```
在查询goods表时,我们需要设置rowno属性。可以使用一个计数器来记录当前是第几行,并在设置rowno属性时使用该计数器。另外,由于需要逆序显示偶数行的记录,我们可以在查询结果后对其进行反转。
```java
List<Goods> goodsList = new ArrayList<>();
int count = 1;
try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "password")) {
String sql = "SELECT id, name, price FROM goods";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
Goods goods = new Goods();
goods.setId(rs.getInt("id"));
goods.setName(rs.getString("name"));
goods.setPrice(rs.getDouble("price"));
goods.setRowno(count++);
goodsList.add(goods);
}
}
}
}
Collections.reverse(goodsList);
```
最后,我们编写showMoveGoods.jsp页面来逆序显示偶数行的记录:
```jsp
<%@ 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>
<html>
<head>
<meta charset="UTF-8">
<title>Show Move Goods</title>
</head>
<body>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Price</th>
</tr>
<c:forEach items="${goodsList}" var="goods">
<c:if test="${goods.rowno % 2 == 0}">
<tr>
<td>${goods.id}</td>
<td>${goods.name}</td>
<td>${goods.price}</td>
</tr>
</c:if>
</c:forEach>
</table>
</body>
</html>
```
在showMoveGoods.jsp页面中,我们使用<c:forEach>标签来遍历查询结果,并使用<c:if>标签来判断当前行是否为偶数行。如果是偶数行,则将该行记录显示在表格中。
完整的代码如下:
```java
public class QueryGoodsServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Goods> goodsList = new ArrayList<>();
int count = 1;
try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "password")) {
String sql = "SELECT id, name, price FROM goods";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
Goods goods = new Goods();
goods.setId(rs.getInt("id"));
goods.setName(rs.getString("name"));
goods.setPrice(rs.getDouble("price"));
goods.setRowno(count++);
goodsList.add(goods);
}
}
}
}
Collections.reverse(goodsList);
request.setAttribute("goodsList", goodsList);
request.getRequestDispatcher("/showMoveGoods.jsp").forward(request, response);
}
}
public class Goods {
private int id;
private String name;
private double price;
private int rowno;
public Goods() {
}
public Goods(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getRowno() {
return rowno;
}
public void setRowno(int rowno) {
this.rowno = rowno;
}
}
```
```jsp
<%@ 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>
<html>
<head>
<meta charset="UTF-8">
<title>Show Move Goods</title>
</head>
<body>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Price</th>
</tr>
<c:forEach items="${goodsList}" var="goods">
<c:if test="${goods.rowno % 2 == 0}">
<tr>
<td>${goods.id}</td>
<td>${goods.name}</td>
<td>${goods.price}</td>
</tr>
</c:if>
</c:forEach>
</table>
</body>
</html>
```
阅读全文