outputting a blob from database to the browser using struts

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.
raidtAsked:
Who is Participating?
 
girionisCommented:
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
 
girionisCommented:
if (rs.next()) {
            Blob blob = rs.getBlob("column name");
            new ImageIcon(ImageIO.read(blob.getBinaryStream()) );
       }
0
 
TimYatesCommented:
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
girionisCommented:
You might as well use the InputStream:

Instream in = rs.getBinaryStream("column name")
0
 
TimYatesCommented:
ImageIcon in a struts application girionis? ;-)
0
 
girionisCommented:
Ehm... no you are right :)

Return just the bytes from the servlet, this should do :)
0
 
girionisCommented:
> Return just the bytes from the servlet

I mean in from your action class.
0
 
TimYatesCommented:
:-)
0
 
girionisCommented:
> I mean in from your action class.

should be:

I mean from your action class.

I hate when my spelling sucks :(

0
 
TimYatesCommented:
Heheh :-)

I would still use a servlet for this, rather than an action class...  But I guess an Action class would do :-)
0
 
girionisCommented:
Essentially an Action is a servlet, or not? It's been some time I used Struts.
0
 
TimYatesCommented:
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
 
girionisCommented:
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
 
raidtAuthor Commented:
well the thing is i am using a J2ee concept so i cant put the SQL in the servlet
0
 
TimYatesCommented:
> J2ee concept so i cant put the SQL in the servlet

Errrr...  why not?
0
 
TimYatesCommented:
>  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
 
girionisCommented:
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
 
girionisCommented:
> As soon as I finish researching Hibernate

Hibernate rocks!!! (a bit oot) :)
0
 
TimYatesCommented:
> 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
 
raidtAuthor Commented:
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
 
raidtAuthor Commented:
I like the idea of Stream -> array , array-> Stream but can you please tell me how with the code please
0
 
girionisCommented:
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
 
raidtAuthor Commented:
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
 
girionisCommented:
> 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
 
TimYatesCommented:
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
 
raidtAuthor Commented:
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
 
TimYatesCommented:
> 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
 
raidtAuthor Commented:
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
 
TimYatesCommented:
Can't you just connect to the database in your Action class?
0
 
raidtAuthor Commented:
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
 
girionisCommented:
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
 
TimYatesCommented:
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
 
raidtAuthor Commented:
Then why use struts???!!!

Sorry I am looking for a solution using struts correctlly
0
 
raidtAuthor Commented:
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
 
TimYatesCommented:
>  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
 
girionisCommented:
> >  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
 
raidtAuthor Commented:
Tim sorry to say that you treat people like they are really dum any way i will search more thanks any way
0
 
girionisCommented:
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
 
TimYatesCommented:
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
 
raidtAuthor Commented:
Ok guys so i take it from you that there is no way to follow struts framework in doing this?

0
 
TimYatesCommented:
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
 
TimYatesCommented:
Split between girionis and me I think...
0
 
girionisCommented:
>  Split between girionis and me I think...

I double this :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.