Skip to content

Instantly share code, notes, and snippets.

@authentical
Last active October 2, 2018 04:57
Show Gist options
  • Save authentical/d85c42389080082983b87d105f645db7 to your computer and use it in GitHub Desktop.
Save authentical/d85c42389080082983b87d105f645db7 to your computer and use it in GitHub Desktop.
This JSP helps me view the folders and files in the WebContent directory
<%@page import="java.util.Arrays"%>
<%@page import="java.io.File"%>
<%@page import="java.util.ArrayList" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%
File jsp = new File(request.getSession().getServletContext().getRealPath(request.getServletPath()));
File[] directoryListArray = jsp.getParentFile().listFiles();
ArrayList<File> directoryList = new ArrayList<>();
directoryList.addAll(Arrays.asList(directoryListArray));
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Index of WebContent</title>
</head>
<body>
<h1>Index of WebContent</h1>
<table>
<tbody>
<tr><th valign="top"><th>Name</th></tr>
<tr><th colspan="5"><hr></th>
<%
StringBuilder index= new StringBuilder();
for(File entry: directoryList){
int lastSlashIndex = entry.toString().lastIndexOf('\\');
index.append("<tr><td valign=\"top\"></td>");
index.append("<td>" +
entry.toString().substring(lastSlashIndex, entry.toString().length()) +
"&nbsp;</td>");
index.append("</td><td>&nbsp;</td></tr>");
out.write(index.toString());
index.setLength(0);
}
%>
<tr><th colspan="5"><hr></th>
</tbody>
</table>
<address>Java EE at localhost Port 8080</address>
</body>
</html>
@authentical
Copy link
Author

This WebContent directory view is a bit messy right now. Pretty sure I can use Spring functionality to make this cleaner.. but right now its just a plain Dynamic Web Project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment