Win a copy of Practical SVG this week in the HTML/CSS/JavaScript 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.
 
Whatever. Here's a tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!