www.webdeveloper.com
Results 1 to 2 of 2

Thread: pass jstl sql query result to jsp

  1. #1
    Join Date
    Aug 2009
    Posts
    11

    pass jstl sql query result to jsp

    Hi I have the following code which works perfectly

    Code:
     <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    
    
    <sql:setDataSource dataSource="jdbc/dbtest"/>
    
    <sql:query var="items" sql="SELECT * FROM homepagesubscriptions">
    </sql:query>
    
    <table border="0" align="center" valign="top">
    
    <c:forEach var="row" items="${items.rows}" begin = "0" end="10" >
    			<tr>
    			<td>
    				<img src="${row.imgurl}" width="100" height = "100"></a>
    			</td>
    			<tr>
    </c:forEach>
    </table>
    Now what I want is to perform the query in one page and then display the data in another, as such
    index.jsp
    Code:
    <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
    
    <sql:setDataSource dataSource="jdbc/dbtest"/>
    
    <sql:query var="items" sql="SELECT * FROM homepagesubscriptions">
    </sql:query>
    
    <!-- NOT SURE WHAT TO DO HERE
    
     <jsp:useBean id="resultBean" scope="request"
         class="javax.servlet.jsp.jstl.sql.ResultSupport" />
     <jsp:setProperty name="rs" property="rs" />
    <jsp:forward page="test.jsp" />
    -->
    test.jsp
    Code:
     <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    
    <c:set var="items" value="${resultBean}" />
    
    <table border="0" align="center" valign="top">
    
    <c:forEach var="row" items="${items.rows}" begin = "0" end="10" >
    			<tr>
    			<td>
    				<img src="${row.imgurl}" width="100" height = "100"></a>
    			</td>
    			<tr>
    </c:forEach>
    </table>
    I know in index.jsp I can use jsp forward and usebean but am not sure how to? What type is the result from the query? thanks

  2. #2
    Join Date
    Jun 2009
    Posts
    98
    You need a request scope.

    I still recommend you use a Servlet Controller to work with JSP and SQL.
    Last edited by JavaServlet; 08-25-2009 at 05:00 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles