3

Basically, I want to display the products in an ArrayList on a JSP page. I have done that in the servlet code. But theres no output.

Also Do I have to place products.jsp in the /WEB-INF folder? When I do that, I get a requested not resource error.

My Servlet Code (InventoryServlet.java)

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    try {
        List<Product> products = new ArrayList<Product>();
        products = Inventory.populateProducts(); // Obtain all products.
        request.setAttribute("products", products); // Store products in request scope.
        request.getRequestDispatcher("/products.jsp").forward(request, response); // Forward to JSP page to display them in a HTML table.
    } catch (Exception ex) {
        throw new ServletException("Retrieving products failed!", ex);
    }

}

My JSP Page (products.jsp)

<h2>List of Products</h2>

<table>
    <c:forEach items="${products}" var="product">
       <tr>
           <td>${product.Description}</td>
          <td>${product.UnitPrice}</td>
       </tr>
    </c:forEach>
</table>

Web.xml

<web-app version="3.0"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 

 <servlet>
   <servlet-name>Inventory</servlet-name>
   <servlet-class>com.ShoppingCart.InventoryServlet</servlet-class>
 </servlet>
 <servlet-mapping>
    <servlet-name>Inventory</servlet-name>
    <url-pattern>/products</url-pattern>
  </servlet-mapping>
</web-app>
user478636
  • 3,304
  • 15
  • 49
  • 76
  • may be you need to put the JSP **just outside** the WEB-INF – Nishant Mar 28 '11 at 19:13
  • If you trace into the servlet call - are you sure that some value is being put into 'products'? – ethrbunny Mar 28 '11 at 19:14
  • JSP can go most anywhere so long as you use the correct path to refer to them. Your compiled bits will end up under WEB-INF/ - probably as classes/ or similar – ethrbunny Mar 28 '11 at 19:16
  • 2
    Just to make sure: you navigate to `/products`, not to `/products.jsp`, right? – axtavt Mar 28 '11 at 19:21
  • Ok. Now I'm getting this error. javax.el.PropertyNotFoundException: Property 'Id' not found on type com.ShoppingCart.Product – user478636 Mar 28 '11 at 20:31

3 Answers3

10

You need to open the page by requesting the servlet URL instead of the JSP URL. This will call the doGet() method.

Placing JSP in /WEB-INF effectively prevents the enduser from directly opening it without involvement of the doGet() method of the servlet. Files in /WEB-INF are namely not public accessible. So if the preprocessing of the servlet is mandatory, then you need to do so. Put the JSP in /WEB-INF folder and change the requestdispatcher to point to it.

request.getRequestDispatcher("/WEB-INF/products.jsp").forward(request, response);

But you need to change all existing links to point to the servlet URL instead of the JSP URL.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Ok. Now I'm getting this error. javax.el.PropertyNotFoundException: Property 'Id' not found on type com.ShoppingCart.Product – user478636 Mar 28 '11 at 20:31
  • That's a new problem which actually deserves its own question, but ala. To fix this error, you need to ensure that you have a `getId()` method and that you access it as follows `${product.id}` (and thus **not** `${product.Id}`). – BalusC Mar 28 '11 at 21:37
5

Here is a diagram for web application folder structure. No need to place your JSPs under WEB-INF.

enter image description here

  • debug or put print statememnts in your Servlet to make sure that the arraylist has elements in it.
  • Right-click on your browser and view page source. Is there anything generated at all?
ring bearer
  • 20,383
  • 7
  • 59
  • 72
  • What about my java package....all the classes belong to that package. And are stored in the src folder in the main project folder. – user478636 Mar 28 '11 at 19:38
  • OK so I guess you are still in your development environment and your IDE( eclipse,netbeans, Idea) is taking care of deploying web components. However, the java classes would go under WEB-INF\classes – ring bearer Mar 28 '11 at 19:48
1

the difference between put a jsp file under WebRoot and WEB-INF is: if you put under WebRoot, user can access your jsp file using the URL on the address bar of browser; if you put under WEB-INF, user can't access the file because it is hidden from public.

The only way you can access that is through Servlet using forward or redirect.

ttt
  • 3,934
  • 8
  • 46
  • 85