0

enter image description here

    <%@page import="java.sql.*"%>
<%@page import="com.binod.db.DBConnection"%>
<jsp:include page="header.jsp"/>
<center>

<div class="content">
<table border="1" cellpadding="5" class="full">
<tr>
<td>Cat_id</td>
<td>cat_name</td>
<td>
Cat_Path
</td>
</tr>

<%
ResultSet rs;
DBConnection db=new DBConnection();
db.open();
String query="Select * from `category`";
PreparedStatement preparedStatement =db.getPreparedStatement(query);
rs=preparedStatement.executeQuery();
while(rs.next()){
    int cat_id=rs.getInt("cat_id");
    String cat_name=rs.getString("cat_name");
    String cat_path=rs.getString("cat_image");
    System.out.println(cat_path);

    %>
    <tr>
    <td><%=cat_id %></td>
    <td> <%= cat_name %></td>
    <td class="Images"><img src="<%= cat_path%>" alt="Image not found"/></td>

    </tr>
    <%
}

%>


</table>

</div>

</center>

<jsp:include page="footer.jsp"/>

MY Console output: Image path stored in database

C:\Users\Binod\workspace\shop\Pictures\Category\bbb.jpg C:\Users\Binod\workspace\shop\Pictures\Category\bbb.jpg C:\Users\Binod\workspace\shop\Pictures\Category\Screenshot (5).png C:\Users\Binod\workspace\shop\Pictures\Category\Screenshot (5).png C:\Users\Binod\workspace\shop\Pictures\Category\Screenshot (5).png C:\Users\Binod\workspace\shop\Pictures\Category\Screenshot (5).png C:\Users\Binod\workspace\shop\Pictures\Category\Screenshot (5).png C:\Users\Binod\workspace\shop\Pictures\Category\Screenshot (6).png

  • Are you using your Machine as client as well server ? – Darshit Mar 10 '17 at 14:51
  • console means browser console ? can you add more details or screenshot for browser console at the time rendering of your jsp ? – Ankit Mar 10 '17 at 14:51
  • 1
    I don't think system paths are going to work for a domain. You want something like `/shop/Pictures/Category/file.jpg` or `http://localhost/shop/Pictures/Category/file.jpg`. – hungerstar Mar 10 '17 at 14:51
  • How to use that path on my code. give me detail about that @hungerstar – Binod Pant Mar 10 '17 at 15:07
  • @Darshit yes i am using my laptop as a client as well as server – Binod Pant Mar 10 '17 at 15:12
  • @BinodPant I already did. What folder is the root folder of your project? What URL do you use to access your project? – hungerstar Mar 10 '17 at 15:19
  • @hungerstar my project is a shop.so shop is a root folder. and my image access url is http://localhost:8082/shop/showCategory.jsp – Binod Pant Mar 10 '17 at 15:30
  • @hungerstar how to use give me detail code. In Database imge path stored in C:\Users\Binod\workspace\shop\Pictures\Category\Screenshot (6).png such way. – Binod Pant Mar 10 '17 at 16:04
  • @BinodPant use `/shop/Pictures/Category/bbb.jpg` instead of `C:/Users/Binod/workspace/shop/Pictures/Category/bbb.jpg`. – hungerstar Mar 10 '17 at 16:13
  • @hungerstar i use but image is not shown.i am copy image in webcontent folder and use .image is show.But i am unable to show image in Category folder and jsp file in webcontent folder.You have a idea.plz share me – Binod Pant Mar 10 '17 at 16:34
  • @hungerstar and my image path store in database C:/Users/Binod/workspace/shop/Pictures/Category/bbb.jpg in that form.How to display image with that relative path – Binod Pant Mar 10 '17 at 16:36

2 Answers2

0

maybe you should avoid using the physical path of image or file.

just place the resource(such as image, file) in the project you are working in.

and save the relative path of image in the databse.

0

1. The reason your JSP is unable to show the image is because of "\".A single "\" is a escape sequence,it is not being read as a file separator.To overcome it,replace "\" with "\\".

2. Chrome doesn't allow local file to be read due to some security issue.Read here.

To overcome such problem, what you can do is that first replace original_path with new path with will have "\\" instead of "\".Then create a link to another jsp/servlet to convert new_path to original_path, then then display it. Something like this:

String original_path="G:\images";
String new_path=original_path.replace("\","\\");

Let us suppose your 2nd.jsp is the file which will display the images,then create a lionk in the main jsp file

<a href="2nd.jsp?new_path="+new_path+"/>image</a>

From your 2nd jsp,get the parameter,and display the file.

Community
  • 1
  • 1
Shadab Faiz
  • 2,380
  • 1
  • 18
  • 28