0

我正在尝试编写一个简单的 java servlet 来列出目录中的文件。路径存储在 web.xml 的 init-param 中。当我调用 getInitParameters() 时,它返回目录路径。但是当我尝试将它传递给处理程序时,它返回 null。不知道我做错了什么。有什么帮助吗?

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

interface Handler {
  public void doGet (HttpServletRequest request, HttpServletResponse response) 
    throws IOException; 
}

class DispatchChoice {
  public final String param; 
  public final GetHandler getHandler; 
  public DispatchChoice (String param, Handler getHandler) 
  {
    this.param = param;
    this.getHandler = getHandler;
  }
}

public class MyServlet extends HttpServlet
{
    String value;
    public void init() throws ServletException {
        value = getInitParameter("addressfile"); // correct value is saved here
        System.out.println("Init value : "+value);
    }
  DispatchChoice myChoice = new DispatchChoice("/test1", new FileHandler(value));

  public void doGet (HttpServletRequest request, HttpServletResponse response) 
    throws IOException
  {
        myChoice.getHandler.doGet(request, response);
  }
}

class FileHandler implements Handler {
    private String place;
    public FileHandler (String value){
        this.place = value; // this is NULL, not the value from above
        System.out.println("Param value : " + value);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        File directory = new File(place); //is NULL
        File[] files = directory.listFiles();
        PrintWriter pw = response.getWriter();
        for (int index = 0; index < files.length; index++) {
            pw.println(files[index].getName());
        }
    }
}

web.xml

<servlet>
<servlet-name>ListManagerServlet</servlet-name>
<servlet-class>savva.listmanagerservlet.ListManagerServlet</servlet-class>
<init-param>
    <param-name>addressfile</param-name>
    <param-value>d:\\temp\\</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ListManagerServlet</servlet-name>
<url-pattern>/ListManagerServlet</url-pattern>
</servlet-mapping>
4

4 回答 4

2
DispatchChoice myChoice = new DispatchChoice("/test1", new FileHandler(value));

此行在之前执行init(),因此value仍未null分配!相反,将作业移到 内部init(),例如:

DispatchChoice myChoice;

public void init() throws ServletException
{
    value = getInitParameter("addressfile"); // correct value is saved here
    myChoice = new DispatchChoice("/test1", new FileHandler(value));
    System.out.println("Init value : "+value);
}
于 2012-10-13T10:00:50.590 回答
1
  DispatchChoice myChoice = new DispatchChoice("/test1", new FileHandler(value));

在创建实例时,在调用 init() 之前,您正在初始化 myChoice,因此value仍然为空。

初始化它init()

于 2012-10-13T10:01:53.520 回答
1

在您的 servlet 中,您初始化类 menber

 DispatchChoice myChoice = new DispatchChoice("/test1", new FileHandler(value));

在 init() 方法使用 init 参数中的 hte 路径初始化值之前,它为空。

您应该将其实现为

public void init() throws ServletException {
    value = getInitParameter("addressfile"); // correct value is saved here
    if (myChoice == null) {
        myChoice  = new DispatchChoice("/test1", new FileHandler(value))}
    }
    System.out.println("Init value : "+value);
}
DispatchChoice myChoice = null;
于 2012-10-13T10:06:39.517 回答
0

经过一番搜索,我发现使用 getServletContext().get/set Attribute() 比我以前的设计更好。但感谢您解释我的问题。

于 2012-10-13T11:07:54.820 回答