• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to display byte[]

 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

Can someone can give me a high level description of how to accomplish this. My backing bean will call a web service and it is suppose to return a byte[] that is a image bit map. If the call is successful, my xhtml suppose to render a link, Not an image, for the user to click. When the user click the link, than it open a separate window and display the image bit map. My question is, if the call was successful, how do I store the bit map byte[] and than display it when the user click the link?

Much appreciated with any input

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At a high level: store the byte array somewhere permanent, with an identifier which will be used to retrieve the byte array for download.

Then generate your HTML to contain a link which contains that identifier as a parameter. The link will point to a servlet which retrieves the byte array from that permanent location and returns it as the response, along with a MIME type which matches the contents of the array.
 
Davie Lin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

Thank you for your reply, I am sorry I was not able to reply you sooner. I was wondering if there is a way for me to do this using JSF backing bean without creating a servlet. What I was thinking was perhaps I can create a File object to store the byte[]. When the user click the link than my page will open a separate window and display the file. My coworker actually suggested that I create a image when the byte[] comes back and display the image when user click the link. Does any of these ideas are possible?

Thanks again
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I don't know much about JSF, that's why I kept my answer "at a high level".

However you'll notice that there are two parts to the solution -- my guess is that a backing bean would be suitable for storing the data but something else would be responsible for generating the HTML. And the generated link would point to some JSF artifact which -- okay, there are three parts to the solution -- would return the image. I assume there's a JSF equivalent to the standard "image servlet".
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote: I assume there's a JSF equivalent to the standard "image servlet".



You assume wrong, Paul. JSF in its standard configuration renders HTML and only HTML meaning text conforming to the appropriate RFCs, not binary data, XML, or other stuff. If you want anything else, use a servlet. Or a JSP. JSPs are good for XML output.
 
Davie Lin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:

Paul Clapham wrote: I assume there's a JSF equivalent to the standard "image servlet".



You assume wrong, Paul. JSF in its standard configuration renders HTML and only HTML meaning text conforming to the appropriate RFCs, not binary data, XML, or other stuff. If you want anything else, use a servlet. Or a JSP. JSPs are good for XML output.



@Tim,

Based upon what I need to meet my requirements, and I would like to use only JSF tags without creating something outside of JSF. What would you recommend how I can display this image byte[] that I get back from a web service call.

Thanks again
 
Tim Holloway
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JSF is not intended to generate non-HTML data. Nor is it intended to be an all-encompassing octopus that manages each and every facet of a webapp. JSF excels in rendering and reading/validating HTML forms and other HTML constructs, but for non-HTML data, you need to use the appropriate technology or you're just going to end up driving nails with a screwdriver.

If you don't believe me, browse some of the recent history in this forum from other folks who've tried. If you're working in a shop where the edict is that All Code Must Be JSF, update your CV, because you're working for clueless incompetents.
 
Davie Lin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys,

I hope this thread is not too old to revive. so here is what I am thinking, If I get a byte[] back from a web service call and I will save this byte[] as an attribute in ServletContext. Now this part is done in JSF backing bean. When the user click the link to display the image, I have a servlet to look up the attribute in ServletContext and flush out the byte[] to the user. Does this sound reasonable?

Thanks
 
Tim Holloway
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Davie Lin wrote:Hey guys,

I hope this thread is not too old to revive. so here is what I am thinking, If I get a byte[] back from a web service call and I will save this byte[] as an attribute in ServletContext. Now this part is done in JSF backing bean. When the user click the link to display the image, I have a servlet to look up the attribute in ServletContext and flush out the byte[] to the user. Does this sound reasonable?

Thanks



Just store it in a session-scope bean.
 
Davie Lin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Just store it in a session-scope bean.



Tim,

what is this session-scope bean that you were talking about? Is the session-scope bean the same as a managed-bean in session scope? I sure hope not because I thought JSF were not suppose to handle raw binary data. Can you elaborate a little more? Are you suggesting that I don't need to use ServletContext or a servlet?

Thanks
 
Tim Holloway
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Davie Lin wrote:

Tim Holloway wrote:Just store it in a session-scope bean.



Tim,

what is this session-scope bean that you were talking about? Is the session-scope bean the same as a managed-bean in session scope? I sure hope not because I thought JSF were not suppose to handle raw binary data. Can you elaborate a little more? Are you suggesting that I don't need to use ServletContext or a servlet?

Thanks



A session-scope bean and a JSF session-scope Managed Bean are one and the same thing. Which is to say, a JavaBean that can be located by obtaining the user's HttpSession object and using the getAttribute() method. The only difference between a traditional J2EE session object and the JSF one is that JSF constructed and initialized it. After that, it's completely impossible to tell the two types of session objects apart.

JSF code can certainly handle binary data. It just shouldn't attempt to output binary data as binary data. That's because the default output format for JSF is HTML and HTML is a text-only format. Which is why HTML uses things like the IMG tag to bring in the pictures on a web page via separate URLs whose content-type is not text.
 
Davie Lin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:A session-scope bean and a JSF session-scope Managed Bean are one and the same thing. Which is to say, a JavaBean that can be located by obtaining the user's HttpSession object and using the getAttribute() method. The only difference between a traditional J2EE session object and the JSF one is that JSF constructed and initialized it. After that, it's completely impossible to tell the two types of session objects apart.



So I take it that I should store the byte[] in HttpSession instead of ServletContext.

Thanks a bunch again
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic