I have a User object which contain the array of byte(bytes of image) and the name. I have that object on JSP. I want to display the image and name. Can someone please help me to display both the things :(
Bear Bibeault wrote:JSP produces HTML. So, you figure out how to display what you want in HTML, and then figure out the JSP to make that happen.
So the first question is: how does one display an image in HTML? (Hint: it's not by using the bytes of the image.)
Thanks Bear for the response.
We display Image in HTML using "Img" tag and that will take the image path(src attribute). So are you pointing me to write all the Byte in a temp file and store it and use the path of temp file to display the Image.
Or is there is somthing else ?
I also tried to use the Buffered image and ImgaeIo classes but didn't get the success :(
http://myserver.com/myWebApp/Image?type=user&userID=theUserID, where the "type" parameter indicates the kind of image you want to serve (taking into account that in the future there might be other type sof images besides user images), and "userID" is some kind of ID that uniquely identifies the user. Example code for image streaming from a servlet can be found at http://www.coderanch.com/how-to/java/CodeBarnSimpleStream
BufferedImage and ImageIO would not be involved.
I manually create the link
With this also the image is not displayed
You create a buffer of zero length to hold a data. Of course reader cannot put anything in that buffer (arrays are not resizeable in java). So you output an empty buffer to a client.
Also any read call may fill only part of the buffer due to a multiple reasons. And further calls to read method will read more data.
So, you need to:
1. Allocate buffer of some nonzero length (1024 * 8 bytes, for example).
2. Use a loop to read a portion of the file and then write that portion to the output stream. you should read the file until end of file is reached. And you should write only a filled portion of the buffer. Read documentation for InputStream.read to see how to treat it's return values.
I am able to load the Image now :)
I am one more doubt in this. As of now I am working with single machine(Server and Client). In the Request I just pass the Image Name and in servlet I am pointing my desktop file. If the same thing I perform using 2 different machine(one is client and one is server) I believe it will create problem.
what if I want to upload the file and display it on another page. In that case How I upload the image. My servlet code will run at server side and from there I don't think we can point my client machine image.
Please correct me if I am going on wrong track. Help me to understand how we can do that ??
I want to upload the file which is at my machine to the remote server. I search over the google and found that there are apache library to upload the files. I am just thinking how I can do that without using third party library. Do I need to set the all the file into request and for that should I write the java code in jsp or is there is any another way.
If you are using a Servlet 3.0 container, it supports uploading. But otherwise, you need to use one of the 3rd-party libraries. Unless you want to write the code to parse multi-part requests yourself, and trust me, that is something you most certainly do not want to do! That's a needless wheel to re-invent, and a very complex one.
[Edit: Ulf snuck in just before me!]
I have completed my small program which is actually based using Spring Wizard Controller. In the Wizard flow My controller create a tem file and store it and while displaying information i used the Img tag which call the another servlet and read that Image and Display :rolling:
Thanks again for you help :beerchug: