• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can i keep a configFile in webapp?

 
Hui Ge
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
D Rog
Ranch Hand
Posts: 472
Linux Objective C Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can request resource URL for config file and then open it as File.
 
Karthik Kannan
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or just use a propertiesfile which you can simply open with something like
 
Sonny Gill
Ranch Hand
Posts: 1211
IntelliJ IDE Mac
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not use the web.xml file, and store your values as context parameters or servlet initialization parameters?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • 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).
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic