This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlet resource.

 
shiva rao
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am try to check whether a image is there with in the war file or not. If found I will display the image at front end. If not I want to show a custom message.

Right not I am checking like
try {
session.getServletContext().getResourceAsStream("images/client1.gif").read();
return "images/client1.gif";
}catch (Exception e){
e.printStackTrace();
return "Not Found";
}
In the above code if it throws an exception then I get the "Not Found" message.

Is there a better way to achieve this.

Thanks!
 
Bala Krishna
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could get the RealPath of the image using the getRealPath method in ServletContext class. That path can be used to construct a java.io.File object and you can call the exists() method on that File object, to see if the image exists. There may be better and simpler ways of doing this.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic