Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I render images using servlets

 
PhatD
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic