Link to home
Start Free TrialLog in
Avatar of haneefnb
haneefnb

asked on

add images to jsp page dynamically


add dynamic content to jsp page
Bookmark:
Question: hi i want to add images to my jsp page dynamically..
<%
     int count=new File(filepath).listFiles().length;
%>
think from the above code i get count value as 10.now i want to add 10.jpg to my jsp page
by the following code.(now i want to add a image to my jsp page with respective the count value)
<table>
 <tr>
  <td><img src="images/ln/headings/"+count.jpg+""></td> (all my jpg files in images/ln/headings)
 </tr>
</table>
if the count value is 9, it has to add 9.jpg to my jsp page.
so, plz observe the above html code and correct that code and give correct one.
ASKER CERTIFIED SOLUTION
Avatar of Chad Smith
Chad Smith
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Please try this sample JSP.
<%@ page import="java.io.*" %>
<%
  String dirPath ="/images/ln/headings";
  String realPath = application.getRealPath(dirPath);
  File dir = new File(realPath);
  String[] list = dir.list(new FilenameFilter(){
                                       public boolean accept(File dir, String name){
                                                               return name.endsWith(".jpg");
                                       }
                             });
 
%>
<html>
<body>
<% 
  for(String imagePath:list){
    out.print("<img src=\"" + request.getContextPath() + dirPath + "/" + imagePath + "\"/>");
  }
%>
</body>
</html>

Open in new window