sven_meye
sven_meye

Reputation: 13

Java ResultSet to JSP

Can I transmit a ResultSet from a Servlet to a JSP file and using it in a c:forEach ?

At the moment I'm doing it like this:

<sql:query dataSource="${datasource}" var="result">SELECT * FROM events WHERE DATE >= CURDATE() ORDER BY Date</sql:query>
<c:forEach var="events" items="${result.rows}">

But I want to create the ResultSet with Java like this:

String sqlSelectEvents = "SELECT * FROM events";
pstmt = (PreparedStatement) con.prepareStatement(sqlSelectEvents);
ResultSet rs = pstmt.executeQuery();
request.setAttribute("rs", rs);

And then in JSP view:

<c:forEach var="events" items="${rs.rows}">

Upvotes: 1

Views: 144

Answers (2)

Your Hope
Your Hope

Reputation: 3

You should set a Attribut in the .java file.

Code for Java part:

String sqlSelectEvents = "SELECT * FROM events";
pstmt = (PreparedStatement) con.prepareStatement(sqlSelectEvents);
ResultSet rs = pstmt.executeQuery();
request.getSession().setAttribute("resultset", rs);

code for JSP part:

< c:forEach var="events" items="${resultset.rows}">

Upvotes: 0

SpringLearner
SpringLearner

Reputation: 13844

you can create a list and then transfer that list to jstl.

  1. create a list say resultList
  2. loop through resultset and add items to resultList
  3. pass this list to jstl and using <c:foreach> loop it and get the elements

Upvotes: 1

Related Questions