如何在JSP中显示文件夹中的所有图像?

时间:2019-01-05 19:34:35

标签: java jsp servlets

我使用jsp,servlet和bootstrap开发简单的网页,并且想要创建一个图库。我的画廊在jsp上只显示白屏。

这是我的servlet代码:

@WebServlet(name = "WelcomeServlet", urlPatterns = "/gallery")
public class FirstServlet extends HttpServlet {

private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");

    PrintWriter out = response.getWriter();

    getImageFromFolder(request, "D:\\images");


    RequestDispatcher rd = request.getRequestDispatcher("gallery.jsp");
    rd.forward(request, response);
}    

public void getImageFromFolder(HttpServletRequest req, String path) {

    List list = new ArrayList();
    File file = new File(path);

    for(File f : file.listFiles()) {
        String filename = f.getName();
        list.add(filename);
    }

    req.setAttribute("list", list);
}

在JSP中:

<div class="container">
        <h3>GALLERY</h3>

        <c:forEach var="img" items="${list}">
            <img src="<c:out value="${img}"/>" width="150" height="150" />
        </c:forEach>

</div>

我在服务器上的输出:

https://imgur.com/a/vYEWWe5

0 个答案:

没有答案