1

I'm new to Java and have been trying to output data from a database to a JSP using JSTL foreach.

I have been following the main response to this question: Show JDBC ResultSet in HTML in JSP page using MVC and DAO pattern. Although this hasn't proved successful for me as my code doesn't output the data.

I am using VSCode, Maven, JDK and Servlet 2.3

offered.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

///

<c:forEach items="${listings}" var="listing">
    <div class="l_tile">
        <div class="text">
            <div class="title">
                <c:out value="${listing.type}" /> -
                <c:out value="${listing.category}" />
            </div>
            <div class="title">
                <c:out value="${listing.title}" />
                <c:out value="${listing.condition}" />
            </div>
            <br>
            <div class="title">
                <c:out value="${listing.description}" />
            </div>
        </div>
        <div class="sector">
            <button class="view" id="myBtn">View</button>
        </div>
    </div>
</c:forEach>

view_listings_servlet.java

@WebServlet("/view_listings_servlet")
public class view_listings_servlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    private String url = "jdbc:sqlserver://localhost:1433;databaseName=CommunityRecycle;user=CommunityRecycleAdmin;password=59^7K1Nht#x3";
    private ListingDAO listingDAO;

    @Override
    public void init() {
        listingDAO = new ListingDAO(url);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        try {
            List<Listing> listings = listingDAO.list();
            System.out.println(listings);
            request.setAttribute("listings", listings); // Will be available as ${listings} in JSP
            request.getRequestDispatcher("offered.jsp").forward(request, response);

        } catch (Exception e) {
            System.out.println();
            e.printStackTrace();
        }
    }
}

ListingsDAO.java

public class ListingDAO {

    private String url;

    public ListingDAO(String url) {
        this.url = url;
    }

    public List<Listing> list() throws SQLException {
        List<Listing> listings = new ArrayList<Listing>();

        try (Connection connection = DriverManager.getConnection(url)) {
            CallableStatement viewListings_SP = connection.prepareCall("{call viewListings_SP(?)}");
            viewListings_SP.setInt("type", 2);
            ResultSet rs = viewListings_SP.executeQuery();

            while (rs.next()) {
                Listing listing = new Listing();
                listing.setType(rs.getString(1));
                listing.setTitle(rs.getString(2));
                listing.setCategory(rs.getString(3));
                listing.setDescription(rs.getString(4));
                listing.setCondition(rs.getString(5));
                listing.setState(rs.getString(6));
                listings.add(listing);
            }
        }

        return listings;
    }
}

Listing.java

public class Listing {

    private String type;
    private String title;
    private String category;
    private String description;
    private String condition;
    private String state;

    public String getType() {
        return type;
    }

    public String getTitle() {
        return title;
    }

    public String getCategory() {
        return category;
    }

    public String getDescription() {
        return description;
    }

    public String getCondition() {
        return condition;
    }

    public String getState() {
        return state;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setCondition(String condition) {
        this.condition = condition;
    }

    public void setState(String state) {
        this.state = state;
    }
}

Output of System.out.println(listings);

[Listing@15a1b7a4, Listing@7b2d8d89, Listing@529e2fd0]

The code outputs "${listing.type}", "${listing.category}" etc. instead of the actual data.

Any help with this will be greatly appreciated.

Tyreece
  • 27
  • 9
  • Can you please post the entire `offered.jsp` code? – Ramu Nov 08 '19 at 15:25
  • There's a whole lot of irrelevant code, so I have posted more which I deem relevant. Is there anything you're looking for in particular? – Tyreece Nov 08 '19 at 15:33
  • No, thank you. I believe the Listing variables you used in the JSP page does not match with the variables defined in the Listing class. Especially `` Please correct those and try once. Let me know if you are still facing the issue. – Ramu Nov 08 '19 at 15:48
  • I believe `listing.name` was the only one not correct, I have changed that now thank you. But still no luck with getting the data – Tyreece Nov 08 '19 at 15:53
  • That's weird. I've tried your code in my local with the mocked data and it's working fine. Can you do one more thing? Add toString method to your Listing class and add all member variables (If you are using eclipse, you can use eclipse to generate the toString() method) and post the output of `System.out.println(listings);` here. – Ramu Nov 08 '19 at 15:56
  • EL expression isn't supported by Servlet 2.3 apparently. See my answer. – Jonathan Laliberte Nov 08 '19 at 16:04
  • 1
    Thanks so much guys for your help. I updated the servlet to 2.4 and that fixed it. – Tyreece Nov 08 '19 at 16:09

0 Answers0