Link to home
Start Free TrialLog in
Avatar of Mike R.
Mike R.

asked on

Design .jsp (or other) to dynamically create page based on items found in directory

Hey Gang,

I am a Solaris admin, but not a web designer (have had a little experience...very little.)

We are running Apache 2.0.35 on Solaris 5.8.

I would like to create a page (.jsp, or whatever) that dynamically looks at the list of items within the “../htdocs/subdir” directory within Apache, and creates a page on the fly with those items as links to that file/dir.

The reason being is, we want to be able to have scripts copy directories full of “txt” documents into “../htdocs/subdir” without us having to hand re-write the web page with the links.

I.E...
# ls ../apache2/htdocs/servers
serv1 (dir)
myserv (dir)
document.txt
differentDoc.html

Webpage****
Hi!  Welcome to the admin webpage!  Click any of the links below...
serv1
myserv
document.txt
differentDoc.html

...and clicking differentDoc.html brings up that page, clicking document.txt brings up that doc, and clicking on the link for serv1 or myserv just takes you into that dir (the subdir does not need formatting, per se’-as it will just be .txt docs the person can click on.)

A million thanks
Mike
ASKER CERTIFIED SOLUTION
Avatar of bloodredsun
bloodredsun
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is a neater version that also returns whether the file is a "file" or a "directory". You should note that this jsp works for the current webapp that it is in (it finds that from the call getServletContext().getRealPath("/") ). If you wanted to hard code the apache htdocs directory, then just replace line with the correct one.

new version of ls.jsp
--------------------------
<%@ page import="java.io.*, java.util.*" %>
<html>
<head><title>list files and directories</title>
</head>
<body>
<%
      String currentLocation = getServletContext().getRealPath("/") ;
      String[] arDirs = new File(currentLocation).list();
      Arrays.sort(arDirs) ;
      for( int ii=0 ; ii<arDirs.length ; ii++){
            File file = new File(currentLocation + arDirs[ii]) ;
            String type = file.isFile() ? " &lt;file&gt;" : "  &lt;dir&gt;";
            out.println( "<a href='"+arDirs[ii]+"'>"+ arDirs[ii] + type +"</a><br>") ;
      }
%>
</body>
</html>
Avatar of Mike R.
Mike R.

ASKER

Hey Gang,

Sorry about all the delays.  As it turns out, I am being forced to re-allocate this server to a different function...so I am going to have to start the process over.

I am going to assign points since I think you gave me what I need to know...I just need to test it.  I will resubmit another question if i run into more snags!

Thanks for the help and the patience!!