Hello everybody,

I worked long hours this evening trying to unthread the whole thing and still not working... I don't know why. The page display the results using normal code in JSP (no jstl tags). When using jstl tags the page results in blank screen, no posts. Here is the code:

index.jsp (jstl not working)

<%@ page import="bean.PostBean"%>
<%@ page import="model.Posts" %>
<%@ page import="java.util.*" %>
<jsp:useBean id="Posts" class="model.Posts" scope="page" />
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Report Results (NIOSH)</title>
</head>
<body>
<% ArrayList<PostBean> posts = new ArrayList<PostBean>(Posts.getPosts()); %>
<% //System.out.println(posts); %>
    <c:forEach var="post" items="${posts}">
   		<h2>${post.title}</h2>
   		<p>${post.text}</p>
    </c:forEach>   
</body>
</html>

index.jsp (works)

<%@ page import="bean.PostBean"%>
<%@ page import="model.Posts" %>
<%@ page import="java.util.*" %>
<jsp:useBean id="Posts" class="model.Posts" scope="page" />
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Report Results (NIOSH)</title>
</head>
<body>
<%
ArrayList<PostBean> posts = Posts.getPosts();
%>
	<%for(int i=0; i < posts.size(); i++)
		{
		PostBean post = new PostBean();
		post = (PostBean) posts.get(i);
		%>
		<h2><%= post.getTitle() %></h2>
		<p><%= post.getText() %></p>
	<%}%>
</body>
</html>

Posts.java

import java.sql.Statement;
import java.util.ArrayList;

import bean.PostBean;

public class Posts {

	public ArrayList<PostBean> getPosts() throws Exception{
		ArrayList<PostBean> list = new ArrayList<PostBean>();
		
		String driver = "com.mysql.jdbc.Driver";
		String user = "root";
		String password = "xx";
		
		Class.forName(driver);
		Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/dcaminoa", user, password);
		
		Statement select = (Statement) conn.createStatement();
		
		ResultSet result = select.executeQuery("select * from posts");
		
		while(result.next())
		{
			PostBean post = new PostBean();
			post.setId(result.getInt("id"));
			post.setTitle(result.getString("title"));
			post.setText(result.getString("text"));
			list.add(post);
		}
		result.close();
		conn.close();
		
		return list;
	}
}

PostBean.java

package bean;

public class PostBean {
	private int id;
	private String title;
	private String text;
	
	public PostBean() {}
	
	public int getId() {
		return id;
	}
	public void setId(int num) {
		id = num;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String str) {
		title = str;
	}
	public String getText() {
		return text;
	}
	public void setText(String str) {
		text = str;
	}

}

Recommended Answers

All 2 Replies

JSTL does not know of the Java variables you set, therefore in the following code the variable 'posts' cannot be used as is:

<% ArrayList<PostBean> posts = new ArrayList<PostBean>(Posts.getPosts()); %>
<% //System.out.println(posts); %>
<c:forEach var="post" items="${posts}">
<h2>${post.title}</h2>
<p>${post.text}</p>
</c:forEach>

In this case c:forEach tries to find a variable called 'posts' in the session. So, if you had used

session.setAttribute("posts",posts")

right after the variable definition, it would work.

However, none of the above is necessary.
JSTL already has access to Post.getPosts, because of the "jsp:useBean" you have declared earlier.

So, this should do the trick:
<c:forEach var="post" items="${Posts.posts}">

Note that in JSTL the dot notation accounts for the getter of the specified property, so ${Posts.posts} is equal to "Posts.getPosts()".

Thank you. It's ok now using the dot notation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.