0

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)
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Nathan Campos
  • 28,769
  • 59
  • 194
  • 300
  • doGet does not call ls-method. Maybe you forgot to paste something? – Joni Jul 08 '10 at 16:15
  • f.listFiles(); may return null (I saw this while testing in c:\\windows) and thought it happens only with windows 'special files' check for null and return if so. – stacker Jul 08 '10 at 17:48
  • @stacker: I'm still getting the same thing – Nathan Campos Jul 08 '10 at 18:15
  • @Nathan could you please post the whole code, or at least describe what is in line 30 of Files.java? – stacker Jul 09 '10 at 07:50
  • @Nathan Oops PrintWriter out = response.getWriter(); should be out = response.getWriter(); otherwise the member out used in ls() isn't initialized. Also add a return in the block if ( list == null ) – stacker Jul 09 '10 at 09:44
  • I edited your code to reflect the required changes, hope it works now – stacker Jul 09 '10 at 09:47

2 Answers2

2

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
  • 68,052
  • 28
  • 140
  • 210
1

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555