checking if an image exists

Posted on 2005-04-18
Last Modified: 2010-04-01
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?
Question by:filterhead80

    Author Comment

    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.
    LVL 19

    Accepted Solution

    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
    LVL 29

    Expert Comment

    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.

    Expert Comment

    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?
    LVL 29

    Expert Comment

    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("/") .........

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    At the beginning of the year, the IT world was taken hostage by the shareholders of LogMeIn. Their free product, which had been free for ten years, all of the sudden became a "pay" product. Now, I am the first person who will say that software maker…
    This paper addresses the security of Sennheiser DECT Contact Center and Office (CC&O) headsets. It describes the DECT security chain comprised of “Pairing”, “Per Call Authentication” and “Encryption”, which are all part of the standard DECT protocol.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now