Java 显示从 servlet 到 jsp 页面的结果集值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22555112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
displaying resultset values from servlet to jsp page
提问by Ashish Srivastava
i am fetching the data from database by servlet and then setting it to resultset. Now i want to dislay data on the jsp page. for that i used list to set all data and with jstl i displayed at jsp page. but i am getting two column with same data. Here is the code..
我正在通过 servlet 从数据库中获取数据,然后将其设置为结果集。现在我想在jsp页面上显示数据。为此,我使用列表来设置所有数据,并使用 jstl 显示在 jsp 页面上。但我得到两列具有相同数据。这是代码..
SERVLET CODE:
服务代码:
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
wb.setDeviceAccount(rs.getString("accountID"));
wb.setVehicleId(rs.getString("deviceID"));
wb.setSimNumber(rs.getString("simID"));
wb.setImeiNumber(rs.getString("imeiNumber"));
wb.setLastTimestamp(rs.getString("lastGPSTimestamp"));
wb.setLastLoginTime(rs.getString("lastUpdateTime"));
wb.setExpirationTime(rs.getString("expirationTime"));
list.add(wb);
}
rs.close();
stmt.close();
con.close();
request.setAttribute("deviceList", list);
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/installedDeviceList.jsp");
requestDispatcher.forward(request, response);
JSP PAGE:
JSP页面:
<c:forEach items="${deviceList}" var="dList">
<tr>
<td></td>
<td>${dList.deviceAccount}</td>
<td>${dList.vehicleId}</td>
<td><a href="#" onclick="toggle();">${dList.simNumber}</a></td>
<td>${dList.imeiNumber}</td>
<td>${dList.lastTimestamp}</td>
<td>${dList.lastLoginTime}</td>
</tr>
</c:forEach>
采纳答案by Gautam
IN your loop always create new reference of Wb. here i am assuming Wb is your class name.you can change code based on your class name.
在您的循环中始终创建 Wb 的新参考。 这里我假设 Wb 是您的班级名称。您可以根据您的班级名称更改代码。
Wb wb=null;
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
wb =new Wb();
wb.setDeviceAccount(rs.getString("accountID"));
wb.setVehicleId(rs.getString("deviceID"));
wb.setSimNumber(rs.getString("simID"));
wb.setImeiNumber(rs.getString("imeiNumber"));
wb.setLastTimestamp(rs.getString("lastGPSTimestamp"));
wb.setLastLoginTime(rs.getString("lastUpdateTime"));
wb.setExpirationTime(rs.getString("expirationTime"));
list.add(wb);
}
回答by Gimby
You are creating only one object apparently while you should be creating a unique 'wb' instance for each row. You don't have two duplicate rows, you simply add the exact same object multiple times to the list.
您显然只创建了一个对象,而您应该为每一行创建一个唯一的“wb”实例。您没有两个重复的行,您只需将完全相同的对象多次添加到列表中。
回答by Ravinder Reddy
For each of the row in result set, create a new wb
object to store data from resultset and add the same to list.
对于结果集中的每一行,创建一个新wb
对象来存储结果集中的数据并将其添加到列表中。
Example:
示例:
while ( rs.next() ) {
// set this to equivalent class on your wb variable.
// wb = new wb();
wb.setDeviceAccount( rs.getString( "accountID" ) );
// read other column values here and set in wb
// ...
// lastly
list.add( wb );
} // while rs
Then on your web page, you will get each record read correctly from the list.
然后在您的网页上,您将从列表中正确读取每条记录。