Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to save a file inside an application context?

 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>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
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am curious how you have had 'a ServletContextListener run every so often'? You didn't mean that literally?

ram.
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic