Rather than embedding a reference to the image data, you need to return a properly formed response containing the correct http headers, then the image data. Here's an excerpted example from one of my servlets that does this:
ImageDTO imageValue = ResourceFacadeDelegate.getInstance().getImageDTO(imageKey); response.setContentType(imageValue.getMimeType()); response.getOutputStream().write(imageValue.getImageData());
Okay i have changed my code the following way (in the bold) to take care of the appropriate content type i am showing on the html page(since i only have one gif file , i know the content type should be image/gif .
since the code you gave..probably uses your own custom-classes like ImageDTO...(i think so ...so i couldnt use that for my problem-solution)
iLength = (int)image.length();
ii = new ImageIcon(image.getBytes( 1, iLength ));
out.println("here's the icon");
When i do it as above, the image still doenst show up and when i right click to do View image in the browser , i get Tomcat error page saying
HTTP Status 404 - /timeraccess/servlet/javax.swing.ImageIcon@85e57 type Status report message /timeraccess/servlet/javax.swing.ImageIcon@85e57 description The requested resource (/timeraccess/servlet/javax.swing.ImageIcon@85e57) is not available. Apache Tomcat/5.0.28
so kindly help me with either understanding your code snippet as to what package are the classes ImageDTO ,ResourceFacadeDelegate,imageKey belong to .
or kindly help me understand what's wrong with the code above assuming i know for sure, its always going to be an image/gif
type of image,that i am trying to display.
thanks in advance ...loooking forward to hearing from you soon.
As you suggested(as i concluded lookinga at your code as of what i can do using the java package without your custom classes) to get a separate output stream to write this image content onto the page,
i dont make the Imageicon, just get the image Blob from the DB. then find its length.
then when i do
Tomcat webserver says, method .getWriter as already been called for this response object and displays an IllegalStateException and webBrowser shows the regular
empty image placeholder.
10:27:32,595 INFO [STDOUT] hi there 10:27:32,615 INFO [STDOUT] filename jchatbox.gif 10:27:32,625 INFO [STDOUT] imagecom.mysql.jdbc.Blob@85a863 10:27:32,665 INFO [STDOUT] ilength=5779 10:27:32,665 INFO [STDOUT] sucessflag 10:27:32,675 INFO [STDOUT] GenericException: getWriter() has already been called for this response 10:27:32,695 INFO [STDOUT] Caught an unexpected exception! 10:27:32,695 INFO [STDOUT] java.lang.IllegalStateException: getWriter() has already been called for this response 10:27:32,845 INFO [STDOUT] at org.apache.coyote.tomcat5.CoyoteResponse.getOutputStream(CoyoteResponse.java:568) 10:27:32,865 INFO [STDOUT] at org.apache.coyote.tomcat5.CoyoteResponseFacade.getOutputStream(CoyoteResponseFacad ava:148) 10:27:32,945 INFO [STDOUT] at dev.servlets.AccessTimerServlet.doPost(AccessTimerServlet.java:107)
I understand this , coz i have already
response.setContentType("text/html"); PrintWriter out = response.getWriter();
in my servlet code.
so what do you think can be get-around for this situation.
or if you can please provide some more insight into your own code i would really appreicate it...as i am nearing my MAster's project deadline...and need help with this ...pretty badly.
I'm a little confused with the code snippet you provided... Is that servlet writing HTML an the icon in the same response? If so, I think that's not allowed, so you should provide a servlet for the HTML and another one for the images.
If your servlet just provides an image, then you must not call "response.getWriter()" 'cause this method provides a character oriented output stream and you need a byte oriented one, which is provided with the call "response.getOutputWriter()". Finally, say that you cannot call "response.setContentType()" after writing something to the output.