Link to home
Start Free TrialLog in
Avatar of raidt
raidt

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
if (rs.next()) {
            Blob blob = rs.getBlob("column name");
            new ImageIcon(ImageIO.read(blob.getBinaryStream()) );
       }
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You might as well use the InputStream:

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

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

I mean in from your action class.
> I mean in from your action class.

should be:

I mean from your action class.

I hate when my spelling sucks :(

Heheh :-)

I would still use a servlet for this, rather than an action class...  But I guess an Action class would do :-)
Essentially an Action is a servlet, or not? It's been some time I used Struts.
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 :-)
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.
Avatar of raidt
raidt

ASKER

well the thing is i am using a J2ee concept so i cant put the SQL in the servlet
> J2ee concept so i cant put the SQL in the servlet

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

Hibernate rocks!!! (a bit oot) :)
> 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
Avatar of raidt

ASKER

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
Avatar of raidt

ASKER

I like the idea of Stream -> array , array-> Stream but can you please tell me how with the code please
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.
Avatar of raidt

ASKER

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?
> 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.
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)
Avatar of raidt

ASKER

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
> 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
Avatar of raidt

ASKER

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?
Can't you just connect to the database in your Action class?
Avatar of raidt

ASKER

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..
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.
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
Avatar of raidt

ASKER

Then why use struts???!!!

Sorry I am looking for a solution using struts correctlly
Avatar of raidt

ASKER

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

Thank you
>  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
> >  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 :)
Avatar of raidt

ASKER

Tim sorry to say that you treat people like they are really dum any way i will search more thanks any way
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 :)
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
Avatar of raidt

ASKER

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

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

I double this :)