I am new to Java servlets and hence was working over it when this problem stepped in and I have searched a lot for getting the solution but no luck.
In Servlet
I have stored a List of Object in session and forwarded it to the JSP page.
List<PostBean> posts= PostDAO.getPosts(user.getId());
session.setAttribute("posts", posts);
request.getRequestDispatcher("dashboard.jsp").forward(request, response);
Here, getPosts() returns all the posts created by the user as List<PostBean>
In JSP Page
<% List<PostBean> posts = (List<PostBean>) session.getAttribute("posts");
request.setAttribute("posts", posts);
%>
<section class="posts">
<c:forEach items="${posts}" var="post">
<h2><c:out value="${post.userId}"/></h2>
</c:forEach>
</section>
I learnt in an answer on stackoverflow that the object list have to be stored in the request object as such and then used in the jstl, while I also tried using sessionScope.posts
but still it didn't work.
PostBean
I have made two of its properties public just because calling the getter method on the object was being warned as Cannot resolve method 'getBody'
. So to directly access them, I made them public but still not working.
package Beans;
public class PostBean {
private int id;
public String body; //made public
public int userId; //made public
private String link;
private String date;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
What's wrong in the code? Can someone please explain briefly about it?
Thanks in advance.
${post.userId}
${post.userId}
"*. This is ambiguous. Are you saying that you're seeing `${post.userId}` right there in the HTML source? In other words, the `${...}` things have not been executed by the server at all? If you can't express in the right terms, it may be helpful to copypaste the generated HTML output into the question so that we can observe and conclude. – BalusC Nov 26 '18 at 20:41${post.userId}
– Shubham Yadav Nov 26 '18 at 21:23