Discussions

Web tier: servlets, JSP, Web frameworks: Usage of ServletContext

  1. Usage of ServletContext (5 messages)

    I have to load Properties file from one of my class which is bundled within a war file placed on JBoss.

    Properties prop = new Properties();
    prop.load(new FileInputStream(new File("db.properties")));

    When im writting this piece of code for file loading, its saying FileNotFoundException, even though file is on root path of war file.I think by default server is trying to find file from some other folder.

    Could you please tell me is there any alternative method or is there any way to set the relative path or anything like that.

    Threaded Messages (5)

  2. when you are using this code, server is finding db.properties from JVM_HOME.


    I have to load Properties file from one of my class which is bundled within a war file placed on JBoss.Properties prop = new Properties();prop.load(new FileInputStream(new File("db.properties")));When im writting this piece of code for file loading, its saying FileNotFoundException, even though file is on root path of war file.I think by default server is trying to find file from some other folder.Could you please tell me is there any alternative method or is there any way to set the relative path or anything like that.
  3. Usage of ServletContext[ Go to top ]

    Yeah I know, But what should i do to sort out that thing, Is there any alternative?
  4. server[ Go to top ]

    Hi,

    How about putting it in JBoss server classpath.That should work.

    Ash
  5. You can keep the properties file in a jar file and put it in WEB-INF/lib directory. It should able to find the file from your lib directory.

    or else,

    Keep the file into your WEB-INF/classes directory and refer the path. It should work.

    Enjoy.

    Sankar.
    I have to load Properties file from one of my class which is bundled within a war file placed on JBoss.Properties prop = new Properties();prop.load(new FileInputStream(new File("db.properties")));When im writting this piece of code for file loading, its saying FileNotFoundException, even though file is on root path of war file.I think by default server is trying to find file from some other folder.Could you please tell me is there any alternative method or is there any way to set the relative path or anything like that.
  6. Using ServletContext[ Go to top ]

    I know of atleast two ways to use find the properties file:

    1) put your "props.properties" file in your WAR classpath, i.e. /WEB-INF/classes dir. Then, you can get the "props.properties" using the following syntax:

    InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("props.properties");
           
    2. put your "props.properties" file anywhere in your WAR file (e.g. /WEB-INF directory). Here is the syntax:

    InputStream stream = context.getResourceAsStream("props.properties");