wood burning stoves 2.0*
The moose likes Servlets and the fly likes Specifying file location in web.xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Specifying file location in web.xml" Watch "Specifying file location in web.xml" New topic
Author

Specifying file location in web.xml

Michael Cleary
Ranch Hand

Joined: Jul 29, 2003
Posts: 93
Within my servlet I need to read a line of text from a .txt file. How do I specify the location of this file in the deployment descriptor? I've tried using an init-param with the value being the location of the file:
<init-param>
<param-name>Testfile</param-name>
<param-value>/mywebapp/file.txt</param-value>
</init-param>
When I run the servlet, the value of the param is "null".
I tried using "url-pattern" instead of value, but I get the same result.
Obviously, I'm very new to servlets... How can I specify the location in the web.xml file?
Thanks,
Mike


Mike<br />SCJP 1.4<br />----------------------------<br />mdcleary@earthlink.net<br />----------------------------<br />There are 10 types of people<br />in the world. Those that <br />understand binary, and those<br />that don't.
Prakash Dwivedi
Ranch Hand

Joined: Sep 28, 2002
Posts: 452
Hi Michael
you can put that line of text in a property file, and add that property file to your classpath(also you can put that property file in your classes folder.
In case you want to keep it as a text file only(and not property file). You can use getResourceAsStream() method of servlet context(If that text file is located inside your web application).
Also can you provide the code where you are trying to retireve this init param.
[ January 26, 2004: Message edited by: Prakash Dwivedi ]

Prakash Dwivedi (SCJP2, SCWCD, SCBCD)
"Failure is not when you fall down, Its only when you don't get up again"
Michael Cleary
Ranch Hand

Joined: Jul 29, 2003
Posts: 93
Thanks - I like the idea of using getResourceAsStream() myself. But, this is a small part of a class project, and the text has to be "maintained in a file whose location is to be specified as servlet initialization data within the deployment descriptor". Then I have to derive the text from the file and add it to one of the web pages associated w/ my servlet. If I can just somehow specify the absolute path in the web.xml file, I can pass the path to a FileReader, then use a BufferedReader to read in the whole line. Any more suggestions would be greatly appreciated.
Thanks,
Mike
Vijay Rajaram
Greenhorn

Joined: Nov 17, 2003
Posts: 12
Michel,
Your approach of using in init-param should be working, may be the code snippet which tries to get the initialisation parameter might need to be reviewed. So post that code. Mean while, you have a special provision for these kind of requirement in the web.xml. i.e you can use environmental entries in the web.xml. as given below

Then you can use this environment entry anywhere in your application as given below

Hope this would meet your requirement.
- VJ
[ January 26, 2004: Message edited by: Vijay Rajaram ]
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
I think what you may be missing is that you need the full path to the file in the init parameter. Hardly any of the examples I've seen so far look like full paths.
On windows, I would expect to see something like c:\projects\java\mywebapp\file.txt
On Linux/Unix I would expect to see something like /usr/mcc/projects/java/mywebapp/file.txt
Obviously you need to substitute your actual directories, but the point is that the filename should start from the "root" of the filesystem.
Can you tell us where the file you want to read is located?


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Michael Cleary
Ranch Hand

Joined: Jul 29, 2003
Posts: 93
Thanks all! The full path is C:\tomcat4.1.29\webapps\myapp\testfile.txt
I had not tried the <env-entry> before, mainly because I wasn't aware of it...
I had been getting the parameters with the following code which is in my service method:
Enumeration parameters = m_config.getInitParameterNames();
while(parameters.hasMoreElements())
{
String param = (String) parameters.nextElement();
System.out.println("Parameter name: " + param);
System.out.println("Parameter value: " + config.getInitParameter(param));
}
I was only using the println statements so I could see some output in the console.
Thanks again!
Mike
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
I am reading a properties file via web.xml as follows:
web.xml entry:
<init-param>
<param-name>
passwordFile
</param-name>
<param-value>
C:\server directory...\WEB-INF\passwords.properties
</param-value>
</init-param>
my Init:
public void init(ServletConfig config)
throws ServletException {
super.init(config);
try {
passwordFile = config.getInitParameter("passwordFile");
passwords = new Properties();
passwords.load(new FileInputStream(passwordFile));
} catch(IOException ioe) {}
}


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Michael Cleary
Ranch Hand

Joined: Jul 29, 2003
Posts: 93
Thanks Bosun! I used something similar and it works great! Only, I did not retrieve the parameter in the init method, but in the service method. I did this because I wanted to be able to change the text in the text file "on the fly", w/o having to restart the server. If I remember correctly, the init method is only called once. Subsequent calls to the servlet do not call the init method again. Please correct me if I'm wrong...
Thanks again,
Mike
Prakash Dwivedi
Ranch Hand

Joined: Sep 28, 2002
Posts: 452
You are right Michael that init method is called only once in the life cycle of a servlet. But what u r passing in init method is just the name of the text file and not the text itself. Also it appears that u dont have any plan to change the name of the file. So u can still change the text of the file and u dont have to restart the server.
u can retrieve init parameter from the service method as well.
Alternatively u can retrieve this parameter in init() method and save the values as an instance variable.
Michael Cleary
Ranch Hand

Joined: Jul 29, 2003
Posts: 93
Dooh..... Of course, I didn't think about that - I have no need to change the name of the file (or it's location). As long as I read the file in the service method I can just grab the location in init.
Thanks!
Mike
R. Shellbay
Greenhorn

Joined: Sep 19, 2003
Posts: 13
If the file is located within your servlet context, u can use the following approach to achieve the full path
String resource = "smallpath/text.txt"
java.net.URL u = context.getResource(resource);
String fullpath = u.toString();
 
Consider Paul's rocket mass heater.
 
subject: Specifying file location in web.xml