File tree servlet 1.1


It is a Java servlet lets you obtain file tree info in JSON format. You can use this servlet in your client site (JavaScript) code for getting file info from your server.

How to use it:

a) copy filetreePackage.jar into your WEB-INF/lib directory.

b) define FileTreeServlet in your web.xml file.
 


    <servlet>
     <servlet-name>FileTree</servlet-name>
     <servlet-class>com.jsos.file.FileTreeServlet</servlet-class>
    </servlet>

c) define a mapping:
 


    <servlet-mapping>
     <servlet-name>FileTree</servlet-name>
     <url-pattern>/servlet/filetree</url-pattern>
    </servlet-mapping>

you can pass a name for the directory (or file) you are interested in as a parameter dir. And parameter callback describes a name for your JavaScript function (JSONP). E.g. the following code lists data from the directory /WEB-INF/data:
 


<html>
<head>
<script>

function f(dir)
{
var cbscript = document.createElement("script");
cbscript.src = "http://myhost/servlet/filetree?dir="+dir+"&callback=c";
cbscript.type = 'text/javascript';
document.body.appendChild(cbscript);
}

function c(list)
{
var s="";
for (var i=0; i {
var o = list[i];

if (o.isDir=="true")
s+="<"+o.name+"><br/>"
else
s+=o.name+"<br/>";
}

document.getElementById("d").innerHTML=s;
}
</script>
</head>
<body>

<input type="button" onClick="f('WEB-INF/data')" value="Get dir"/>

<div id="d">

</div>
</body>
</html>

our JavaScript callback gets back an array with info about files. You can obtain the following information about each object:

isDir possible values are true or false - is it directory or file
name describes a name
size describes a size for file
lastModified describes a last modified time

Also you can describe an initital parameter for this servlet: root. This parameter describes some directory on your server and restricts access to the server-side file system to this directory and its sub-directories only. Parameter dir (if any) describes in this case a subdirectory. E.g.:
 


    <servlet>
     <servlet-name>FileTree</servlet-name>
     <servlet-class>com.jsos.file.FileTreeServlet</servlet-class>
    <init-param>
    <param-name>root</param-name>
    <param-value>/WEB-INF/data</param-value>
    </init-param>
    </servlet>

and /servlet/filetree?dir=pics will describe actually a directory /WEB-INF/data/pics

    For downloading:   filetreePackage.jar

 © Coldbeans Software     Comments?

See also JSOS - the largest collection of servlets and filters.

Also in JSOS: