0

ディレクトリ内のファイルを一覧表示する簡単なJavaサーブレットを作成しようとしています。パスは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を初期化しているため、valuenullのままです。

で初期化init()

于 2012-10-13T10:01:53.520 に答える
1

サーブレットで、クラスメンバーを初期化します

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

init()メソッドの前に、initパラメーターからのhteパスを使用して値を初期化するため、nullになります。

次のように実装する必要があります

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 に答える