[JSP]同じディレクトリにあるファイルをリストする。
jspと同じディレクトリのファイルを出力するをもう少し簡単に。
index.jsp
紫本SJC-WC J2EE1.5用

index.jsp
<%@ page import="java.util.*,java.io.*,java.net.*,java.util.regex.*" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
pageContext.setAttribute("pagename","listing my files");
%>
<html>
<head>
<title>${pagename}</title>
</head>
<body>
<h1 style="background:white;">${pagename}</h1>
<%
String docsBase=application.getRealPath(request.getServletPath())+"/..";
%>
<%
File docDir = new File(docsBase);
List<String> fnlist = new ArrayList<String>();
List<String> dirlist = new ArrayList<String>();
for (File file : docDir.listFiles()) {
if (Pattern.matches(".*¥¥.(txt|html|htm)$", file.getName().toLowerCase()))
fnlist.add(file.getName());
if(file.isDirectory()){
dirlist.add(file.getName());
}
}
pageContext.setAttribute("fnlist",fnlist);
pageContext.setAttribute("dirlist",dirlist);
%>
<h3>DIRS</h3>
<c:forEach items="${dirlist}" var="dirname">
<a href="${dirname}"> <c:out value="${dirname}" escapeXml="true" /> </a><br>
</c:forEach>
<h3>FILES</h3>
<c:forEach items="${fnlist}" var="filename">
<a href="${filename}"> <c:out value="${filename}" escapeXml="true" /> </a><br>
</c:forEach>
</body>
</html>
紫本SJC-WC J2EE1.5用
コメント
コメントを投稿
「コメントを投稿できるユーザー」の範囲は変更される可能性があります。