[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 164
  • Last Modified:

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?
0
filterhead80
Asked:
filterhead80
1 Solution
 
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.
0
 
KuldeepchaturvediCommented:
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

if(f.exist())
{
//file is there
}
else
{
file is not there
}
0
 
bloodredsunCommented:
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

getServletContext.getRealPath("/");
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.
0
 
MalinaliCommented:
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?
0
 
bloodredsunCommented:
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("/") .........
%>
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now