problems using property files within servlets.

From: hernan rancati (hernan.rancati_at_gmail.com)
Date: 02/25/05


Date: 25 Feb 2005 10:17:06 -0800

Hi people, I'm trying to build a counter in JSP that stores counter
values in a property file. But I'm facing a problem, I can't open
that file due to a FileNotFoundException. This is caused because
the current directory is c:\windows\system32 instead of my project's
home (system32 is where tomcat's VM is started.)

I need access to read (to get counter values) and store (to store new
counter
values) that property file.

Well, if anyone has some clue to solve this, let me know

 thanks to everyone, cesare.

-----------------------8<------------------------------------------------------
Here is the section that produces the Exception:
   properties=new Properties();
   try {
      System.out.println("Counter:loading properties");
      //properties.load(getClass().getResourceAsStream(propertiesName));
      properties.load(new FileInputStream(propertiesName));
   } catch (FileNotFoundException e) {
      ...
   }
-----------------------8<------------------------------------------------------
And here is the full source:

public class Counter {
        protected static String propertiesName="counters.properties";
        protected int counter=0;
        protected static Properties properties;
        protected String name;
        
        public Counter(String name) {
                this.name=name;
                System.out.println("path:"+new
File(propertiesName).getAbsolutePath());
                loadProperties();
                setCounter();
        }
        
        public void visit() {
                ++counter;
                properties.setProperty(name,""+counter);
        }
        
        public void store() {
                storeProperties();
        }
        
        private void setCounter() {
                String propertyValue=properties.getProperty(name);
                if (propertyValue!=null && propertyValue!="")
                        counter=Integer.parseInt(propertyValue);
        }
        
        private void storeProperties() {
                try {
                        properties.store(new
FileOutputStream(propertiesName),"this are the counters");
                } catch (FileNotFoundException e) {
                        System.out.println("Counter: no se encontró el archivo de
propiedades");
                } catch (IOException e) {
                        System.out.println("Counter: error al intentar escribir el archivo
de propiedades");
                }
        }
        
        private void loadProperties() {
                if (properties!=null) return;
                properties=new Properties();
                try {
                        System.out.println("Counter:loading properties");
                        //properties.load(getClass().getResourceAsStream(propertiesName));
                        properties.load(new FileInputStream(propertiesName));
                } catch (FileNotFoundException e) {
                        System.out.println("Counter: no se encontró el archivo de
propiedades");
                } catch (IOException e) {
                        System.out.println("Counter: error al intentar leer el archivo de
propiedades");
                }
        }
        
        public String toString() {
                return ""+counter;
        }
}