0

I am trying to make a shopping page where I am showing different items available for purchase using the given JSP:

<%
String query ="select * from PRODUCT";

Connection con = ConnectDao.connect();
PreparedStatement ps = con.prepareStatement(query);
 ResultSet rs = ps.executeQuery();
%> 

<%    while(rs.next()){%>

      <div class="grid1_of_4">
                 <img src="/uploads/<%=rs.getString(9) %>" class="img-responsive" alt=""/>
                  </a>
                <h4><a href="#"><%=rs.getString(2) %></a></h4>                  
                 <div class="item_add"><span class="item_price"><h6>ONLY $<%=rs.getString(6) %></h6></span></div>
                <div class="item_add"><span class="item_price"><a href="#">add to cart</a></span></div>
                 </div>
            </div>
        </div>
        <%} %>

I have the images stored in the uploads folder inside WebContents of my eclipse project folder. And the corresponding image names are stored in the database(DB2) in the 9th column ( hence rs.getString(9)).

But still I am not being able to fetch the image.

  • so what is the `src` attribute of `img` tag showing? – Apostolos Jul 28 '16 at 05:28
  • should it be `` ? without the / before uploads – Gauthaman Sahadevan Jul 28 '16 at 05:33
  • @GauthamanSahadevan yeah i tried that also...i.e. using / before uploads – Payel Basu Jul 28 '16 at 05:37
  • @Apostolos src attribute not being able to find the image in the given directory even though i have pictures in that directory – Payel Basu Jul 28 '16 at 05:39
  • src will point to relative context path directory, not directory everywhere in your local drive. i guess you know that right? – Apostolos Jul 28 '16 at 05:42
  • could you please share more info? like the project structure. as @Apostolos asked what is the value of the `src` attribute? moreover where is the start of the `` tag? the one which points to your image. – Gauthaman Sahadevan Jul 28 '16 at 05:42
  • @Apostolos yeah I know...thats why I used src="uploads/filename.jpg" – Payel Basu Jul 28 '16 at 05:46
  • if you inspect the element and hover it, you will see which is the full path of the image it is trying to display. – Apostolos Jul 28 '16 at 05:48
  • Yes and it directs to the same path as the image is lying in – Payel Basu Jul 28 '16 at 05:54
  • Thanks guys...I got my problem....I had to refresh the WebContent folder of my project in eclipse and everything was working fine – Payel Basu Jul 28 '16 at 06:02
  • It's nice you have found the problem, but I would like to remark, that you should not have business logic code (e.g. fetching from DB) in JSP. Better approach is to use [MVC pattern](http://stackoverflow.com/questions/18354034/how-to-create-mvc-based-application-without-using-framework), or, at least, to put the logic aside to a Java class, and use JSP only to call the class methods and display results. – Jozef Chocholacek Jul 28 '16 at 06:20
  • @JozefChocholacek thank you. Point noted. – Payel Basu Jul 28 '16 at 09:47

0 Answers0