0

in order to write to a text file in JSP where should the file be created in NETBEANS web application project.

rose petal
  • 21
  • 1
  • 6

1 Answers1

1

Store it in a fixed path outside the IDE/server/webapp. E.g. C:\var\webapp\data. You can just write to it using FileOutputStream like so

File file = new File("C:/var/webapp/data", "filename.txt");
OutputStream output = new FileOutputStream(file);

The fixed path can in turn be made configureable by a configuration/properties file which is in turn placed in the classpath. See also Where to place and how to read configuration resource files in servlet based application?

String dataPath = properties.getProperty("data.path");
File file = new File(dataPath, "filename.txt");
OutputStream output = new FileOutputStream(file);

Don't store it in the Netbeans' folder structure, simply because that doesn't exist at all when deployed and running at production environment or even when moved to a different IDE. It makes your webapp totally unportable. Don't store it in the expanded webapp's folder structure (with WEB-INF and all on em, for which one would have used ServletContext#getRealPath() to convert it to a disk file system path), simply because every change in that folder structure would completely disappear once the WAR get redeployed or, in certain configurations, even when the server get restarted, simply because the created/edited text file is not part of the original WAR.

A completely different alternative is to use an (embedded) SQL database. This may be a much better option when you frequently need to manipulate the data or need to search in it. A SQL database is absolutely highly optimized for this job.

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452