aspose file tools*
The moose likes JSP and the fly likes display image after retrieving it from database? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "display image after retrieving it from database?" Watch "display image after retrieving it from database?" New topic
Author

display image after retrieving it from database?

Abhishek Reddy
Ranch Hand

Joined: Mar 28, 2006
Posts: 259
i want to retrieve image from the database and display it to the user..
i tried but nothing is displaying..
here is the code ....
In response nothing is shown...
can anyone tell me where did i went wrong...........
Also i want to display some information beside photo .Please suggest me how can i do it..

thanks in advance.


[BSouther: Added UBB CODE tags]

[ September 07, 2007: Message edited by: Ben Souther ]
[ September 07, 2007: Message edited by: Bear Bibeault ]

Abhishek
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

JSP is not a good tool for streaming binary content to browsers.
It is a templating language that serves as shorthand for building servlets that output textual output, usually HTML and Javascript.
The JSP compiler itself adds whitespace and other characters to the output which will usually foul up your image or any other binary output.

This is much easier to do with a servlet.
If you would like an example of a servlet that streams images, I have one on my own site.
http://simple.souther.us
Look for SimpleStream.

In this example, the images are being streamed from under the WEB-INF directory but it would be easy to modify it to stream the content from your database.

-Hope this helps.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

Originally posted by Abhishek Reddy Chepyala:
Also i want to display some information beside photo .Please suggest me how can i do it..


Hint: How would you do this in HTML?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Abhishek Reddy
Ranch Hand

Joined: Mar 28, 2006
Posts: 259
thanks for the reply....
my job is to display the following information to the user
1.photo
2.personal information like name,DOB,address etc

All this information is available in the database (oracle)..
i have to retrieve and show them to the user.

Since both the data are of different types(text and image) iam unable to display them to the user at a time..can any one suggest me the way of doing this.

iam using jsps to do this..
I tried using include action tags one for image and the other for data, but it didnot worked..The problem is i have to retrieve image from the database and display it to the user directly..
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Abhishek Reddy Chepyala:
...can any one suggest me the way of doing this.


Yes, we did.

The key thing to understand is that a single response can not send both binary and textual data at the same time.

Like Bear said, treat this no different than any other web page that has <img ../> tags in it.
Have your image tags point to a servlet that can stream the images to the browser.

The example app from the link I gave you does this.
The code is pretty straight forward.
Grab it and take a look.
Abhishek Reddy
Ranch Hand

Joined: Mar 28, 2006
Posts: 259
Have your image tags point to a servlet that can stream the images to the browser.
Thanks for the reply
You mean i have to get the binarystream from the database and store it in a file and give the path to the file to the image tag.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Abhishek Reddy Chepyala:
Have your image tags point to a servlet that can stream the images to the browser.
Thanks for the reply
You mean i have to get the binarystream from the database and store it in a file and give the path to the file to the image tag.


That's one approach.
Another would be to retrieve only the text from the database on the page hit and then retrieve the images when they're called (via the <img ../> tag) and stream them directly to the browser. This way, you don't have image files piling up on your web server.
Abhishek Reddy
Ranch Hand

Joined: Mar 28, 2006
Posts: 259
thanks Ben Souther i got it...
pawanwith kumar
Greenhorn

Joined: Apr 03, 2012
Posts: 3
<%
try{
// Class.forName("com.mysql.jdbc.Driver").newInstance();
//con=DriverManager.getConnection(connectionURL,"root","root");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn= DriverManager.getConnection("jdbc:odbc:dbase");
Statement st1=cn.createStatement();
String strQuery = "select images from image where id=?";

ResultSet rs1 = st1.executeQuery(strQuery);
rs1.setString(1,"2");
String imgLen="";
if(rs1.next()){
imgLen = rs1.getString(1);
}
rs1 = st1.executeQuery(strQuery);
if(rs1.next()){
int len = imgLen.length();
byte [] rb = new byte[len];
InputStream readImg = rs1.getBinaryStream(1);
// int index=readImg.read(rb, 0, len);
// out.print(index);
InputStream in = new ByteArrayInputStream(rb);
BufferedImage bImageFromConvert = ImageIO.read(in);
System.out.println("bImageFromConvert : "+bImageFromConvert);


st1.close();
response.reset();
response.setContentType("image/jpg");
response.setHeader("Content-disposition","attachment; filename=" +filename);
response.getOutputStream().write(rb,0,len);
response.getOutputStream().flush();
}
}
catch (Exception e){
out.print(e);
e.printStackTrace();
}
%>
the image can be store in system memory from database........
means its simply download from database...
but i want that it should display over the jsp page ....and then i can download it either by rt. click or provide a button to download........
..
please give your expert advise to do this .......thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

First of all, do not do this from a JSP! Please read this article to find out why JSP is a poor choice.

Secondly, you may need to adjust your content disposition header.
pawanwith kumar
Greenhorn

Joined: Apr 03, 2012
Posts: 3
ok I get it ...jsp is poor for such thing ........!!
then please give me code for servlet part....!!!
....
can i access multiple image file over the same web page at a single click......!!!
please suggest.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

pawanwith kumar wrote:ok I get it ...jsp is poor for such thing ........!!
then please give me code for servlet part....!!!
....
can i access multiple image file over the same web page at a single click......!!!
please suggest.

One punctuation mark at the end of a sentence would suffice. Multiple periods and exclamation marks are just clutter and make your post difficult to read.

Also, a sentence should start with an uppercase letter. And finally, this is a forum -- NotACodeMill <-- link


luck, db
There are no new questions, but there may be new answers.
pawanwith kumar
Greenhorn

Joined: Apr 03, 2012
Posts: 3
how can i display an image from database to the web page......with the help of servlet.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

Create a servlet that streams the image data as the response after setting the response headers appropriately. Then use an <img> tag in the JSP to reference the servlet.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: display image after retrieving it from database?
 
Similar Threads
servlet not running
Exception with displaying image
displaying image from database
Displaying an image from byte[] into JSP
Download image(jpg format) using URL in jsp