Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Image issue with Servlet

 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,

I am using following code that calls the servlet that in turn sets the output stream to the response. and my image src calls the servlet and it should display the chart. But it is not happening. It does call the servlet but it is not displaying the image. Can any one tell me where am i mistaking.



Servlet
-------------------------------------------------------------
OutputStream out = businessObj.getChart(inputMap);

res.getOutputStream().write(outputStream.toByteArray());
-----------------------------------------------------------
Jsp
-----------------------------------------------------------
url= request.getContextPath() +"/chart?current="+date;
<img src="<%=url%>" align="right" border="1" />



Thanks
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried writing the contents of the output stream to a file, or the console, to make sure it contains the correct output?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. are you setting the response content-type correctly?
2. are you setting the response content-length correctly?
3. what does the URL for the request look like?

Bill
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by William Brogden:
1. are you setting the response content-type correctly?
2. are you setting the response content-length correctly?
3. what does the URL for the request look like?

Bill

it was working fine earlier.We changed our project hierarchy and switched to tomcat 5.5.25. I dont know what happened to it now. all was working well before it.

2. i did not get this question.I am not aware about how to set content-length in response. i have not set any such variable.
3. it is /appname/servletname?current=Tue Jul 01 09:39:20 EDT 2008.but it was earlier /servletname?current=Tue Jul 01 09:39:20 EDT 2008.but if it is able to hit the servlet inspite of the changed url, how did it start malfunctioning
[ July 01, 2008: Message edited by: Patricia Samuel ]
 
Siddharth Naik
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not aware about how to set content-length in response


res.setContentLength(myByteArrayOutputStream.size());
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason I suggest setting the content-length is that browsers are sometimes picky about interpreting binary data without it.

Another possible problem occurs to me - are you closing the output stream after writing the image data to it? The last buffer load of data may be just sitting there.

Bill
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks William .

I found the reason. It was working earlier fine. But there is problem with jfreechart 1.0.9 version. it does not support JPEG image properly. Hence i changed to PNG.Now its working fine.



Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic