Reputation: 6190
I am trying to create an RSS feed dynamically and store it on server, such that to access the created RSS feed I can just type in the URL from the browser. Is it possible to do so?
I can create the RSS feed, but when I try to save it on server using FileOutputStream("WebContent/filename.xml")
it gives me FileNotFoundException
. Is my approach correct, or is there any other way to store a file on the server and access it using a simple URL?
I am using spring schedulers to run my application every minute to create the RSS feed dynamically.
Upvotes: 2
Views: 15787
Reputation: 16602
Here is my solution in a Servlet method doGet(...). It don't save the RSS in the file system, it will be created every time (when there is a request). I write it out as the HTTP response.
response.setContentType("application/rss+xml");
response.setHeader("Cache-Control", "no-cache"); // HTTP 1.1
response.setHeader("Pragma", "no-cache"); // HTTP 1.0
response.setDateHeader("Expires", 0); // prevents caching at the proxy server
PrintWriter out = response.getWriter();
out.print("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
[...]
out.print("</xml>");
out.flush();
out.close();
// set header information
When its necessary to write it on the filesystem, here is a solution, I used it to save an image on the server, its again in a servlet method:
String absoluteFilesystemPath = getServletContext().getRealPath("/");
System.out.println(absoluteFilesystemPath);
File f = new File(absoluteFilesystemPath+"file.xml")
Upvotes: 0
Reputation: 691865
You're confusing the directory that you're using to store the source files of your project and the directory where the application is deployed on the server.
First define how and where you will deploy the application (if it's as a war file, you won't even have a root directory), and then design your application accordingly.
If you want to stay portable, you should define a configuration parameter (in your web.xml) defining where external files are stored, and have a servlet reading files from this directory and writing their contents in the response output stream.
But you could also configure your server to serve static files from a specific directory and write the XML file to this directory. Read the documentation of your server to know if and how it's possible to do that.
Upvotes: 1
Reputation: 40831
What Justin says is right, but I'd go further and say do you actually need to store it on disk?
If it's not that big of a file, (my bet would be not), then just have the url point to a servlet which returns it from cached memory, and then have that servlet also rebuilds it every minute.
Upvotes: 0
Reputation: 597164
Generally, you don't "generate & store" such things. You serve dynamically & cache them (both http cache (for clients) and internal cache (so that you don't fetch multiple times)). Spring 3.1 cache abstraction would allow that easily.
In order to store files to the server you need to know what the root directory of the webapp is. You can get it by request.getServletContext().getRealPath("/")
. Note that these files will be gone after undeploy, so you might consider storing them in an absolute, outside-app location. Also note that you can't write them in unexploded war archives. And another thing - there is no WebContent
by default - it is the project directly but it does not correspond to a directory in the web app distributable.
Upvotes: 4
Reputation: 4989
My recommendation would be to expose a directory on the server using your HTTP daemon (probably Apache) and then have the application write there. As far as I know, you can't write files inside your WAR/EAR.
Upvotes: 2