Solved

how to show a default image

Posted on 2006-06-30
5
197 Views
Last Modified: 2010-08-05
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
0
Comment
Question by:huzefaq
  • 3
  • 2
5 Comments
 
LVL 27

Accepted Solution

by:
rrz earned 500 total points
ID: 17020365
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
 

Author Comment

by:huzefaq
ID: 17054213
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
 

Author Comment

by:huzefaq
ID: 17054221
If you don't mind I just wanted to know what does this code do

else{ response.sendRedirect(request.getContextPath() + "/default.jsp"); return;}
0
 
LVL 27

Expert Comment

by:rrz
ID: 17054445
>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
 

Author Comment

by:huzefaq
ID: 17054568
ok, thanks
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How do we balance the user experience (UX) with reasonable security measures? It can be done, if you keep these fundamentals in mind.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 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

12 Experts available now in Live!

Get 1:1 Help Now