how to show a default image

I have this code in which I show an image on a JSP page
<tr>
      <td width="386"><a border ='0' href="/chapters/pub"><img src="/chapters/attachments/images/<%=chapter_ID%>LOGO" width="386" height="61"></a></td>
</tr>

Now how can I write code which checks if an image with this name exists, if it doesn't show a default page

Any help will be greatly appreciated
huzefaqAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
rrzConnect With a Mentor Commented:
Here is one way.  
<%
  File file = new File(application.getRealPath("/chapters/attachments/images/" + chapter_ID + "LOGO"));
  if(file.exists()){
%>
<tr>
     <td width="386"><a border ='0' href="/chapters/pub"><img src="/chapters/attachments/images/<%=chapter_ID%>LOGO" width="386" height="61"></a></td>
</tr>
<%
   }else{ response.sendRedirect(request.getContextPath() + "/default.jsp"); return;}
%>
0
 
huzefaqAuthor Commented:
Thanks a lot

I actually end up with this code

<%
file = new File(application.getRealPath("attachments/images/" + chapter_ID + "SIMAGE.jpg"));
if(file.exists()){
              %>
                    <tr>
                        <td><img src="attachments/images/<%=chapter_ID%>SIMAGE.jpg" width="140" height="140"></td>
                    </tr>
              <%
                  }else{
              %>
                    <tr>
                        <td><img src="attachments/images/DefaultSIMAGE.jpg" width="140" height="140"></td>
                    </tr>
              <%
                }
              %>
            
0
 
huzefaqAuthor Commented:
If you don't mind I just wanted to know what does this code do

else{ response.sendRedirect(request.getContextPath() + "/default.jsp"); return;}
0
 
rrzCommented:
>if it doesn't show a default page  
I thought you wanted to go to another page. The code would have redirected the browser.
0
 
huzefaqAuthor Commented:
ok, thanks
0
All Courses

From novice to tech pro — start learning today.