Solved

outputting a blob from database to the browser using struts

Posted on 2004-09-14
46
367 Views
Last Modified: 2013-11-24
I have developed an application that will take photos stored in a blob field in an Oracle Database and display the photo on the browser. Now i am Implementing the same thing to a struts application can any one help me with that?

in the first application i was not storing the photo to a file i was using the servlet outputStream.
i want to do the same thing meaning i dont want to store the photo to a file and display it to the browser.
0
Comment
Question by:raidt
  • 16
  • 16
  • 11
46 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 250 total points
Comment Utility
It should be the same as loading a fiel from database without using Struts. If you have a ResultSet "rs" simply do:

        if (rSet.next()) {
            Blob blob = rSet.getBlob("column name");
            new ImageIcon(ImageIO.read(blob.getBinaryStream()) );
       }

0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
if (rs.next()) {
            Blob blob = rs.getBlob("column name");
            new ImageIcon(ImageIO.read(blob.getBinaryStream()) );
       }
0
 
LVL 35

Assisted Solution

by:TimYates
TimYates earned 250 total points
Comment Utility
In your web page, add an image tag where the "src" parameter points to a servlet

eg:
       <img src="/servlet/MyPhotoServlet">

Then, you can write the servlet that reads the blob from the database (as you currently do in your test app), and streams it back out to the HttpResponse.getOutputStream

That should work...

You may need to set the response type whatever the mime type of the image is...but that shouldn't ne necessary :-)

Tim
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
You might as well use the InputStream:

Instream in = rs.getBinaryStream("column name")
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
ImageIcon in a struts application girionis? ;-)
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
Ehm... no you are right :)

Return just the bytes from the servlet, this should do :)
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
> Return just the bytes from the servlet

I mean in from your action class.
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
:-)
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
> I mean in from your action class.

should be:

I mean from your action class.

I hate when my spelling sucks :(

0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
Heheh :-)

I would still use a servlet for this, rather than an action class...  But I guess an Action class would do :-)
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
Essentially an Action is a servlet, or not? It's been some time I used Struts.
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
True.  But I'm not sure what you would do about returning an ActionMapping to another page when the action completes...

I guess you could return null...but I am not 100% sure that works...

Both should work though :-)
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
Yes, you return null. We did something similar using Spring. We needed to construct a PDF document and after we sent the bytes to the browser we returned null from the method.
0
 

Author Comment

by:raidt
Comment Utility
well the thing is i am using a J2ee concept so i cant put the SQL in the servlet
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
> J2ee concept so i cant put the SQL in the servlet

Errrr...  why not?
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
>  Yes, you return null. We did something similar using Spring.

Cool :-)  I'll remember that :-)  As soon as I finish researching Hibernate, I'm going to have a look at Spring :-)
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
Put it in a bean then (that returns a byte array), call this bean from your servlet and then return this array from your servlet. But Tim's comment is also valid. Why not?
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
> As soon as I finish researching Hibernate

Hibernate rocks!!! (a bit oot) :)
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
> Put it in a bean then (that returns a byte array), call this bean from your servlet and then return this array from your servlet.

This is a cool way of doing it, but it does introduce more of an overhead (stream->array, array->stream)

> Hibernate rocks!!! (a bit oot) :)

 Hehehe, it sure does :-)  And now I have it running as the bean persistence layer in JBoss :-D
0
 

Author Comment

by:raidt
Comment Utility
well i am using a java  file as the manager the manager knows the controller another java class the controller knows the Class that is calling SQLs and every thing is stored in objects and then retrived when ever nedded from the objects on the JSP page
0
 

Author Comment

by:raidt
Comment Utility
I like the idea of Stream -> array , array-> Stream but can you please tell me how with the code please
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 35

Expert Comment

by:girionis
Comment Utility
It will create overhead as Tim said but if this is the only way to do it then ok.

Have the java class to read the blob from the database. Then call this class and one of its methods (the one that reads the blob) from the servlet. Have the method return a bytes[]. Then simply call the servlet from your HTML page.
0
 

Author Comment

by:raidt
Comment Utility
I belileve that defetes the whole point of struts where you should't call any servlet from a JSP all should be done from struts config.xml right?
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
> I belileve that defetes the whole point of struts where you should't
> call any servlet from a JSP all should be done from struts config.xml right?

It should but nothing stops you doing it. There are times that Struts is not enough and you need a specialized way to do it. Otherwise return the image in your Action class.
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
Yeah, would doing the SQL from your Action class be a more "correct" way of doing it?

Struts is a really good framework, but as girionis says, you can step outside it and do your own thing whenever it becomes necessary.

Writing the Image to a byte array and then back to a stream is a solution (to use a bean), but it is a fair amount of processing to do (as you don't know the size of the inputStream until you have read it, so you will need to regularly re-allocate your array)
0
 

Author Comment

by:raidt
Comment Utility
can i use the Action servlet to call a connection from another calls that will return a resultSet and then the Action servlet will output the photo to the forwarded JSP Page?

if yes please help with the code
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
> and then the Action servlet will output the photo to the forwarded JSP Page?

Not without saving the image to a file...

This way, you will have to do something like:

    <img src="GetTheImage.do">

Then in your "GetTheImage" Action, connect to the database, and write the InputStream from the blob to the OutputStream of the HttpServletResponse
0
 

Author Comment

by:raidt
Comment Utility
ok i agree to that but my question is in struts it is wrong to return the resultSet to the Action servlet. so is there any other way of doing it?
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
Can't you just connect to the database in your Action class?
0
 

Author Comment

by:raidt
Comment Utility
Tim i can but that is wrong in the struts framework if there is not other solution then i might have to do some thing like that but i dont want to start with something wrong..
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
There is no wrong or right as long as you can get your application work. Make it work first by using a method and then spend a few minutes and see how you can optimize it.
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
There is nothing in the Struts framework to get an image out of a Blob field in a database and display it in a web page (with or without saving it to a file first)...

So therefore, you are going to have to write something that does it.

This does not make it wrong

It makes it necessary
0
 

Author Comment

by:raidt
Comment Utility
Then why use struts???!!!

Sorry I am looking for a solution using struts correctlly
0
 

Author Comment

by:raidt
Comment Utility
Tim right if that is the case then i will ask around and let you guys know if i found some thing

Thank you
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
>  Then why use struts???!!!

Because Struts does so much more than just displaying images loaded from a BLOB field in a database...

If that is all you are doing, then don't use Struts....just write a servlet
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
> >  Then why use struts???!!!
>
>Because Struts does so much more than just displaying images loaded from a BLOB field in a database...
>
>If that is all you are doing, then don't use Struts....just write a servlet

So true :)
0
 

Author Comment

by:raidt
Comment Utility
Tim sorry to say that you treat people like they are really dum any way i will search more thanks any way
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
raidt, this is not true. Tim is trying to help. All he means is that if you simply want to display an image do not use struts. No offence :)
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
Sorry if I upset you...this was not my intention...  

I was just trying to help, and between me and girionis, you have the correct answer.

Good luck with the project!

Tim
0
 

Author Comment

by:raidt
Comment Utility
Ok guys so i take it from you that there is no way to follow struts framework in doing this?

0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
Yeah...you have to go outside the struts framework to do this, but as we said before, there is nothing wrong with doing this...  

This is a situation that Struts wasn't designed for, so you have to roll your own way of doing it...
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
Split between girionis and me I think...
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
>  Split between girionis and me I think...

I double this :)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now