Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Image in Servlet

 
amit sanghai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi everybody,

How do we load an Iage in a Servlet?
 
Bob Moranski
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String sourcePath = req.getPathTranslated();
Frame frame = new Frame();
//frame.addNotify();
Image image = Toolkit.getDefaultToolkit().getImage(sourcePath);
MediaTracker mt = new MediaTracker(frame);
mt.addImage(image, 0)
try{
mt.waitForAll();
}catch(Exception e){}
You can try to load the Image "image" like that. Depending on if you need the Graphics, if you do then you need to call Frame's addNotify().
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or, if your question is actually "ehy don't the images appear in HTML pages generated by servlets? It's probably because you are using relative URLs for your images, so the browser is trying to retrieve them from the servlets directory. The quickest solution is to hard-code the URL for the images, eg.
<img src="/images/arrow.gif">
 
amit sanghai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks Bob and Frank.

Bob , can you please explain your code in detail. Is the statement mt.addImage(image,0) correct?

Also explain the frame.addNotify() method?

Why do we use MediaTracker class? Can we use DoubleBuffering class?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic