views:

51

answers:

2

I have this code here:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author Nathan Campos
 */
public class Files extends HttpServlet {
    PrintWriter out = null;              // moved outside doGet() for use in ls()
    @Override
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html");
        // PrintWriter out = response.getWriter(); would create a copy no accessable in ls()
        out = response.getWriter();   // this uses the out declared outside
        File startDir = new File("C:\\test");
        ls(startDir);
    }

    private void ls(File f) {
        File[] list = f.listFiles();
        if ( list == null ) {
            out.println("Returned null");
                    return; // otherwise the for loop will crash
        }
        for(File file : list) {
            if(file.isDirectory()) {
                ls(file);
            } else {
                out.println("<a href='+file.toURL()+'>'+file.getName()+'</a>");
            }
        }
    }
}

But I want to make it search on the folder C:\WorkFiles\ServletFiles. How could I do this?

Update: When I tried to use private void ls(File f)(without being static), I got this error on the browser(running Tomcat):

java.lang.NullPointerException
    Files.ls(Files.java:30)
    Files.doGet(Files.java:18)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:627)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
+2  A: 

The directory where you start should be read from configuration. However you might call it like that:

PrintWriter out = null; // moved outside doGet() for use in ls()

public void doGet(HttpServletRequest request,
                  HttpServletResponse response)
throws ServletException, IOException {
    response.setContentType("text/html");
    out = response.getWriter();

    File startDir = new File("C:\\WorkFiles\\ServletFiles");
    ls( startDir );
}

The printing line in ls() has an issue (you can't mix ' and " ) and should be rewritten as

out.println("<a href="+file.toURL()+'>'+file.getName()+"</a>");

(Assuming you wan't the output in the redered html page instead of stdout)

Note: The deprecated file.toURL() method throws MalformedURLException

EDIT:

Since listFiles could return null you should also add

File[] list = f.listFiles();
if ( list == null ) return;
stacker
I just got this error: `non-static variable out cannot be referenced from a static context`. Refereed to the `out.println(url here)`
Nathan Campos
Remove static from the ls() method, making "PrintWriter out" static would be a bad idea.
stacker
@stacker: I'm still getting the same thing
Nathan Campos
+1  A: 

Unrelated to the actual problem:

PrintWriter out = null;              // moved outside doGet() for use in ls()

This is an extremely bad idea. This way the response writer is shared among all HTTP requests and every new HTTP request would override the response writer of the previous request. When the previous request was actually still unfinished, then the client would never retrieve the remnant of the response and this would instead be shown in the response of the latest request of another client.

In other words: your servlet is not threadsafe. Don't get hold of request and/or session specific data as instance variabel of a servlet. Just pass it along as method argument.

private void ls(File f, PrintWriter out) {
    // ...
}

See also:

BalusC