Solved

how to show a default image

Posted on 2006-06-30
5
194 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Moving applications to the cloud or switching services to cloud-based ones, is a stressful job.  Here's how you can make it easier.
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 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

19 Experts available now in Live!

Get 1:1 Help Now