将 ResultSet 从 servlet 传递到 JSP
我在 SampleServlet.java 中执行以下操作
//Fill resultset from db
....
try {
ArrayList Rows = new ArrayList();
while (resultSet.next()){
ArrayList row = new ArrayList();
for (int i = 1; i <= 7 ; i++){
row.add(resultSet.getString(i));
}
Rows.add(row);
}
request.setAttribute("propertyList", Rows);
RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/DisplayProperties.jsp");
requestDispatcher.forward(request,response);
,然后在 jsp DisplayPropeties.jsp 中
<%
ArrayList rows = new ArrayList();
if (request.getSession().getAttribute("propertyList") != null) {
rows = (ArrayList ) request.getSession().getAttribute("propertyList");
}
%>
执行以下操作,但 rows
始终为 null。
我做错了什么?
I am doing the following in my SampleServlet.java
//Fill resultset from db
....
try {
ArrayList Rows = new ArrayList();
while (resultSet.next()){
ArrayList row = new ArrayList();
for (int i = 1; i <= 7 ; i++){
row.add(resultSet.getString(i));
}
Rows.add(row);
}
request.setAttribute("propertyList", Rows);
RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/DisplayProperties.jsp");
requestDispatcher.forward(request,response);
and then in my jsp DisplayPropeties.jsp I have
<%
ArrayList rows = new ArrayList();
if (request.getSession().getAttribute("propertyList") != null) {
rows = (ArrayList ) request.getSession().getAttribute("propertyList");
}
%>
but rows
is always null.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您也不应该在 JSP 中使用 ResultSet。那是数据库游标,一种稀缺资源。您可以让它在一个简单的页面上“工作”,但我敢打赌您没有明确的责任来关闭代码中的结果集、语句或连接。您很快就会用完并想知道为什么您的代码会因异常而崩溃。
任何 java.sql 接口实现都不应脱离定义良好的持久层。获取连接,获取 ResultSet,将其映射到对象或数据结构,并以与获取相反的顺序关闭所有资源,然后将对象或数据结构返回到 JSP(仅使用 JSTL 编写,不使用 scriplet)进行显示。这是正确的做法。
如果必须在 JSP 中使用 SQL,请使用 JSTL;标签来做到这一点。
You should also not be using a ResultSet in a JSP. That's a database cursor, a scarce resource. You may get this to "work" on a simple page, but I'd bet that you don't have clear responsibility for closing the ResultSet, Statement, or Connection in your code. You'll soon run out and wonder why your code is crashing with exceptions.
None of the java.sql interface implementations should escape out of a well-defined persistence layer. Acquire the connection, get the ResultSet, map it into an object or data structure, and close all your resources in reverse order of acquisition, then return the object or data structure to your JSP, written only with JSTL and no scriplets, for display. That's the right thing to do.
If you MUST use SQL in a JSP, use the JSTL <sql> tags to do it.
我不明白鉴于您的
if
语句,行如何可以为null
。不管怎样,它不应该是
DisplayProperties.jsp
中的request.getAttribute("propertyList")
吗?I don't understand how rows can be
null
given yourif
statement there.Anyway, shouldn't it be
request.getAttribute("propertyList")
in theDisplayProperties.jsp
?您已经有了答案,所以我只想做一个增强建议:不要在 JSP 中使用 scriptlet。在适当的地方使用标签库和 EL。生成列表的一个示例是:
您可以对 HTML
table
和下拉option
执行相同的操作。希望这有帮助。You've the answer, so I am only going to do an enhancement suggestion: do not use scriptlets in JSP. Use taglibs and EL where appropriate. An example to generate a list would be:
You can do the same for HTML
table
s and dropdownoption
s. Hope this helps.使用
而不是
在您的 servlet 代码中 。它将完美地工作。
Use
instead of
in your servlet code. It will work perfectly.