How to list contents of a server directory using JSP?

后端 未结 5 1610
心在旅途
心在旅途 2020-12-09 20:03

When writing a JSP file, how can I get the current directory of this file at runtime
(to be able to iterate the directory and list its contents)?

<
5条回答
  •  被撕碎了的回忆
    2020-12-09 20:20

    As of Version 2.1 of the Java Servlet API use:

    File jsp = new File(request.getSession().getServletContext().getRealPath(request.getServletPath()));
    File dir = jsp.getParentFile();
    File[] list = dir.listFiles();
    

提交回复
热议问题