[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5110
  • Last Modified:

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.
0
haneefnb
Asked:
haneefnb
1 Solution
 
Chad SmithSoftware EngineerCommented:

<table>
 <tr>
  <td><img src="images/ln/headings/<%= count %>.jpg"></td> (all my jpg files in images/ln/headings)
 </tr>
</table>

Open in new window

0
 
rrzCommented:
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

0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now