This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Servlets and the fly likes servlets and text file data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlets and text file data" Watch "servlets and text file data" New topic

servlets and text file data

Sri Sar
Ranch Hand

Joined: Mar 25, 2004
Posts: 44
i have two text files (abcd.txt and efgh.txt). text file abcd.txt contains names of districts in a country with their area values whereas the file efgh.txt contains the names of the same districts but with their population values. (the text files have only two columns: name of the district and either the area/population values)
now i want to write a servlet that extracts the 'area' values from abcd.txt and 'population' values from efgh.txt and calculates Density ie divides the area values by the population values and obtain the result.
since i am new to Servlets, i am in desperate need of some sort of sample code in proceeding with the above functionality.
thanks in advance.
Jon Entwistle
Ranch Hand

Joined: Feb 20, 2003
Posts: 118

A good way to procedd is as follows. To load a file called 'conversion.dat' for the sake of argument, with a path of WEB-INF/currency relative to your application root, put the following in your web.xml (for tomcat at least!) file straight after your opening <webapp> label:

Now from within your servlet, create a File instance using this path:

Note the getServletContext() method must be called from within a servlet.

You can then treat this file in the normal way.


[ May 25, 2004: Message edited by: Jon Entwistle ]

I agree. Here's the link:
subject: servlets and text file data
It's not a secret anymore!