• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can i keep a configFile in webapp?

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to create a config file to keep some settings such that my servlet can access it to retrieve some settings (avoid hardcoding values into the servlet).

I want to try to keep it under WEB-INF (so as to disallow public access). But when i do a io
FileInputStream in = new FileInputStream("configFile");
i keep getting error saying it cannot find the file.

Did anyone do something similar before? what should be the path that i should supply?

I know i can use web.xml to keep settings. But i want to keep a setting for example names whose values may consist of Tom, Dick and Harry. My servlet would read this setting and store the values in an ArrayList. If i use web.xml i can only store one name. Maybe someone can help me out with other alternative solutions?

Thanks in advance.
 
Ranch Hand
Posts: 472
Objective C Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can request resource URL for config file and then open it as File.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi HuiGe,

FileInputStream in = new FileInputStream("configFile");

this will work only if the file is in your current working directory.
i believe the file should be in your WEB-INF/classes folder.

or the other way...
you can specify the real path of the file using..

this.getServletContext().getRealPath("/");

this will return your servlet context path eg.. http://host/contextPath/, where contextPath is the context path of this ServletContext.

then append the file name with the path.. in your case it should be
String fPath = this.getServletContext().getRealPath("/") + "WEB-INF/configFile";

Think experts will come out with more suggestions...

Regards,
Karthik K.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or just use a propertiesfile which you can simply open with something like
 
Ranch Hand
Posts: 1211
Mac IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not use the web.xml file, and store your values as context parameters or servlet initialization parameters?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you go with the ResourceBundle as Jeroen has suggested, just make sure your properties file is in a place where you webapp can see it.
Either put it in the WEB-INF/classes directory or pack it in a jar under the WEB-INF/lib directory.

Another alternative is to use ServletContext.getResourceAsStream(path).
reply
    Bookmark Topic Watch Topic
  • New Topic