import java.io.File; import java.io.IOException; import java.io.PrintWriter; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; @WebServlet("/DirIndex") public class DirIndex extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType( "text/html; charset=UTF-8"); PrintWriter out = response.getWriter(); // ↓適切なパスに変更する String path = getServletContext().getRealPath("/"); File dir = new File(path); // dir にあるファイル名の配列を得る String[] files = dir.list(); out.println("
"); out.println(""); int i; out.printf("%sのファイル一覧%n%n", path); for (i = 0; i < files.length; i++) { // files の各要素を順に出力 out.println(files[i]); } out.println(""); out.println(""); out.close(); } }