File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: servlet resource.