0

I'm stuck on trying to get and print the table of data. I think I set up the request correctly, in servlet but when I try to get the list of book in test.jsp, it says the server encountered an unexpected condition that prevented it from fulfilling the request. Is that where they should go? I feel like I'm missing something or they completely wrong, but I can't figure out what. Thanks for any help.

in the servletTest

public class TestServlet extends HttpServlet {

protected void doPost(HttpServletRequest request,
                      HttpServletResponse response)
        throws ServletException, IOException {

}

protected void doGet(HttpServletRequest request,
                     HttpServletResponse response)
        throws ServletException, IOException {

        String userPath="/test";
        String url = "/WEB-INF/jsp"+userPath+".jsp";


        String categoryName = request.getParameter("category");
        request.setAttribute("selectedCategoryName", categoryName);

        CategoryDao categoryDao = 
        ApplicationContext.INSTANCE.getCategoryDao();

        BookDao bookDao = ApplicationContext.INSTANCE.getBookDao();
        request.setAttribute("bookList",bookDao);


        request.getRequestDispatcher(url).forward(request,response);
}

In the test.jsp

<body>

  <ul>
      <li><a href="test?category=architecture">architecture</a></li>
      <li><a href="test?category=fine art">fine art</a></li>
      <li><a href="test?category=fashion">fashion</a></li>
      <li><a href="test?category=photography">photography</a></li>
      <li><a href="test?category=graphic">graphic</a></li>
 </ul>
 <c:set var="categoryName">${selectedCategoryName}</c:set>
 <h1><span style="size: 14px">${categoryName}</span> </h1>
 <table border="1">

 <tr>
    <th>book_id</th>
    <th>title</th>
    <th>author</th>
    <th>price</th>
    <th>is_public</th>
    <th>category_id</th>
 </tr>

    <c:forEach var="book" items="${bookList}">
        <tr>
            <td>${book.bookId}</td>
            <td>${book.title}</td>
            <td>${book.author}</td>
            <td>${book.price}</td>
            <td>${book.is_public}</td>
            <td>${book.category_id}</td>
        </tr>
    </c:forEach>
</table>
</body>
</html>

Book class

public class Book {

private long bookId;
private String title;
private String author;
private int price;
private boolean isPublic;
private Long categoryId;


public Book(long bookId, String title, String author, int price,
            boolean isPublic, long categoryId) {
    this.bookId = bookId;
    this.title = title;
    this.author = author;
    this.price = price;
    this.isPublic = isPublic;
    this.categoryId = categoryId;
}

public long getBookId() {
    return bookId;
}

public String getTitle() {
    return title;
}

public String getAuthor() {
    return author;
}

public int getPrice() {
    return price;
}

public boolean getIsPublic() {
    return isPublic;
}

@Override
public String toString() {
    return "Book{" +
            "bookId=" + bookId +
            ", title='" + title + '\'' +
            ", author='" + author + '\'' +
            ", price=" + price +
            ", isPublic=" + isPublic +
            ", categoryId=" + categoryId +
            '}';
    }
}
Jonathan Laliberte
  • 2,672
  • 4
  • 19
  • 44
Eros
  • 9
  • 5

1 Answers1

0

The error is because you are not calling the correct names. In your book class you have the following variables:

private boolean isPublic;
private Long categoryId;

But in your jsp, you call these variables:

 <td>${book.is_public}</td>
 <td>${book.category_id}</td>

Which is wrong. You need to call:

 <td>${book.isPublic}</td>
 <td>${book.categoryId}</td>

Then it will work. Make sure the names are always the same.

Jonathan Laliberte
  • 2,672
  • 4
  • 19
  • 44
  • Thank you so much but still I am getting this error : Description The server encountered an unexpected condition that prevented it from fulfilling the request. Exception org.apache.jasper.JasperException: An exception occurred processing JSP page [/WEB-INF/jsp/test.jsp] at line [32] 29: category_id 30: 31: 32: 33: 34: ${book.bookId} 35: ${book.title} – Eros Oct 05 '17 at 23:08
  • what does the error say exactly? Post it too if you can – Jonathan Laliberte Oct 05 '17 at 23:09
  • org.apache.jasper.JasperException: An exception occurred processing JSP page [/WEB-INF/jsp/test.jsp] at line [32] 32: – Eros Oct 05 '17 at 23:11
  • What happens if you just do ${bookList} outside the loop, what do you see? – Jonathan Laliberte Oct 05 '17 at 23:12
  • .jsp cannot get data from items="${bookList}" – Eros Oct 05 '17 at 23:13
  • Something is wrong in the TestServlet – Eros Oct 05 '17 at 23:15
  • no, you have no setters in your book class. You have getters but no setters. you need to set the data before you can get it. – Jonathan Laliberte Oct 05 '17 at 23:15
  • I can not use setter that is a port of requirements. – Eros Oct 05 '17 at 23:26