Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how does JSP get an image created from a servlet ?

 
mike zhang
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose "MyServlet.java" servlet creates an image object. Then it forwards to "MyJsp.jsp" page. In the JSP page how do I show the image "MyServlet" just created ?
I know if it is a normal object, I can use beans by "setAttribute" and "getAttribute". How to handle image object ? Do I have to save it as a file first and then let jsp get that file ? Or there is better way ?
thanks lot.
Mike
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stop and think about what a HTML page that contains an image looks like.
Right! it has an img tag with an href to the source of the image. You can't mix the binary data of an image with the character data of an HTML page.
Your MyJsp.jsp page has to generate an img tag with an href pointing to the image generating servlet. The servlet has to generate binary data (ie OutputStream, not Writer) and must set the headers for content type and content length.
Most servlet books (mine included) discuss this.
Bill
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to expound on William's answer: you've sort of got it backwards. Your servlet shouldn't be pushing the image content to the JSP, your HTML page (generated by the JSP) should pull the image from the servlet via an img tag as William discussed.
hth,
bear
 
flying jordan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Happened to read this thread. I had the similar problem. Now I want to make sure if I understand Bill's point:
1. In the JSP page that include both image and other html text, the standard way to get an image is <img src="myIgameFile.gif">. So do you mean servlet HAS to generate an image file and JSP just refer to that file ?
2. Is it possible for servlet NOT to generate a image file and JSP can still directly get it ? If so,Could you write few lines sample ?
 
Sandep Chaturvedi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1. In the JSP page that include both image and other html text, the standard way to get an image is <img src="myIgameFile.gif">. So do you mean servlet HAS to generate an image file and JSP just refer to that file ?
servlet doesnt generate
2. Is it possible for servlet NOT to generate a image file and JSP can still directly get it ? If so,Could you write few lines sample ?

1. Servlet in this case does not generate the image. Its a seperate request to a location on the server where image is located.
2. I think, you can do something like this:
<img src="yourhost/servlet/imageservlet">
In your servlet you can set content type to image and write image to servletoutputstream -- if you have a dynamic image (such as from database or different image based on request parameter). Sometime this is advantageous as you can introduce compression,quality etc -- this is how some products such as JClass from sitraka work.
Other experts please correct me if I am wrong.
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by William Brogden:
Stop and think about what a HTML page that contains an image looks like.
Right! it has an img tag with an href to the source of the image. You can't mix the binary data of an image with the character data of an HTML page.
Your MyJsp.jsp page has to generate an img tag with an href pointing to the image generating servlet. The servlet has to generate binary data (ie OutputStream, not Writer) and must set the headers for content type and content length.
Most servlet books (mine included) discuss this.
Bill

Quite right! Most of the older examples tell you to use the proprietary Sun image encoders (at least the ones I read did), though. Those classes are not necessarily available on all JDKs (such as ones developed by IBM or other vendors). In JDK1.4, they have introduced a new javax.imageio package which allows you to stream image objects in a portable manner. Hope this helps.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2. Is it possible for servlet NOT to generate a image file and JSP can still directly get it ? If so,Could you write few lines sample ?

You don't have to generate a file, the servlet can write an output stream as if the HTML was retrieving a file from the server. You can do that with any resource that a HTML page might ask for. I did it with sound files and zip files in addition to images.
Its typically a bit more than a few lines though.
Bill
 
Sandep Chaturvedi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sometimes it is also used as a technique to speed up the performance, stream the image and gzip it. most browsers can handle unzipping it on the client end. If you just have a simple .gif file in your image tag, it can be slower if the client is international.
 
flying jordan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess you guys didn't totally understand my scenario (maybe I am wrong). My scenarios is:
I have a "Servlet_A" which generates a image basd on the user input, it also can set some bean attribute. Now I want to display the result in a
"Display.jsp" page which includes the image and those text context set by those beans, by using "forward" from the "Servlet_A" to "Display.jsp". SO, in this case, <img src="/servlet/Servlet_A"> method doesn't work because "Display.jsp" is not going to get a image from a standlone servlet. It wants to get the image from the servlet that forwards to this JSP.
but you can't make recursive calls in the "src=..". I guess that's the dilamma.
 
Sandep Chaturvedi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. page1.jsp -> controller
2. controller generates the outputstream for image
3. controller sets the bean properties, one of the properties is the outputstream for image. BEAN HAS SESSION SCOPE.
4. controller forwards to display.jsp which has img=servlet tag and usebean tag for display info
5. servlet gets the ouputstream from the bean (since it has session scope, it should get the value) and creates the image.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to refactor your design somehow because you can't do what you want with the present design.
IF you want the servlet to generate the image and then forward to JSP, THEN the image will have to be stored somewhere for the browser to request it - ie a file.
IF you want to generate the image on the fly THEN you will have to hand a servlet enough information to do the generating. Possibly as an object attached to a session.
Bill
 
Ricardo Gil
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look my little article in this page:
jsp/servlet image on the fly

it�s portuguese but it�s not diff. to understand, look only the code.. forget the rest..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic