0

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.

Shubham Yadav
  • 561
  • 7
  • 16
  • I do not see anything wrong in your code, did you debug to check that the list returned by the PostDAO.getPosts(user.getId()); is not empty? Also why don't you set the list in the request inside the java class and you do that on the .jsp? – NickAth Nov 26 '18 at 20:16
  • Check the generated HTML output in webbrowser. Rightclick, *View Page Source*. Do you see the `` in there or has it been removed? – BalusC Nov 26 '18 at 20:17
  • @NickAth yes its returning the values as expected. I tried setting the list inside the java class but now it warns by saying "cannot resolve posts" – Shubham Yadav Nov 26 '18 at 20:21
  • @BalusC has been removed, it's as if i wrote

    ${post.userId}

    – Shubham Yadav Nov 26 '18 at 20:23
  • @ShubhamYadav Have you the jstl library imported on your jsp page? – NickAth Nov 26 '18 at 20:26
  • @NickAth yes, imported. its working fine if i use java code to loop through it. the values are being printed – Shubham Yadav Nov 26 '18 at 20:29
  • Could you add the code where you import the jstl tag in your .jsp page please? – NickAth Nov 26 '18 at 20:38
  • *" it's as if i wrote

    ${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
  • @NickAth i have already imported jstl using "<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>" and also in maven as per https://stackoverflow.com/questions/31043869/intellij-and-jsp-jstl-cannot-resolve-taglib-for-jstl-in-tomcat7 – Shubham Yadav Nov 26 '18 at 21:23
  • @BalusC the output generated is

    ${post.userId}

    – Shubham Yadav Nov 26 '18 at 21:23
  • @ShubhamYadav That means that the jstl tags are not been recognized by the server, I guess that the jstl library has not been imported properly, could you provide us with the .jsp code please? – NickAth Nov 26 '18 at 22:22
  • @NickAth i have imported as such <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ page import="Beans.UserBean" %> <%@ page import="java.util.List" %> <%@ page import="Beans.PostBean" %> – Shubham Yadav Nov 27 '18 at 13:35
  • @ShubhamYadav Try to add this in your .jsp file <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%> – NickAth Nov 27 '18 at 14:06
  • @NickAth thanks a lot :-) it worked – Shubham Yadav Nov 27 '18 at 22:05

0 Answers0