aspose file tools*
The moose likes Web Services and the fly likes Is it possible to return an image with a web service ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "Is it possible to return an image with a web service ?" Watch "Is it possible to return an image with a web service ?" New topic
Author

Is it possible to return an image with a web service ?

raminaa niilian
Ranch Hand

Joined: Jul 14, 2005
Posts: 551
Hi
Thank you for reading my post.
I have a big problem for showing Charts in my jsf portlet , there is no library to use.
so i come to this idea to make a web service and , the web service take parameters and create chart using Jfree, .... .
and then web service return the chart to my jsf portlet . i can use that chart and show it to my visitors.

now my question is :
1-How i can return an image with a web service ?
2-how i can show that image ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42286
    
  64
Why do you think that you can't use JFree in connection with a JSF-based portlet? Figuring out what the perceived problem on that is sounds a lot easier than creating a web service that sends images (although that is certainly possible).


Ping & DNS - my free Android networking tools app
raminaa niilian
Ranch Hand

Joined: Jul 14, 2005
Posts: 551
Hi
Thank you for reply.
as you know Jfree , CeWolf ....
all of them use Servlets to render the chart , and portlets (at least jsf portlets) does not works with servlets.

for example , i tried to use jenia4faces which is charting component for jsf
but when i tried , it return varity of errors from NPE to an empty image
i never could make it works inside a portlet.
Reza Ravasizadeh
Ranch Hand

Joined: Jun 08, 2004
Posts: 177
I don't have info about Jchart and JSF so I can only tell you it is possible to send a picture via web service.
I think you can code the picture (for example base64) then send as a string.
On client you get the String of code, you can decode it and cast it to other object (if there is any image object in java).


Reza
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42286
    
  64
JFree does not use servlets - it is a class library that works in whichever Java context you care to use it. Servlets, applets, applications, web service ... whereever you can use a Java class, you can use JFree.

cewolf is a tag library, so you would need a JSP enviroment for that. I don't see why it wouldn't work in conjunction with JSF.
raminaa niilian
Ranch Hand

Joined: Jul 14, 2005
Posts: 551
Hi
Thank you for your helps.
i post a extened explanation of my problem in :
http://www.coderanch.com/t/362001/Servlets/java/servlet-url-mapping
i will be happy if you could take a look at it.
Reid M. Pinchback
Ranch Hand

Joined: Jan 25, 2002
Posts: 775
Unless I'm missing something here, why not just keep using it as a servlet? Your portlet just decides which HTML img url to insert, but the servlet responds to the request for the image. Just deploy the servlet as a servlet. Portlets are responsible for rendering a section of an HTML document, not for rendering assets external to the document but referred to by that document.


Reid - SCJP2 (April 2002)
raminaa niilian
Ranch Hand

Joined: Jul 14, 2005
Posts: 551
Thank you for reply , can you please give me more information , i can not understand what you mean.

Should i write another web application to render the images ?


Thank you
Rusty Enisin
Ranch Hand

Joined: May 26, 2005
Posts: 107
Servlets can return text, HTML, images, pdf or whatever you want. Use the servlet to generate the chart and return it to the user. The process works something like this.

Your portlet generates the HTML. It sets the image src to some servlet with a customized query string. The browser renders the HTML. The browser has no idea this is a portlet. At this point it is just HTML. It renders the HTML as it knows how to do. When it finds an <img src=""> tag it makes another request to the server for the image.

When your portlet sets the image source it should do something like this http://www.yourcompany.com/servlets/MyImageChartRenderer?imageData=whateverYouNeed&lots=more Inside the MyImageChartRenderer servlet set it's content type to whatever image type you want to return instead of HTML. Have the MyImageChartRenderer servlet use the query string data to render the chart image however you normally would with JFree. Then return the image back to the user instead of HTML.

Doing it this way your portlet only needs to dynamicaly change the url for the image source. That is just text manipulation. Portlet or not, this should always work.
[ January 23, 2006: Message edited by: Rusty Enisin ]

The squeaky wheel gets the grease. Well, that or replaced...
raminaa niilian
Ranch Hand

Joined: Jul 14, 2005
Posts: 551
Hi
Thank you very much , this solution will help.
I just think that i create a seperate web application with just one servlet ,
in my jsf page i should set image source attribute to that sevlet and i should pass all of my parameters.
now i just have some questions

1-in some times there maybe more that one person request a chart , i know that servlet will return the image as a stream.
what will happen when 10 people request the page that contain that chart ?
as you know diffrent page may need different charts with different parameters.
will the servlet return correct chart for each of them ?

2-How mych long my url for image source could be ?
can it contain 20 parameters ?


Thank you
Rusty Enisin
Ranch Hand

Joined: May 26, 2005
Posts: 107

1-will the servlet return correct chart for each of them ?

Yes. Be sure the variable containing the chart that you return is a method local variable. No static variables. No class instance variables. Create it only inside the method. Servlets share static and instance variables.



2-How mych long my url for image source could be ?
can it contain 20 parameters ?

20 parameters will be fine. I think GET requests are limited to 256 characters, or something there near ( ??? ). But POST is unlimited. So if you are worried about it, do a POST request.
[ January 24, 2006: Message edited by: Rusty Enisin ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
I just think that i create a seperate web application with just one servlet

NO - not a separate web application, a servlet that lives in the same web application as the JSP. That way you can use the session to store any amount of variable detail and your img tag can simply provide a key to retrieve the parameters from the session.
Bill
raminaa niilian
Ranch Hand

Joined: Jul 14, 2005
Posts: 551
Thank you for advice about using a servlet in same web application.
I described my problem with servlet in same web application in the following thread , sorry if i do not post it again because i describe it extensivly and pasting it here will just make the topic so long .

http://www.coderanch.com/t/362001/Servlets/java/servlet-url-mapping

i will be hapy if you take a look at that thread
thank you again
 
 
subject: Is it possible to return an image with a web service ?