Solved

outputting a blob from database to the browser using struts

Posted on 2004-09-14
46
376 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
ID: 12052461
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
ID: 12052462
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
ID: 12052474
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:girionis
ID: 12052478
You might as well use the InputStream:

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

Expert Comment

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

Expert Comment

by:girionis
ID: 12052504
Ehm... no you are right :)

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

Expert Comment

by:girionis
ID: 12052511
> Return just the bytes from the servlet

I mean in from your action class.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12052512
:-)
0
 
LVL 35

Expert Comment

by:girionis
ID: 12052515
> 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
ID: 12052521
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
ID: 12052543
Essentially an Action is a servlet, or not? It's been some time I used Struts.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12052561
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
ID: 12052575
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
ID: 12052580
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
ID: 12052582
> J2ee concept so i cant put the SQL in the servlet

Errrr...  why not?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12052590
>  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
ID: 12052594
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
ID: 12052601
> As soon as I finish researching Hibernate

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

Expert Comment

by:TimYates
ID: 12052616
> 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
ID: 12052623
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
ID: 12052642
I like the idea of Stream -> array , array-> Stream but can you please tell me how with the code please
0
 
LVL 35

Expert Comment

by:girionis
ID: 12052652
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
ID: 12052698
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
ID: 12052716
> 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
ID: 12052760
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
ID: 12052845
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
ID: 12052861
> 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
ID: 12052883
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
ID: 12052911
Can't you just connect to the database in your Action class?
0
 

Author Comment

by:raidt
ID: 12052951
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
ID: 12052999
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
ID: 12053046
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
ID: 12053057
Then why use struts???!!!

Sorry I am looking for a solution using struts correctlly
0
 

Author Comment

by:raidt
ID: 12053078
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
ID: 12053098
>  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
ID: 12053124
> >  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
ID: 12053140
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
ID: 12053150
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
ID: 12053164
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
ID: 12055548
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
ID: 12055651
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
ID: 12281223
Split between girionis and me I think...
0
 
LVL 35

Expert Comment

by:girionis
ID: 12284349
>  Split between girionis and me I think...

I double this :)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Which non-HTML GUI front end to use with Java? 3 50
Java: anonymous class 4 37
Setup GlassFish 4 34
IntelliJ and Eclipse Neon 2 installations 4 31
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

679 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