Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Displaying image on jsp  RSS feed

 
JigarN Shah
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am setting image byte array to servlet output stream after setting content type as "image/jpg".

Problem 1:
if I set contentType as "image/jpeg" or "image/gif" it is showing some image on page and for some image red cross mark comes in upper corner.

Problem 2 :
For "image/jpg", File Download window pops up and asks me for saving the file. I don't want to save it.

Below is the sequence of code in my action class.

response.resetBuffer();
response.setContentType("image/jpeg");
servletOutputStream.write(barray);
servletOutputStream.flush();
servletOutputStream.close();

this image should be displayed on new opened window from where i have called above action.

 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

if I set contentType as "image/jpeg" or "image/gif" it is showing some image on page and for some image red cross mark comes in upper corner.

For the images that fail are they of the type defined in your contetn type/


Problem 2 :
For "image/jpg", File Download window pops up and asks me for saving the file. I don't want to save it.

This is what OSs do if they don;t know what to do with a specific MIME type. image/jpg is not a vailid MIME type.

If you use Tomcat, and if you use the Content-Disposition header and specify a file name (with the correct extension) then map Content-Types in web.xml the servlet container should correctly set Content-Type for you.
 
JigarN Shah
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your quick reply..

I was also suspecting that failing images might be of different format...
But that is not the case.
Because before redirecting image stream to servlet I also right it on local drive. (for testing purpose only)



I can view the image stored as file. But can't view the same on JSP.

Let me also tell you that I am getting image as hexadecimal string. I process it and convert it into byte array.
Hence I don't know how I can determine its MIME type.. Can't I view each image's hexadecimal string as jpeg/gif?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Browsers like to get a Content-Length with binary data such as images, try supplying that header also.

[picky-teacher-mode]You do not "display image on jsp" - you display image on browser from an link written into the HTML page a jsp produces. Using the wrong words for a process leads to sloppy thinking and bad programming.[/picky-teacher-mode]

A great way to learn about headers is to use a browser such as FireFox that will let you see all the headers that go into all the requests which create a typical HTML page loaded with images, css, etc.

Bill
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!