• 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

StringBuffer

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

I am having the data in the StringBuffer. I want to know how to display it in the web browser. I can able to display it in the console but not in the web browser. Please help me. Thanks in Advance.

 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to host the file on some kind of server and transmit it via JSP or Servlets. Have you set up a Java or J2EE servlet before? I'd download and try Tomcat out, then try some JSP writing tutorials. Once you've got that covered you can output the string buffer data pretty easily.

Alternatively, if you have a non-java server you could have the java program generate the string buffer as an HTML file that the server than serves but this solution isn't usually very useful especially if the file changes based on certain inputs like user information/state.
 
cathymala louis
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I tried as follows: Converting StringBuffer to String and using getBytes method getting the byte and storing it in the byte array and passing that byte array to the jsf.

sb - StringBuffer
b - byte array.

b = sb.toString().getBytes();

It is showing the error.
java.net.SocketException: Connection reset by peer: socket write error

The StringBuffer can't be transferred like that.

Thanks in Advance.

 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should try to be more forthcoming with information, rather than
mentioning things like "jsf" in passing! If you are indeed using JSF,
I would have a backing bean expose a property of type String and then
just access that string, for example:
In your backing bean class, you can implement this any way you like,
for example, with a StringBuffer:
[ November 21, 2005: Message edited by: Jeff Albrechtsen ]
 
Scott Selikoff
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All java Objects, especially StringBuffer, have a toString() method that converts it to java String. I'm not sure why'd you need to transmit it as a byte array.
 
cathymala louis
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Let me explain more clear.

I need to display an image. Trying to convert image into byte array, so that I can display it in the web browser.

For a Single page it works fine. Since I need to display more than one page. I concatinating all the page contents into String Buffer, by using append method. It is appending fine.

Now my problem is how to convert StringBuffer into byte array.

So that I can display it in the web browser.

Thanks.


how to convert StringBuffer into byte array
 
Marshal
Posts: 28226
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
Remember that you are generating a page that can be interpreted by a browser. And browsers can't interpret pages that contain mixtures of text and binary data like images. When web designers want to "include" an image in a page, they do that by putting an <img> element in their page with a link to the image. They don't put the image itself into the page.

So you will have to do the same thing. If you are generating the image dynamically then your HTML will have to have an <img> element whose "src" attribute is a link to something (I would use a servlet) that returns only the binary data and nothing else.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are indeed using JSF, why not just use h:graphicImage?
 
cathymala louis
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I am not displaying image from any particular location. The image I am getting is from IBM Content Manager. It has some java API. Using those API's the only way i can display images is using byte array. For a single page it works fine. Since need to display more pages used String buffer to append. Now wants to convert this StringBuffer into byte array so that i can display it in the browser.

Sorry for the confusion.
Thanks.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Finally! You've explained your context! Again, html use the <img> tag to embed images:

<img src="path"/>

Different web frameworks have their own tags to generate that html.

The typical bare-bones servlet/jsp solution is to map such paths to a servlet
that writes back the image. Any textbook on servlets/jsps will have an
example of that.

Now, it would be great if JSF had some way to bind a property to simplify this,
but I haven't seen that done. Anybody?
 
reply
    Bookmark Topic Watch Topic
  • New Topic