0

How to display image in jsp file from database?

JSP code below:-

<%
  ResultSet rs=stmt.executeQuery("select img,name from books");
%>
<html>
<head>
 <title>ONLINE BOOK STORE - LOGIN PAGE</title>
</head>
<body>
 <table align=center>
  <tr>
   <td align=center><img src="logo.jpg"></img>
  <tr>

Where do we store the image to be extracted from database?

Can anyone please help?

Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62

2 Answers2

0

I did that in the past by writing a servlet which reads an image file from local file to alter the size and format on the fly. As an alternative the bytes of the images could be from a blob column of a database, it is similar. Then the servlet path could be addressed inside a JSP inside an image tag. Is this enough? Or do you need an sample code?

MarZa
  • 3
  • 2
  • As an alternative to the servlet solution you could use inline images: https://stackoverflow.com/q/8499633/9267891 – MarZa Jun 25 '18 at 17:38
  • Need the sample code. Can you please send me? – Richard Eder Jun 26 '18 at 02:41
  • Ok, but you understand that I will not do your (home) work here, instead I can point you to some directions.. By the way, before asking questions maybe you search stackoverflow a bit more (sample code): https://stackoverflow.com/q/50139756/9267891 – MarZa Jun 26 '18 at 04:44
  • Practical advice: Don't put large objects like images inside a database... Rather store the image file name inside the database and have the image in a specific directory on the server. – MarZa Jun 26 '18 at 04:49
0

Save image as base64 string in db and render the string in <img src="<%= rs.getString(0); %>">

For converting image to base64 string. move over here Convert image to Base64 String

Chirag
  • 555
  • 1
  • 5
  • 20