• 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

Specifying file location in web.xml

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 452
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Michael Cleary
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Michael Cleary
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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) {}
}
 
Michael Cleary
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 452
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
 
reply
    Bookmark Topic Watch Topic
  • New Topic