将 ResultSet 从 servlet 传递到 JSP

发布于 2024-08-11 04:52:24 字数 859 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

手心的海 2024-08-18 04:52:24

您也不应该在 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.

极致的悲 2024-08-18 04:52:24

我不明白鉴于您的 if 语句,行如何可以为 null

不管怎样,它不应该是 DisplayProperties.jsp 中的 request.getAttribute("propertyList") 吗?

I don't understand how rows can be null given your if statement there.

Anyway, shouldn't it be request.getAttribute("propertyList") in the DisplayProperties.jsp?

您已经有了答案,所以我只想做一个增强建议:不要在 JSP 中使用 scriptlet。在适当的地方使用标签库和 EL。生成列表的一个示例是:

<ul>
    <c:forEach items="${propertyList}" var="item">
        <li>${item}</li>
    </c:forEach>
</ul>

您可以对 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:

<ul>
    <c:forEach items="${propertyList}" var="item">
        <li>${item}</li>
    </c:forEach>
</ul>

You can do the same for HTML tables and dropdown options. Hope this helps.

梦回梦里 2024-08-18 04:52:24

使用

request.getSession().setAttribute("propertyList", Rows);

而不是

request.setAttribute("propertyList", Rows);

在您的 servlet 代码中 。它将完美地工作。

Use

request.getSession().setAttribute("propertyList", Rows);

instead of

request.setAttribute("propertyList", Rows);

in your servlet code. It will work perfectly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文