File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Can i keep a configFile in webapp? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Can i keep a configFile in webapp?" Watch "Can i keep a configFile in webapp?" New topic
Author

Can i keep a configFile in webapp?

Hui Ge
Greenhorn

Joined: Mar 11, 2005
Posts: 25
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

Joined: Feb 07, 2004
Posts: 472

You can request resource URL for config file and then open it as File.


Retire your iPod and start with HD Android music player Kamerton | Minimal J2EE container is here | Light weight full J2EE stack | and build tool | Co-author of "Windows programming in Turbo Pascal"
Karthik Kannan
Greenhorn

Joined: Dec 29, 2004
Posts: 12
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

Joined: Oct 12, 2000
Posts: 5093
Or just use a propertiesfile which you can simply open with something like


42
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

Why not use the web.xml file, and store your values as context parameters or servlet initialization parameters?


The future is here. It's just not evenly distributed yet. - William Gibson
Consultant @ Xebia. Sonny Gill Tweets
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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).


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can i keep a configFile in webapp?