Stack Exchange
Error when displaying data from a mySQL join command in JSP
I'm having 3 tables: Book(code, name), Author(id, name), and BookAuthor(book_code, author_id)
(many-to-many relationship) and I want to display code, name, AuthorName(Author's name) but got an error: 
So here is my code:
In BookAuthor.java: 
public class BookAuthor{
private String code;
private String name;
private String authorName;
public BookAuthor() {}

public BookAuthor(String code, String name, String authorName) {
    this.code = code;
    this.name = name;
    this.authorName = authorName;
}

public String getCode() {
    return code;
}
public void setCode(String code) {
    this.code = code;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getAuthor() {
    return authorName;
}

public void setAuthor(String authorName) {
    this.authorName = authorName;
}   
}

in DBUtils.java:
public class DBUtils {
    public static List<BookAuthor> queryBook(Connection conn) throws SQLException {
    //this query worked perfectly
    String sql2 = "Select b.Code, b.Name, a.Name as AuthorName from BOOK b inner join BOOKAUTHOR ba on ba.Book_Code = b.Code " //
                 +" inner join Author a on ba.author_id = a.id" ;

    PreparedStatement pstm = conn.prepareStatement(sql2);

    ResultSet rs = pstm.executeQuery();     
    List<BookAuthor> list = new ArrayList<BookAuthor>();    

    while(rs.next()) {
        String code = rs.getString("Code");
        String name = rs.getString("Name");
        String authorName = rs.getString("AuthorName");
        BookAuthor bookAuthor= new BookAuthor();
        bookAuthor.setCode(code);
        bookAuthor.setName(name);
        bookAuthor.setAuthor(authorName);
        list.add(bookAuthor);
    }
    System.out.println(list.get(0).getAuthor()); // I can get authorName from here
    return list;
}
}

In BookListServlet.java:
//using this method to get list of book:
 @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Connection conn = MyUtils.getStoredConnection(request);

    String errString = null;
    List<BookAuthor> bookList = null;
    try {
        bookList= DBUtils.queryBook(conn);
    } catch (Exception e) {
        System.out.println("Error: "+ e.getMessage());
        errString = e.getMessage();
        e.printStackTrace();
    }
    request.setAttribute("errString", errString);
    request.setAttribute("bookList", bookList);

    // I can get data from here too
    System.out.println("Controller"+ bookList.get(0).getAuthor());

    RequestDispatcher dispatcher = request.getServletContext().getRequestDispatcher("/WEB-INF/views/bookListView.jsp"); 
    dispatcher.forward(request, response);   
}

In bookListView.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Book List</title>
</head>
<body>
<h3>Book List</h3>
<p style="color: red;">${errString}</p>
<table border="1">
  <tr>
     <th>Code</th>
     <th>Name</th>
     <th>AuthorName</th>

  </tr>   
 <c:forEach items="${bookList}" var="book">
  <tr>
     <td>${book.code}</td>
     <td>${book.name}</td>
     <td>${book.authorName}</td>
  </tr>
 </c:forEach>  
</table>
</body>
</html>

Whenever I try to redirect to Book list page, java throws me an error:
 javax.el.PropertyNotFoundException: Property [authorName] not found on type [models.BookAuthor] 

Which is a little bit weird because I already declared It. And if I remove the ${book.authorName} from td tag, It works fine. Why is this happening ?

rename method getAuthor as getAuthorName in your BookAuthor class

You need to correct the getter and setter method in your BookAuthor pojo class for authorName field.
