user1441218
user1441218

Reputation: 61

Allow directory browsing in my java webserver

I am developing a webserver using java and i want to list all the file under a specific directory from the browser. which http responce code should i send to the browser and how can i send the list of files to the browser.

Upvotes: 1

Views: 403

Answers (1)

Zarkonnen
Zarkonnen

Reputation: 22478

You just want to send a normal HTTP 200 code, and return an HTML page that lists the files in the directory. If you see a listing like that from eg an Apache server, it's just the server generating HTML. HTTP has no built-in way of sending the contents of a directory. (Unlike eg FTP or Gopher.)

Short example of the kind of page you could generate and return:

<!DOCTYPE HTML>
<html>
    <head><title>/Users/Bob/myServerRoot/folder1/folder2/</title></head>
    <body>
        <h1>/Users/Bob/myServerRoot/folder1/folder2/</h1>
        <ul>
            <li><a href="..">..</a></li>
            <li><a href="folder2/myfile.txt">myfile.txt</a></li>
        </ul>
    </body>
</html>

Upvotes: 2

Related Questions