Other JSP articles:
Use the automatic Bean property filling mechanism in JSP
JSP: list server environment variables
Implementation Mechanism and Application of abnormal turning of JSP errorPage commands to error pages
Jsp uses the 404 error page to rewrite the URL
Sometimes you need to regularly execute some programs during JSP running, such as collecting traffic and updating cache data. Generally, Listener and Timer are used. The following is a simple application, the purpose is to check whether the program is being executed at any time.
Create a TimerTask
Public class LogTask extends TimerTask {
@ Override
Public void run (){
// TODO Auto-generated method stub
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss ");
System. out. println (sdf. format (new Date () + "-- this programe is running! ");
}
}
Create a listener
Public class LogListener implements ServletContextListener {
Private Timer timer = null;
@ Override
Public void contextDestroyed (ServletContextEvent arg0 ){
// TODO Auto-generated method stub
If (timer! = Null)
{
Timer. cancel ();
}
}
@ Override
Public void contextInitialized (ServletContextEvent arg0 ){
// TODO Auto-generated method stub
If (timer = null)
{
Timer = new Timer ();
Timer. schedule (new LogTask (), new Date (), 1000 );
}
}
}
Configure in web. xml
<Listener>
<Listener-class> org. heroking. web. LogListener </listener-class>
</Listener>