Java 将 ResultSet 从 servlet 传递到 JSP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1726041/
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
pass ResultSet from servlet to JSP
提问by Dave
I am doing the following in my SampleServlet.java
我在 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
然后在我的 jsp DisplayPropeties.jsp 我有
<%
ArrayList rows = new ArrayList();
if (request.getSession().getAttribute("propertyList") != null) {
rows = (ArrayList ) request.getSession().getAttribute("propertyList");
}
%>
but rows
is always null.
但rows
始终为空。
What am I doing wrong?
我究竟做错了什么?
采纳答案by Murali VP
I don't understand how rows can be null
given your if
statement there.
我不明白如何null
在if
那里给出您的语句。
Anyway, shouldn't it be request.getAttribute("propertyList")
in the DisplayProperties.jsp
?
无论如何,它不应该request.getAttribute("propertyList")
在DisplayProperties.jsp
?
回答by BalusC
You've the answer, so I am only going to do an enhancement suggestion: do notuse scriptlets in JSP. Use taglibs and EL where appropriate. An example to generate a list would be:
你已经回答,所以我只打算做一个增强的建议:不要没有在JSP中使用小脚本。在适当的地方使用 taglibs 和 EL。生成列表的示例是:
<ul>
<c:forEach items="${propertyList}" var="item">
<li>${item}</li>
</c:forEach>
</ul>
You can do the same for HTML table
s and dropdown option
s. Hope this helps.
您可以对 HTML table
s 和 dropdown option
s执行相同的操作。希望这可以帮助。
回答by duffymo
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.
您也不应该在 JSP 中使用 ResultSet。那是一个数据库游标,一种稀缺资源。您可能会在一个简单的页面上让它“工作”,但我敢打赌,您没有明确的责任来关闭代码中的 ResultSet、Statement 或 Connection。您很快就会用完并想知道为什么您的代码会因异常而崩溃。
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.
任何 java.sql 接口实现都不应脱离定义良好的持久层。获取连接,获取ResultSet,将其映射为对象或数据结构,并以获取的相反顺序关闭您的所有资源,然后将对象或数据结构返回到您的JSP,只用JSTL编写,没有脚本,用于显示。这是正确的做法。
If you MUST use SQL in a JSP, use the JSTL <sql> tags to do it.
如果您必须在 JSP 中使用 SQL,请使用 JSTL <sql> 标记来执行此操作。
回答by saksham
Use
用
request.getSession().setAttribute("propertyList", Rows);
instead of
代替
request.setAttribute("propertyList", Rows);
in your servlet code. It will work perfectly.
在您的 servlet 代码中。它会完美地工作。