Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2005

Tip: Looking for answers? Try searching our database.

problems using property files within servlets.

Thread view: 
hernan rancati - 25 Feb 2005 18:16 GMT
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;
    }
}
Oscar kind - 25 Feb 2005 21:12 GMT
> 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
[quoted text clipped - 5 lines]
> counter
> values) that property file.

A deployed web application (what JSP pages are a part of) cannot assume
there is a file system. In that regard, you'd have to use a database.

This is somewhat complex for just a counter. But if you are willing to
make demands on the server, you have another possibility:

If you are willing to sacrifice some security, you can let the server
allow the web application to write to its own webapp directory. IIRC,
Tomcat allows this by default, but check this with the docs to be certain.

If the server allows access to the directory of the web application, you
can get a directory to write to whose contents are also in the classpath:

   <% String directoryName = config.getRealPath("/WEB-INF/classes"); %>

NOTE however, that this method can will return null if the path cannot be
translated (for example when you use a .war file). In that case, what you
are trying to do is not possible.

It is therefore preferable if you find that another solution is feasible.
For the current counter example this is doubtful, but if you wish to store
more information a database is the way to go.

Signature

Oscar Kind                                    http://home.hccnet.nl/okind/
Software Developer                    for contact information, see website

PGP Key fingerprint:    91F3 6C72 F465 5E98 C246  61D9 2C32 8E24 097B B4E2

hernan rancati - 26 Feb 2005 04:36 GMT
Hi Oscar, thank you for your wise response:).

So, I can't rely on filesystem, what a life. I will go on the
database direction thatyou mentioned. But, do you know how struts
framework access it's xml files like struts-config.xml or the
ResourceBundle.properties? They access that files using
config.getRealPath()??

I'm a little confused. anyway, thanks..

Hernan Rancati.

Oscar kind <oscar@danwa.net> wrote in message
> A deployed web application (what JSP pages are a part of) cannot assume
> there is a file system. In that regard, you'd have to use a database.
[quoted text clipped - 18 lines]
> For the current counter example this is doubtful, but if you wish to store
> more information a database is the way to go.
Oscar kind - 26 Feb 2005 10:57 GMT
> So, I can't rely on filesystem, what a life. I will go on the
> database direction thatyou mentioned. But, do you know how struts
> framework access it's xml files like struts-config.xml or the
> ResourceBundle.properties? They access that files using
> config.getRealPath()??

Reading files is easier. You still cannot depend on a file system, but you
don't have to: there is the classpath. Every Class and every instance of
it is loaded by a ClassLoader, which gets its classes and resources
(read: files, sort of) from a classpath. All you know is that you can ask
it for classes and resources: You don't really know how it gets them (read
from a file, read from a .jar file, created on the spot, ...).

To get a resource from the classpath, you can use
Class#getResource(String) and Class#getResourceAsStream(String).

To get a file from the root of the web application, you can use
ServletContext#getResource(String) and
ServletContext#getResourceAsStream(String).

Signature

Oscar Kind                                    http://home.hccnet.nl/okind/
Software Developer                    for contact information, see website

PGP Key fingerprint:    91F3 6C72 F465 5E98 C246  61D9 2C32 8E24 097B B4E2

hernan rancati - 26 Feb 2005 20:08 GMT
Oscar, I didn't know that there is ServletContext#getResourceAsStream(), still
doesn't work for my counter but is nice to know.

thanks!

Hernan Rancati (cesare)

> Reading files is easier. You still cannot depend on a file system, but you
> don't have to: there is the classpath. Every Class and every instance of
[quoted text clipped - 9 lines]
> ServletContext#getResource(String) and
> ServletContext#getResourceAsStream(String).

t


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.