checking if an image exists

I need to make sure that if I specify an image that it exists, if it doesnt then I would display a spacer.
Anyone know how to do that?
Who is Participating?
if you want to do it on the server side then you can do it by creating a new file and checking it

File f  = New File(yourimagename); //make sure that this is a absolute path from your jsp/servlet

//file is there
file is not there
filterhead80Author Commented:
Just in case I am pulling in  the image name and directory but there is an occasion where the image wont exist so Iwould like to cover that basis by "checking and seeing" if the image does exist.
Kuldeeps solution is what you need. To get the path to the image in a servlet or jsp without hardcoding it, you need to call

which will return the path to the root of your webapp. So if you have an image in <webapp>/images/headers/image.gif
you would call

String imgPath =  getServletContext.getRealPath("/") + "/images/headers/image.gif";

and then do the above solution.
I can't make your solution work on a JSP, error:
cannot resolve symbol
symbol  : variable getServletContext
Perhaps there is something that's not "getServletContext" I can use when I'm not in a servlet?
try using the implicit object "application" as it is a ServletContext object already rather than create a new one yourself.

so it becomes:
application.getRealPath("/") .........
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.