I have following method in a servlet. It calls ImgTable.getImage() method which gets Blob data from database, converts it into bytes (using getBytes) and return byte array. Once I have the byte array in servlet, I just want to display the image. It is not displaying anything - just shows empty image box with big red X on top left corner.
Please take a look and let me know if I am missing anything.
System.out.println("in doGet method"); try { ImgTable img = new ImgTable(); // get image data in form of byte array byte[] buff = img.getImage(); // buff now image data in forma of byte array if (buff == null) { System.out.println("failed"); } else { System.out.println("got byte array of size: " + buff.length); //display it on browser response.setContentType("image/jpeg"); ServletOutputStream ostr = response.getOutputStream(); response.setContentLength(buff.length); ostr.write(buff); ostr.flush(); ostr.close(); } } catch (Exception x) { x.printStackTrace(); } } // end doGet
Originally posted by Bear Bibeault: I assume this servlet is being referenced by an <img> tag on the page?
Yes - as
tripti pandey
Greenhorn
Joined: Mar 09, 2006
Posts: 5
posted
0
Hey..were you able to find solution for this problem, i am facing same problem. I have a image as byte array that i need to display, following is the code i wrote in custom tag for the same :
OutputStream o = response.getOutputStream(); o.write(byteImage); o.flush(); o.close();
In the JSP i am using following code <img src="displayImage.jsp">
and this displayImage.jsp finally calls the custom tag. However, it seems that img tag is not accepting jsp. My debugger never reaches inside the custom tag implementation.
You are beating yourself in the head with a hammer trying to render binary data from a JSP. You are much better off doing this sort of thing in a servlet.
More info on why JSP is such a poor technology choice can be had in this article.
tripti pandey
Greenhorn
Joined: Mar 09, 2006
Posts: 5
posted
0
my query basically is can an img tag point to jsp, will jsp be called in that case? please reply polietly, else dont
There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.
In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.
"tripti", please re-read Bears request to change your display name.
Display names must contain a first and last name. I recommend you change it back to the original value, since accounts with invalid display names get deleted.
thanks, Dave.
dema rogatkin
Ranch Hand
Joined: Oct 09, 2002
Posts: 294
posted
0
JSp should work fine, just get hand on out stream and write binary data. let me know if it isn't working. BTW Does anybody have a code for generation an image with text not recognizable by OCR, something similar to Google mail when you do many failed login attempts you start to getting an image.
Tough in space?, <a href="http://tjws.sf.net" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="http://mediachest.sf.net" target="_blank" rel="nofollow">Check it here.</a><br /><a href="http://7bee.j2ee.us/book/Generics%20in%20JDK%201.5.html" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="http://7bee.j2ee.us/bee/index-bee.html" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="http://7bee.j2ee.us/addressbook/" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="http://searchdir.sourceforge.net/" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
I believe it is referred to as 'Capcha' or 'Captcha' and there is a Java API available for it. Unfortunately I don't have any more information at this stage, but tell us how you go.