0

I am trying to creating a JSP home page that contains a table of varying sizes. I am wanting to use the forEach JSTL tag. The servlet is working correctly and is producing a list of two products. However this information does not appear on the final HTML.

If I call:

http://localhost:8080/JavaIntoJSP/products 

the output is good.

If I call:

http://localhost:8080/JavaIntoJSP/default.jsp 

the output is missing

Code as follows (default.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>
        <title>default</title>
    </head>
    <body>  
        <form action="products" method="get">
        <table>
            <c:forEach items="${products}" var="product" >
                <tr> 
                    <td><c:out value="${product.name}" /></td> 
                    <td><c:out value="${product.price}" /></td>
                    <td><c:out value="${product.category}" /></td>
                    <td><c:out value="${product.units}" /></td> 
                </tr>
            </c:forEach>
        </table>
        </form>
        <h1>try again</h1>

    </body>
</html>

The Servlet has the following

@WebServlet("/products")
public class ProductServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProductServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        List<Product> products = new ArrayList<Product>();
        Product prod1   = new Product();
        prod1 = prod1.returnProduct();
        products.add(prod1);
        Product prod2   = new Product();
        prod2 = prod2.returnProduct();
        prod2 = prod2.returnProduct();
        products.add(prod2);


        request.setAttribute("products", products);
        request.getRequestDispatcher("default.jsp").forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }    
}

Can anyone plese let me know why the products are not being found on the JSP?

Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
opc51
  • 13
  • 1
  • 6

2 Answers2

2
http://localhost:8080/JavaIntoJSP/products

It calls the ProductServlet where products list is filled and forwarded to default.jsp

http://localhost:8080/JavaIntoJSP/default.jsp 

If you call default.jsp directly then products list will be null & because of that you can not see any out put on the screen.

Update

Answer to your question from comment

Do not write scriptlets <% %> in JSP, because scriptlets shouldn't be used in JSPs for more than a decade. Learn the JSP EL, the JSTL, and use servlet for the Java code. How to avoid Java Code in JSP-Files?

Use empty operator to check products list is null and empty. There is not if-else in EL, so you have to use <c:choose><c:when>...

<c:choose>
  <c:when test="${!empty products}">
     //print the list 
  </c:when>
  <c:otherwise>
     //call the servlet
    <jsp:forward page="products"></jsp:forward> 
  </c:otherwise>
</c:choose>  

Related

Community
  • 1
  • 1
Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
  • Thanks to Aniket and AdolAurion. The problem was that I was not making a call to the Servlet from the default.jsp I have since modified my code to include a conditional call to the servlet. If anyone knows a cleaner way to do this I would be glad to hear. <% if (request.getAttribute("products") == null){ %> <% } %>
    – opc51 Jan 28 '14 at 08:51
0

http://localhost:8080/JavaIntoJSP/default.jsp doesnt call the servlet. Therefore you wont get "products" into your request's attributes.

You should check on "default.jsp" whether "products" is available.

agastalver
  • 1,036
  • 8
  • 13