怎么让jsp页面打印resultset集合的结果
时间: 2024-05-09 13:19:51 浏览: 5
可以使用JSTL标签库中的<c:forEach>标签来遍历resultset集合,并在页面上展示出来。具体步骤如下:
1. 在JSP页面中引入JSTL标签库:
```
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
```
2. 在页面中使用<c:forEach>标签来遍历resultset集合,例如:
```
<table>
<thead>
<tr>
<th>列名1</th>
<th>列名2</th>
<th>列名3</th>
</tr>
</thead>
<tbody>
<c:forEach var="row" items="${resultset}">
<tr>
<td>${row.column1}</td>
<td>${row.column2}</td>
<td>${row.column3}</td>
</tr>
</c:forEach>
</tbody>
</table>
```
其中,${resultset}表示resultset集合的变量名,${row.column1}表示row对象的column1属性值。
3. 在Servlet中查询数据库并将结果集存入resultset变量中,例如:
```
ResultSet resultset = statement.executeQuery("SELECT * FROM table_name");
request.setAttribute("resultset", resultset);
```
4. 最后,在页面中调用Servlet来获取结果集并展示出来,例如:
```
<form action="servlet_url" method="get">
<button type="submit">查询</button>
</form>
<c:if test="${not empty resultset}">
<table>
...
</table>
</c:if>
```