将ResultSet从servlet传递给JSP

时间:2022-02-27 08:10:02

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.

但行总是为空。

What am I doing wrong?

我究竟做错了什么?

4 个解决方案

#1


4  

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

鉴于你的if语句,我不明白行是如何为null的。

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

无论如何,它不应该是DisplayProperties.jsp中的request.getAttribute(“propertyList”)吗?

#2


6  

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编写,不使用scriplet进行显示。这是正确的做法。

If you MUST use SQL in a JSP, use the JSTL <sql> tags to do it.

如果必须在JSP中使用SQL,请使用JSTL 标记来执行此操作。

#3


2  

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:

你有答案,所以我只想做一个增强建议:不要在JSP中使用scriptlet。在适当的地方使用taglib和EL。生成列表的示例如下:

<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.

您可以对HTML表格和下拉选项执行相同的操作。希望这可以帮助。

#4


2  

Use

使用

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

instead of

代替

request.setAttribute("propertyList", Rows);

in your servlet code. It will work perfectly.

在您的servlet代码中。它会很完美。

#1


4  

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

鉴于你的if语句,我不明白行是如何为null的。

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

无论如何,它不应该是DisplayProperties.jsp中的request.getAttribute(“propertyList”)吗?

#2


6  

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编写,不使用scriplet进行显示。这是正确的做法。

If you MUST use SQL in a JSP, use the JSTL <sql> tags to do it.

如果必须在JSP中使用SQL,请使用JSTL 标记来执行此操作。

#3


2  

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:

你有答案,所以我只想做一个增强建议:不要在JSP中使用scriptlet。在适当的地方使用taglib和EL。生成列表的示例如下:

<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.

您可以对HTML表格和下拉选项执行相同的操作。希望这可以帮助。

#4


2  

Use

使用

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

instead of

代替

request.setAttribute("propertyList", Rows);

in your servlet code. It will work perfectly.

在您的servlet代码中。它会很完美。