I was trying to paging my product list into several pages. Each page contains 12 records. I used servlet to retrieve data from jsp file and JSTL to display data in JSP file. But my jsp file cant display anything. The main problem seems to be with "productList" not being available to the jsp This is my servlet.
public class ProductServlet extends HttpServlet{
private static final long serialVersionUID=1L;
public ProductServlet(){
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
int page=1;
int rowsPerPage=12;
if (request.getParameter("page") != null) {
page = Integer.parseInt(request.getParameter("page"));
}
ProductDAO pd = new ProductDAO();
List<Product> list = pd.getAllPaging((page - 1) * rowsPerPage, rowsPerPage);
int noOfRows = list.size();
int noOfPages = noOfRows / rowsPerPage;
request.setAttribute("productList", list);
request.setAttribute("noOfPages", noOfPages);
request.setAttribute("currentPage", page);
RequestDispatcher view = request.getRequestDispatcher("productlist.jsp");
view.forward(request, response);
}
}
This is my productlist.jsp file.
<table border="1" cellpadding="5" cellspacing="5">
<tr>
<th>Game ID</th>
<th>Game name</th>
<th>price</th>
<th>Realease date</th>
</tr>
<c:forEach var="product" items="${productList}">
<tr>
<td><c:out value="${product.gameid}"/></td>
<td><c:out value="${product.gamename}"/></td>
<td><c:out value="${product.price}"/></td>
<td><c:out value="${product.releasedate}"/></td>
</tr>
</c:forEach>
</table>
<%--For displaying Previous link except for the 1st page --%>
<c:if test="${currentPage != 1}">
<td><a href="productlist.jsp?page=${currentPage - 1}">Previous</a></td>
</c:if>
<%--For displaying Page numbers.
The when condition does not display a link for the current page--%>
<table border="1" cellpadding="5" cellspacing="5">
<tr>
<c:forEach begin="1" end="${noOfPages}" var="i">
<c:choose>
<c:when test="${currentPage eq i}">
<td>${i}</td>
</c:when>
<c:otherwise>
<td><a href="productlist.jsp?page=${i}">${i}</a></td>
</c:otherwise>
</c:choose>
</c:forEach>
</tr>
</table>
And finally, my web.xml
<servlet>
<servlet-name>ProductServlet</servlet-name>
<servlet-class>product.ProductServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ProductServlet</servlet-name>
<url-pattern>/productlist</url-pattern>
</servlet-mapping>
I debugged my method getAllPaging() and it works perfectly, I checked and rechecked my jsp with JSTL syntax. It's ok. So I think the problem belongs to my servlet but I cant figure out what it is. Thank you for your help.