[오라클자바커뮤니티,JSP 학원입니다. 자바서블릿에서 디렉토리의 리소스들을 나열]Listing Web Archive Resource With ] javax.servlet.ServletContext.getResourcePaths
메소드를 이용해 서블릿 컨테이너내의 리소스에 대해 Query할 수 있는데 이 메소드는 String Object의 java.util.Set을 리턴 하며 이를 통해 특정한 디렉토리의 리소스들을 나열해볼 수 있습니다.
아래는
서블릿의 예제 입니다.
(“/”이 있느냐 없느냐에 따라 디렉토리냐 파일이냐를 구분 합니다.)
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Set;
import javax.servlet.*;
import javax.servlet.http.*;
//
// Interprets requests from input form
//
public class ListResourcesServlet extends HttpServlet
{
protected
ServletConfig _config;
public
ListResourcesServlet() {
_config =
null;
}
// Initialize
servlet instance, including getting home
// interface for
user objects.
public void
init(ServletConfig config) {
_config =
config;
}
public void
doGet(HttpServletRequest req, HttpServletResponse
res)
throws
IOException, ServletException {
// The
argument "path" is the path of the thing we want to
// display.
String path =
req.getParameter("path");
if (path ==
null) {
throw new
ServletException("ListResourcesServlet: parameter 'path'"
+
"
required");
}
// If its name
ends in "/", list the resources in that directory.
// Otherwise
serve the indicated file.
if
(path.charAt(path.length()-1) == '/') {
listDirectory(res, path);
} else
{
serveFile(req, res, path);
}
}
// Get name of ..
for given path
static String
dotdot(String path) {
// Strip off
terminal slashes
int lastpos =
path.length();
while (lastpos
> 0 && path.charAt(lastpos-1) == '/') {
lastpos--;
}
path =
path.substring(0, lastpos);
// If there's slash, return everything up
to
// that slash.
If the result is empty, then the
// result is
"/".
lastpos =
path.lastIndexOf('/');
if (lastpos
> 0) {
return
path.substring(0, lastpos);
}
return
"/";
}
protected void
listDirectoryLine(PrintWriter out, String text, String link)
{
boolean
isLinked = (link != null && link.length() !=
0);
if (isLinked)
{
out.print("<A HREF=\"listResources?path=" + link +
"\">");
}
out.print(text);
if (isLinked)
{
out.print("</A>");
}
out.println("<BR>");
}
// List the
resources in the directory rooted in "path"
protected void
listDirectory(ServletResponse res, String path)
throws
IOException, ServletException {
// Get output
writer
res.setContentType("text/html");
PrintWriter
out = res.getWriter();
out.println("<H2>Listing of resources in directory <I>"
+
path + "</I></H2>");
// If the path
variable is empty, list "/".
if (path ==
null) {
path =
"/";
}
// Create a
link to ".."
listDirectoryLine(out, "..", dotdot(path));
// Kill
terminal '/' if there is one (for proper formatting
// of
paths)
if (path.lastIndexOf('/') == path.length()
- 1) {
path =
path.substring(0, path.length() - 1);
}
// List
directory contents as resource links
ServletContext
ctx = _config.getServletContext();
Set files =
ctx.getResourcePaths(path);
Iterator i =
files.iterator();
while
(i.hasNext()) {
String url
= (String)i.next();
listDirectoryLine(out, url, url);
}
}
// Serve the file
with the given name
protected void
serveFile(ServletRequest req,
ServletResponse res,
String path)
throws
IOException, ServletException {
ServletContext
ctx = _config.getServletContext();
RequestDispatcher rd =
ctx.getRequestDispatcher(path);
rd.forward(req, res);
}
}
[결과]
Listing of resources in directory /
..
/src/
/original.war
/META-INF/
/WEB-INF/
/index.html
/htmlsrc/
[결과]
Listing of resources in directory /WEB-INF/
..
/WEB-INF/web.xml
/WEB-INF/classes/
[출처] 오라클자바커뮤니티 -
http://www.oraclejavacommunity.co.kr/bbs/board.php?bo_table=LecServletJSP&wr_id=264
오라클자바커뮤니티교육센터, 개발자전문교육, 개인80%환급
www.oraclejavacommunity.com
평일주간(9:30~18:10) 개강
(7/07)[기업100%환급]C#4.0,WinForm,ADO.NET프로그래밍
(7/07)[기업100%환급]Spring ,MyBatis,Hibernate실무과정
(7/07)[채용예정교육]오라클자바개발잘하는신입뽑기프로젝트,교육전취업확정
(7/14)[기업100%환급]SQL기초에서 Schema Object까지
(7/14)[기업100%환급]PL/SQL,ORACLE HINT,TUNING
(7/14)[기업100%환급]안드로이드개발자과정
(7/21)[기업100%환급]자바기초에서 JDBC, Servlet/JSP까지
평일야간(19:00~21:50) 개강
(7/09)닷넷(C#,Network,ADO.NET,ASP.NET)마스터과정
(7/10)JAVA,Network&WEB&Framework(자바기초에서웹스프링까지)
(7/11)웹퍼블리싱 마스터
(7/15)SQL기초에서실무까지
(7/15)안드로이드개발자과정
(7/15)MyBatis3.X, Hibernate4.X ORM실무과정
(7/16)Spring3.X, MyBatis, Hibernate실무과정
(7/22)자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지
주말(10:00~17:50) 개강
(7/05)자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지
(7/05)SQL초보에서 Schema Object까지
(7/12)MyBatis3.X, Hibernate4.X ORM실무과정
(7/12)개발자를위한PLSQL,SQL튜닝,힌트
(7/13)C#,ASP.NET마스터
(7/19)Spring3.X, MyBatis, Hibernate실무과정
(7/19)웹퍼블리싱 마스터
(7/19)안드로이드개발자과정
주말저녁(18:30~22:20) 개강
(7/19)JAVA,Network&WEB&Framework
(8/09)SQL기초에서실무까지
www.oraclejavacommunity.com
평일주간(9:30~18:10) 개강
(7/07)[기업100%환급]C#4.0,WinForm,ADO.NET프로그래밍
(7/07)[기업100%환급]Spring ,MyBatis,Hibernate실무과정
(7/07)[채용예정교육]오라클자바개발잘하는신입뽑기프로젝트,교육전취업확정
(7/14)[기업100%환급]SQL기초에서 Schema Object까지
(7/14)[기업100%환급]PL/SQL,ORACLE HINT,TUNING
(7/14)[기업100%환급]안드로이드개발자과정
(7/21)[기업100%환급]자바기초에서 JDBC, Servlet/JSP까지
평일야간(19:00~21:50) 개강
(7/09)닷넷(C#,Network,ADO.NET,ASP.NET)마스터과정
(7/10)JAVA,Network&WEB&Framework(자바기초에서웹스프링까지)
(7/11)웹퍼블리싱 마스터
(7/15)SQL기초에서실무까지
(7/15)안드로이드개발자과정
(7/15)MyBatis3.X, Hibernate4.X ORM실무과정
(7/16)Spring3.X, MyBatis, Hibernate실무과정
(7/22)자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지
주말(10:00~17:50) 개강
(7/05)자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지
(7/05)SQL초보에서 Schema Object까지
(7/12)MyBatis3.X, Hibernate4.X ORM실무과정
(7/12)개발자를위한PLSQL,SQL튜닝,힌트
(7/13)C#,ASP.NET마스터
(7/19)Spring3.X, MyBatis, Hibernate실무과정
(7/19)웹퍼블리싱 마스터
(7/19)안드로이드개발자과정
주말저녁(18:30~22:20) 개강
(7/19)JAVA,Network&WEB&Framework
(8/09)SQL기초에서실무까지
댓글 없음:
댓글 쓰기