• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to run a jsp continuously without loading it?  RSS feed

 
Dhanunjaya routu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have a jsp file. it contains the code related to database.
It should run for every 15 mins and it will run in background without load the page.
Can any one help me?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. JSPs are used for rendering textual content (like HTML), not for any kind of processing. What you need is a background thread; check out the java.util.Timer and TimerTask classes for that. Those can be set up in a ServletContextListener.
 
Dhanunjaya routu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:No. JSPs are used for rendering textual content (like HTML), not for any kind of processing. What you need is a background thread; check out the java.util.Timer and TimerTask classes for that. Those can be set up in a ServletContextListener.



It may either jsp or servlet.
How can i run continuously using listener?




 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should move that code out of a JSP, since it doesn't sound like its anything to do with display. Have a look at the classes Ulf has recommended, they will give you the scheduled behaviour you want, and if you start them (as suggested) from a ServletContextListener the will begin when your web application does.

Make sense?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It may either jsp or servlet.

Not sure what you're trying to say. The code that runs repeatedly will be neither a JSP nor a servlet - it'll need to implement the Runnable interface in some form (like TimerTask does). Have you checked out the Timer and TimerTask classes yet?

How can i run continuously using listener?

The context listener is where you set up background jobs, not where those jobs actually run. Are you familiar with servlet context listeners? If not, I recommend you read up on that concept, it's very useful.
 
Arthanari Eswar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dhanunjaya routu wrote:
It may either jsp or servlet.
How can i run continuously using listener?


It is possible to run JSP program continuously every 15mins once. But the recommended code is not related to Java. Its based on HTML tags. Check with meta tag with meta name called "refresh".
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arthanari Eswar wrote:It is possible to run JSP program continuously every 15mins once. But the recommended code is not related to Java. Its based on HTML tags. Check with meta tag with meta name called "refresh".

That relies on a browser continuously displaying the page - not a viable solution.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It is possible to run JSP program continuously every 15mins once. But the recommended code is not related to Java. Its based on HTML tags. Check with meta tag with meta name called "refresh".

Though of course this will stop when there are no clients connected. Kill the browser and the refresh stops.
 
Dhanunjaya routu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your support.
I have solved that using ContextListener and timer service.
But i have one doubt can we establish a timer service in servlet?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can we establish a timer service in servlet?

You can, and it may make sense if the purpose of the timer is closely related to the servlet processing. But for something that runs perpetually, a context listener is the place to handle it. By the way, don't forget to shut down the Timer in the contextDestroyed method.
 
sudhir nim
Ranch Hand
Posts: 212
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To me, it looks like that you have some database code in a jsp file that needs to run every 15 minutes. Instead of going into any scheduler headache, I would suggest you look at the crontab . If you are deploying on a linux platform, create a cron job that would call your jsp/or whatever other URL every 15 minutes.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are deploying on a linux platform, create a cron job that would call your jsp/or whatever other URL every 15 minutes.

No, don't do that. Using cron for this is fine, but using it to invoke a JSP is not. Once and for all: JSP pages are not for processing, they're a templating solution for creating textual output. The cron job can run a standalone Java application instead.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!