Results 1 to 3 of 3

Thread: How to I retrieve records from a db to send to JSP?

  1. #1
    Join Date
    Dec 2004

    How to I retrieve records from a db to send to JSP?

    Hello, long time php developer here. I'm used to using the following code to retrieve db records and return an array with the results:

    PHP Code:
    $query "SELECT * FROM users LIMIT 10";
    $result mysql_query($query);
    $rows = array();
    $row mysql_fetch_array($result)){
    How can i stuff the results into an array from a Java bean to send an array back to JSP for i can iterate through it in JSP? I'm already connected to the db, i can execute queries, and my bean is accessable from my jsp page... i just need to know how to set up the array and return it.

    Thanks again.

  2. #2
    Join Date
    Dec 2007
    Dayton, OH
    I'm not sure if you are using any sort of framework to send queries to your db(ie hibernate), but take a look at result sets.


  3. #3
    Join Date
    Jun 2009
    Best way to do this is to use a list.
    The JSP can get whatever the servlet gives using the request attribute.

    Servlet example:
    AddressBook addressBook = new AddressBook();
    List addressRows = addressBook.getAllAddresses();
    request.setAttribute("addressRows", addressRows);
    request.getRequestDispatcher("/WEB-INF/webpage/view.jsp").forward(request, response);
    Fetch in JSP using an iterator inside a Scriptlet:
    <&#37;@ page import="com.mypackage.AddressRowBean" %>
    <%@ page import="java.util.*" %>
    <jsp:useBean id="addressRows" type="java.util.List" scope="request"/>
      for(Iterator it = addressRows.iterator(); it.hasNext(); ){
        AddressRowBean row = (AddressRowBean)it.next();
      <%= row.getFirstname() %>
      <%= row.getLastname() %>
    or fetch in JSP using more efficient JSTL:
    <c:forEach var="row" items="${addressRows}">     
    Last edited by JavaServlet; 07-21-2009 at 08:20 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