• 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

Writing image into WAR?

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

I am finding it difficult to create and write an image into WAR file and get it through a jsp or html page.

How can I write a image into WAR deployment folder?

My requirement is upon request for a page, I have to get data and create a chart which I have to save it as png to display it along with some table.

Please help me in this regard.
[ May 04, 2008: Message edited by: Bear Bibeault ]
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use ClassLoader.getResourceAsStream to get a stream from a resource contained in the WAR file.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think you really mean that you want to write the folder to the war file. A war file is an entire web application zipped into a single file.

I assume that you want to write the image into the application's directory structure. The answer is... it depends.
Depending on your container, how it's configured, and how you've deployed your application, there may or may not be a directory structure to write to.

If your container unpacks your application before deploying it, you can find the root of your application's directory structure with servletContext.getRealPath("/"). If not, this method will return null because there is no file structure.

I recommend either streaming the generated image directly to the browser if it's only going to be used once or, if you need to access it more than once, writing it to a directory outside of your web container and/or your web application's directory structure. Then, write a servlet that can read this file and stream it to the browser. Either of these two solutions will be more robust and portable because they don't rely on the application being deployed in any particular way in order to work.

By the way:
We have an example application with a servlet that streams images in our Code Barn.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

My requirement is upon request for a page, I have to get data and create a chart which I have to save it as png to display it along with some table.



Writing to your WAR is not the solution for this. It appears that you have to create this data dynamically. Perhaps you are looking for something like this

http://code.google.com/apis/chart/

You can also use the jFreeChart API and couple it with an API like iText if you want to create PDF content or charts using a java API. I wrote a little article about doing this. Check it out if you are interested

http://jtoee.blogspot.com/2008/04/writing-pdf-charts-with-itext-in-less.html
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ben Souther ,

you got my point but still, its not perfect solution for me.

Here is why :

1. I have to embed the image into a html page.
(Displaying the image is not the only purpose.)
I mean the content type response page is "text/html". (Not image/png)

John,
I have alreay used Jfree chart to genrate the chart.
I am not struck in generating the chart but "to embed image in a html response."

David O'Meara ,

can i create a empty image say by name "myImage.png" into images folder of webApp. SOthat I can open the image and write it into it?


Please reply me...
[ May 06, 2008: Message edited by: Srinivasan thoyyeti ]
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

its not perfect solution for me. Here is why :

1. I have to embed the image into a html page.
(Displaying the image is not the only purpose.)
I mean the content type response page is "text/html". (Not image/png)


It actually sounds like a perfect solution. What Ben suggested was to write the image to a file, and then have a servlet stream it to the browser. So in your HTML you would have a tag like this:

<img src="/myWebApp/ImageStreamingServlet?img=xyz.png">

Then all the ImageStreamingServlet needs to do is to load the image and stream it to the browser with its content type set appropriately. See the CodeBarn example Ben linked to for how this works.
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ulf Dittmer,

Thanks a lot.
I am not good at HTML.

I am thinking of img tag's src attribute as path to static png file.
I didn't thought of calling a servlet inside a src attribute.

This is due to lack of knowledge in HTML.

Thanks a lot.
 
reply
    Bookmark Topic Watch Topic
  • New Topic