English 中文(简体)
pass ResultSet from servlet to JSP
原标题:

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?

最佳回答

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?

问题回答

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.

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.

Use

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

instead of

request.setAttribute("propertyList", Rows);

in your servlet code. It will work perfectly.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签