• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Problem with displaying image using jsp

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,

I have searched all the information about how to display images from database using jsp in this forum, but none of the problems is samilar to mine.

I used a servlet class to retrieve the image blob data and wrote it to the response outputstream.

-------------------------------------------------------------------
String getResourceSql = "select content from tblContent where contentID = 9";
ResultSet docRs = stat.executeQuery(getResourceSql);

if (docRs.next()){
byte[] doc = (byte[])(docRs.getObject("content"));
System.out.println("data length " + doc.length);
ServletOutputStream ostr = response.getOutputStream();
ostr.write(doc);
ostr.flush();
ostr.close();
System.out.println("test");
}

---------------------------------------------------------------------

And after, I wrote a jsp file to call the servelt to display the image as below:

<html>
<body>

<img src="/meg/ImageServlet?contentId=8"/>

</body>
</html>

I could get the servlet test message (data length and "test" string ) printed out in Tomcat dos window, which means that the servlet was called successfully. However, the image could not be displayed in the jsp page (only a red cross). I wonder what is wrong with my code?

Thanks a lot in advance!
 
Honour Cooker
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, a little typing mistake from my provious message. The contentID in my jsp page shouldn't be important as I already hardcoded it in the servlet class.
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you set the content type or any other header information?

Jules
 
JulianInactive KennedyInactive
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Honour,

The following simple example works perfectly for me:

Perhaps you could adapt it to your needs.

Jules
 
Honour Cooker
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Julian,

Thank you very much for help. I have replaced your sample code with my database code. It worked good as gold.

My problem is to extract image blob from database not a local file. Inspired by you, I tried to extract blob and saved it as a gif file same as your way to see if the jsp page show image or not. Strangly, the saved gif file even could not be opened by photoshop(jsp too). So I suspect the reason why jsp can not show blob image from database is that the data format has been changed somehow during inserting and retriving to/from database. I don't know why it could happen. By the way, I only set one response header:response.setContentType = "image/gif".

Do you have any ideas? Thanks a lot!

Honour
 
JulianInactive KennedyInactive
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, firstly are you sure the data you're writing to the database in the first place are correct?

Secondly, if you post your database writing and reading code snippets in a new thread on the JDBC forum here you're likely to get more help. Let us know which database and JDBC driver you're using too.

Jules
 
Honour Cooker
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is upload.jsp I use to save image to mysql database. I am using com.mysql.jdbc.Driver

----------------------------------------------------
<!-- upload.jsp -->
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>

<%
String contentType = request.getContentType();
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {

DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();

byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}

try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
out.println("Driver Loaded!");
} catch (Exception E) {
out.println("Unable to load driver.");
}

try {
String connectionURL = "jdbc:mysql://localhost:3306/meg?user=;password=";
Connection C = DriverManager.getConnection(connectionURL,"","");
C.setAutoCommit(false);

String insert = "insert into tblContent (content) values (?)";

PreparedStatement pstmt = C.prepareStatement(insert);
pstmt.setObject(1, dataBytes);
pstmt.executeUpdate();

pstmt.close();
C.close();
out.println("Inserted row " );

} catch (SQLException E) {
out.print( "SQLException: " + E.getMessage());
} catch (Exception E) {
out.print("Error " + E.toString());
}



}
%>

-------------------------------------------------------
Thanks!
 
JulianInactive KennedyInactive
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
pstmt.setObject(1, dataBytes); probably won't help matters. Try using setBytes() instead. That may do the trick.

Jules
 
Honour Cooker
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. The problem is still not fixed. I could see the binary data saved in table via phpAdmin. The saved in and retrieved out binary length are the same. I am going to take another way to save the file path to database instead.
 
All that thinking. Doesn't it hurt? What do you think about this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic