wood burning stoves 2.0*
The moose likes Servlets and the fly likes How do I render images using servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How do I render images using servlets" Watch "How do I render images using servlets" New topic
Author

How do I render images using servlets

PhatD
Greenhorn

Joined: May 18, 2001
Posts: 2
hello,
I'm doing a simple print to the browser. I have something like this:
out.println("<img src=\"someimage.jpg\">");
my image doesn't show up though.. any ideas on how i can display images using servlets. thanx.


Java Rocks
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12809
    
    5
The most important thing to do is think like a browser.
I suggest you get a mockup of your page running as pure HTML first, then convert to JSP. If your output page is being created by a servlet, the browser thinks a resource like src="image.jpg" should come from the same address as the servlet because you have not specified a complete URL.
It is a good idea to use a <base> tag to tell the browser where to find resources such as images, sound files, etc. that have relative addresses.
Bill
PhatD
Greenhorn

Joined: May 18, 2001
Posts: 2
thanks for the follow up.. i did test the image path before i used it. Also I provided the full path in my img url but still no luck. I know i might end up using jsp to display my page i was just wondering how images were rendered using servlets.
I can send images to the browser if i set my content type to the image, but if i need to display text too my image displays as text(gibberish).. is there a way to alter the content type after its been already set as "text/html" ? thanx again
Andrew Shafer
Ranch Hand

Joined: Jan 19, 2001
Posts: 338

Servlets do not render anything.
Something is wrong with the way you are writing the JSP.
Request the JSP and then view source in the browser. That should tell you what is wrong with the generated html.


!_I_Know_Kung_Fu_!
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
"PhatD"
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in with a new name which meets the requirements.
Thanks.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12809
    
    5
"I can send images to the browser if i set my content type to the image, but if i need to display text too my image displays as text(gibberish).. is there a way to alter the content type after its been already set as "text/html" ? thanx again"
Remember my first advice: "Think like a browser"
A browser does NOT get a mix of text and images in one request. Each <img> tag generates a separate request for the image resource. Your servlet must recognize that an image is requested and send the appropriate response. This means a content type and content length in the headers and a binary stream for the content.
Most servlets books will cover this, I know mine does and so does Hunters. Your servlet can create an image on the fly and send it.
Bill

------------------
author of:
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How do I render images using servlets