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.