0

HelloWorldServlet.java の doGet メソッドに以下を含む単純な Hello World Servlet を Eclipse で作成しました。


PrintWriter writer = response.getWriter();
String hello = PropertyLoader.bundle.getProperty("hello");
writer.append(hello);
writer.flush();

PropertyLoader は、次のことを行うサーブレットと同じパッケージ内の単純なクラスです。


public class PropertyLoader {
    public static final Properties bundle = new Properties();

    static {
        InputStream stream = null;
        URL url = PropertyLoader.class.getResource("/helloSettings.properties");
        stream = new FileInputStream(url.getFile());
        bundle.load(stream);         
    }
}//End of class

次の 1 行のコンテンツを含む helloSettings.properties というファイルを /WebContent/WEB-IND/classes に配置しました。

hello=Hello Settings World

プロジェクトにTomcat 6.0を追加してEclipseで実行すると、正常に印刷されます

Webブラウザに「Hello Settings World」。

ただし、プロジェクトを war ファイルとしてエクスポートし、手動で .../Tomcat 6.0/webapps に配置すると、結果として「null」が表示されます。

クラスパス/クラスローダーの設定に問題がありますか? 許可?他の構成ファイルはありますか?helloSettings.properties ファイルが WEB-INF/classes フォルダーにあるという事実を知っています。

何か助けはありますか?

4

1 に答える 1

0

さて、多くのブラウジングの後、私がやろうとしていることをする「通常の」理由を見つけました:

代わりに...(私がどのようにやっていたか)

public class PropertyLoader { 

    public static final Properties bundle = new Properties();

    static { 

        InputStream stream = null; 
        URL url = PropertyLoader.class.getResource("/helloSettings.properties"); 
        stream = new FileInputStream(url.getFile()); 
        bundle.load(stream);

    } 

}//End of class

修正

public class PropertyLoader { 

    public static final Properties bundle = new Properties();

    static { 

        InputStream stream = null;
        stream = SBOConstants.class.getResourceAsStream("/sbonline.properties");
        bundle.load(stream);

    } 

}//End of class

私は他の誰かのコードを変更しているので、そもそもなぜ彼らが別の方法でそれを行ったのかわかりません...しかしurl.getFile()、私の問題だと思いますが、理由はわかりません。

于 2010-08-08T23:27:57.613 に答える