9 Replies - 2272 Views - Last Post: 25 April 2019 - 06:51 AM

#1 luliligavu   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 26-March 19

how to display image in img tag from database using jsp

Posted 24 April 2019 - 04:19 AM

I want to retrieve image from database and display it in JSP using img tag. I am using mySql.

view.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>View</title>
</head>
<body>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/database","username","password");
PreparedStatement ps=con.prepareStatement("select image,username from test_user where username='user' and password='pass'");
ResultSet rs=ps.executeQuery();
if(rs.next()){
	System.out.println("Image:"+rs.getBlob(1)+"\tUsername:"+rs.getString(2));
	out.print("<img src="+rs.getBlob(1)+"");
}
out.print("Hello");
%>
</body>
</html>


Is This A Good Question/Topic? 0
  • +

Replies To: how to display image in img tag from database using jsp

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6829
  • View blog
  • Posts: 28,314
  • Joined: 12-December 12

Re: how to display image in img tag from database using jsp

Posted 24 April 2019 - 04:43 AM

What happens with your code?

The img tag is missing a closing angled bracket > and you probably want to embed quotes around the blob.
Was This Post Helpful? 0
  • +
  • -

#3 luliligavu   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 26-March 19

Re: how to display image in img tag from database using jsp

Posted 24 April 2019 - 04:58 AM

View Postandrewsw, on 24 April 2019 - 04:43 AM, said:

The img tag is missing a closing angled bracket >


Sorry for that

Image is not display on webpage

This post has been edited by luliligavu: 24 April 2019 - 04:59 AM

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6829
  • View blog
  • Posts: 28,314
  • Joined: 12-December 12

Re: how to display image in img tag from database using jsp

Posted 24 April 2019 - 06:12 AM

What does the img tag contain when inspected in your browser?
Was This Post Helpful? 0
  • +
  • -

#5 luliligavu   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 26-March 19

Re: how to display image in img tag from database using jsp

Posted 25 April 2019 - 05:15 AM

View Postandrewsw, on 24 April 2019 - 06:12 AM, said:

What does the img tag contain when inspected in your browser?

It shows as
<img src="[email protected]">

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6829
  • View blog
  • Posts: 28,314
  • Joined: 12-December 12

Re: how to display image in img tag from database using jsp

Posted 25 April 2019 - 05:25 AM

Does the username display correctly? Isn't the image at index 0? I just did a quick search and the columns do start at 1.

I think you need to getBytes according a quick look through this

Retrieve an Image stored as BLOB on a MYSQL DB
Was This Post Helpful? 0
  • +
  • -

#7 luliligavu   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 26-March 19

Re: how to display image in img tag from database using jsp

Posted 25 April 2019 - 05:42 AM

I'm displaying image and Hello.
Hello display on webpage correctly
Image is not displaying
Retrieve an Image stored as BLOB on a MYSQL DB i will work on that

I convert the blob to byte then what I do please tell me
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6829
  • View blog
  • Posts: 28,314
  • Joined: 12-December 12

Re: how to display image in img tag from database using jsp

Posted 25 April 2019 - 06:04 AM

Actually, a bit further down at the link, it is suggested to use

InputStream binaryStream = resultSet.getBinaryStream(yourBlobColumnIndex);


but I cannot assist personally beyond this. Maybe someone else will contribute.

[Personally, I think I would getBytes and examine this data (or set it as src) to see if it looks like something that an img-src would accept.]
Was This Post Helpful? 0
  • +
  • -

#9 luliligavu   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 26-March 19

Re: how to display image in img tag from database using jsp

Posted 25 April 2019 - 06:45 AM

I got it now


 blob=rs.getBlob(1);
    byte[] imageBytes=blob.getBytes(1, (int)blob.length());
    String encodedImage=Base64.getEncoder().encodeToString(imageBytes);
    image="data:image/jpg;base64,"+encodedImage;
    out.print("<img src="+image+">");



Thankyou :bigsmile:/>
Was This Post Helpful? 1
  • +
  • -

#10 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6829
  • View blog
  • Posts: 28,314
  • Joined: 12-December 12

Re: how to display image in img tag from database using jsp

Posted 25 April 2019 - 06:51 AM

Well done ;)

[I thought you might need to add something to the bytes ;) ]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1