How to run JSP code every 15mins once without running it. I want to execute a jsp program code periodically every 15mins once.
Java code doesn't belong in a JSP file. Just move that code into a real Java class. This way you can use ScheduledExecutorService
in a ServletContextListener
to execute it periodically.
@WebListener
public class Config implements ServletContextListener {
private ScheduledExecutorService scheduler;
@Override
public void contextInitialized(ServletContextEvent event) {
scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new Task(), 0, 15, TimeUnit.MINUTES);
}
@Override
public void contextDestroyed(ServletContextEvent event) {
scheduler.shutdownNow();
}
}
Where Task
class implements Runnable
.
public class Task implements Runnable {
public void run() {
// Do your job here.
}
}
Or if your Java EE container is capable of this, use the container-provided job scheduling capabilities. The detailed answer depends on the container which you're using.
You can write a JAVA code which will trigger the JSP code periodically.If you have interest in this solution I can help you to do this (giving the sample code).
精彩评论