views:

15

answers:

1

I wrote a simple Hello World Servlet in Eclipse containing the following in the doGet method of my HelloWorldServlet.java


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

PropertyLoader is a simple class in the same package as the Servlet that does the following:


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

I placed a file called helloSettings.properties in /WebContent/WEB-IND/classes that contains the following single line of content:

hello=Hello Settings World

When I add Tomcat 6.0 to my project and run it in eclipse it successfully prints

"Hello Settings World" to the web browser.

However when I export the project as a war file and manually place it in .../Tomcat 6.0/webapps I then get "null" as my result.

Is it a problem with the classpath/classloader configuration? permissions? any of the other configuration files? I know for a fact that the helloSettings.properties file is in the WEB-INF/classes folder.

Any help?

A: 

Well, after much browsing I found what seems a "normal" why to do what I'm trying to do:

Instead of...(how I was doing it)

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

THE FIX

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

I'm modifiying someone else's code so I'm not sure why they did it the other way in the first place... but I guess url.getFile() was my problem and I don't know why.

sonrael