[JSP]同じディレクトリにあるファイルをリストする。

jspと同じディレクトリのファイルを出力するをもう少し簡単に。

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用

SUN教科書 Webコンポーネントディベロッパ(SJC-WC) 試験番号:310-083


コメント

このブログの人気の投稿

4.3.0 Temporary Lookup Failureでドツボってた話

tomcat起動時の環境変数でJRE_HOMEを指定するときに

何が得られて何処へ向かうかだけを問うべき