wood burning stoves 2.0*
The moose likes Servlets and the fly likes How to save a file inside an application context? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to save a file inside an application context?" Watch "How to save a file inside an application context?" New topic
Author

How to save a file inside an application context?

Bai Shen
Ranch Hand

Joined: Sep 24, 2008
Posts: 323
I have some content that needs to be dynamically generated. Normally I would use a servlet and be done with it. But I don't want to waste processing power generating the content every time there's a request. The content won't change that often. So what I'd like to do is have a ServletContextListener run every so often and generate the content. It would then save this generated content out to a file.

The problem I'm having is how to place the file inside the application context. I can specify paths related to the root directory, but I can't seem to find anything similar to the application context. For example, I want to save the file so that the user can access it from http://myserver/Application/file.html no matter if I'm running the program on my dev tomcat inside eclipse or on the production server where I've deployed the war file.

TIA.
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

>So what I'd like to do is have a ServletContextListener run every so often and generate the content.

The Listener runs only once - when the context is loaded (the web app is deployed). So that's a no-go.

What you can do is start a thread from within the context and have this thread run once in 'X' mins (where 'X' is a configurable value read from a property file). Every time the thread runs, it generates the said file. Pass the context object itself to the thread so that it has access to the Servlet Context (paths etc).
Note the thread wouldnt have access to any information which the context doesnt have (user request/session etc).

In the destroy method of the context listener, terminate the thread gracefully.

ram.
Bai Shen
Ranch Hand

Joined: Sep 24, 2008
Posts: 323
ramprasad madathil wrote:>So what I'd like to do is have a ServletContextListener run every so often and generate the content.

The Listener runs only once - when the context is loaded (the web app is deployed). So that's a no-go.

What you can do is start a thread from within the context and have this thread run once in 'X' mins (where 'X' is a configurable value read from a property file). Every time the thread runs, it generates the said file. Pass the context object itself to the thread so that it has access to the Servlet Context (paths etc).
Note the thread wouldnt have access to any information which the context doesnt have (user request/session etc).

In the destroy method of the context listener, terminate the thread gracefully.

ram.


You completely avoided my question and answered one I didn't even ask and already have a solution for.

I already have everything set up for the file generation. The problem I'm having is how to set the file path consistently to the same place inside the application context.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60789
    
  65

Unless you are running out of an unexpanded war file (in which case things are read-only), you can use ServletContext.getRealPath() to find the context root.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Bai Shen wrote:The problem I'm having is how to place the file inside the application context.


I'm not sure that what you want is really a great idea. Usually all the files are contained in the war, and addressed relatively.

A simple solution is to write a servlet that caches the results you want to send. Or stores them in some directory that
is likely to be there, say /tmp/

Have the servlet look for the file, if its not there, generate it. If its there, check the creation time, and if stale, regenerate it.
Then just open an input stream to the file, and write it's contents to the servlet output.
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

I am curious how you have had 'a ServletContextListener run every so often'? You didn't mean that literally?

ram.
Bai Shen
Ranch Hand

Joined: Sep 24, 2008
Posts: 323
Bear Bibeault wrote:Unless you are running out of an unexpanded war file (in which case things are read-only), you can use ServletContext.getRealPath() to find the context root.


That's exactly what I was looking for. Thanks!
Bai Shen
Ranch Hand

Joined: Sep 24, 2008
Posts: 323
Pat Farrell wrote:
Bai Shen wrote:The problem I'm having is how to place the file inside the application context.


I'm not sure that what you want is really a great idea. Usually all the files are contained in the war, and addressed relatively.

A simple solution is to write a servlet that caches the results you want to send. Or stores them in some directory that
is likely to be there, say /tmp/

Have the servlet look for the file, if its not there, generate it. If its there, check the creation time, and if stale, regenerate it.
Then just open an input stream to the file, and write it's contents to the servlet output.


Um, that's what I'm doing. I just didn't have a method of finding that file.
Bai Shen
Ranch Hand

Joined: Sep 24, 2008
Posts: 323
ramprasad madathil wrote:I am curious how you have had 'a ServletContextListener run every so often'? You didn't mean that literally?

ram.


Ramen Chatterjee
Ranch Hand

Joined: Apr 27, 2006
Posts: 62
I know this has been resolved, but you may also wish to consider using a scheduling framework like Quartz to generate your content.

Regards

Ramen


Could try harder
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to save a file inside an application context?
 
Similar Threads
File Open/Save Operations in Swing Editor
Cannot open PDF directly, save works
creating a batch file for a java/j2ee application
Including html file outside web project
PDF file read streaming