Find.java import java.io.File; import java.util.ArrayList; import java.util.List; public class Find { public File folder = new File(""); static String temp = ""; private String text; public void setText(String text) { this.text = text; } public String getText() { return text; } public List<String> getList() { return getFiles(new File(text)); } private List<String> getFiles(File f) { List<String> result = new ArrayList<String>(); if (f.isDirectory()) { File[] list = f.listFiles(); if (list == null) { return result; } for (File d : f.listFiles()) { if (d.isDirectory()) { result.addAll(getFiles(d)); } else { result.add(d.getName()); System.out.println(d.getPath()); } } } return result; } 

secondpage.jsp

 !DOCTYPE HTML> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@page import="ru.sbrf.asfs.stub.stubsUtils.Find"%> <%@page import="java.util.List"%> <%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% Find find = new Find(); String name = request.getParameter("text"); System.out.print(name); find.setText(name); List<String> list = find.getList(); %> <html> <head> <title>secondpage</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <table border="1"> <th value=> <c:forEach var="l" items='<%= list %>'> <c:out value="${l}"/> <br> </c:forEach> </th> <th Путь> </th> </table> </body> 

    1 answer 1

    It is a sin to use scriptlets in our age. Better this way:

    Lister.java

     package com.example; import java.io.IOException; import java.io.File; import java.nio.file.Files; import java.nio.file.Paths; import java.util.stream.Collectors; public class Lister { private String path; public Lister() {} public void setPath(String path) throws IOException { if (!new File(path).isDirectory()) throw IOException("Not a directory!"); this.path = path; } public String getPath() { return path; } public List<File> getFiles() throws IOException { return Files.walk(Paths.get(path)) .filter(Files::isRegularFile) .collect(Collectors.toList()); } } 

    secondpage.jsp

     <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!doctype html> <html> <head> <title>Test</title> </head> <body> <jsp:useBean id="lister" class="com.example.Lister"> <jsp:setProperty property="path" value="${param.text}"/> </jsp:useBean> <table> <c:forEach var="f" items="${lister.files}"> <tr> <td> <c:out value="${f.absolutePath}" /> </td> <td> <c:out value="${f.length()}" /> </td> <td> <c:out value="${f.lastModified()}" /> </td> </tr> </c:forEach> </table> </body> </html> 

    It is possible for Lister to complicate a little more, so that instead of a list of File objects, it returns a list of wrappers that beautifully show the size and modification date.

    • Thank you, everything is fine, it works. Only unfortunately, where it is necessary, jdk 1.7 costs (it will seem strange to you, but I can’t change it, because it doesn’t depend on me), so there is no walk method. - Pavel Govorkov