Java Servlets - Write to file

I am using Netbeans IDE and currently I am using GlassFish server. What I want to do is write to a file .

I looked through several pages, and the code that I have (which doesn't work as far as I know) looks like this:

File outputFile = new File(getServletContext().getRealPath("/") + "TheFile.txt"); FileWriter fout = new FileWriter(outputFile); fout.write("The Content"); fout.close(); 

This is my project structure:

alt text

And where will the file be placed?


Edit: I forgot to mention that there are several other folders below those in the picture: test packages, libraries, test libraries, and configuration files. However, I do not think the file will be posted there.

Edit (newest): I found out that the file is stored in the / build / web folder, but this does not appear in Netbeans. Even after I restarted it.

+4
source share
3 answers

As you encoded, the file will be hosted on a public web root. What getRealPath("/") points to. To be precise, this is a folder called Web Pages, as in your screenshot. As an exercise, do the following to understand the absolute path so that it can be found in the OS Explorer.

 System.out.println(file.getAbsolutePath()); 

I do not do Netbeans, but probably you need to update the folder in your IDE after writing the file so that it appears in the list in the IDE. Click the folder and press F5. This is at least true for Eclipse.

However, this approach is not recommended. This will not work if the servlet container is not configured to expand the WAR on the disk. Even when this happens, you will lose all new files and changes to existing files when the WAR is redistributed. It cannot be used as permanent storage. Rather, save it on a fixed path outside of the webapp or in the database (which is preferable since you seem to want to invent CMS).

+4
source

Please note that this is in no way guaranteed to work in all web containers or restarts and will most likely be overwritten by redistribution.

If you want your user to be able to update the content, you need to save the new content somewhere and have a servlet or JSP page, or facelet to get new content from the backup storage and send it to the browser.

+4
source

See the documentation for getRealPath . It returns you the disk location for something specified by the url.

I assume that your file is located at the root of your web application on disk in Glassfish (where the WAR file is extracted). I don’t know enough about Glassfish to say where it will be.

Also note that you use string concatenation to create a file name, so if the call to getRealPath does not return a string with "/" at the end, you can create a file in the parent directory of your web application. Perhaps it is best to use the File object for the parent directory when creating the File object for the actual file. Check out the API files.

I would recommend creating a file outside of your web application. If you redeploy your WAR file, you can delete your file, which is probably not the one you need.

Being in a servlet has little effect on what you want to output the file. Just follow the standard file APIs as a starting point. Here is a tutorial.

+2
source

All Articles