wood burning stoves 2.0*
The moose likes Servlets and the fly likes servlet resource. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet resource." Watch "servlet resource." New topic
Author

servlet resource.

shiva rao
Ranch Hand

Joined: May 26, 2005
Posts: 31
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

Joined: Jan 26, 2001
Posts: 95
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.
 
Don't get me started about those stupid light bulbs.
 
subject: servlet resource.