Servlet Context range

Source: Internet
Author: User

Servlet Context Listener:

Global monitoring of an application is enabled as the application starts and disappears as the application disappears. There are two main methods:
ContextDestroyed (Servlet Context Event) is called when the application is closed
ContextInitialized (Servlet Context Event) is called when the application starts.

This listener is mainly used for some work to be completed as the application starts, that is, what many people say I want to do during the container and startup. Generally, it initializes the "global variables", such

 
 
  1. public void contextInitialized(ServletContextEvent event){  
  2. ServletContex sc = event.getServletContext();  
  3. sc.setAttribute(name,value);  

In the future, you can get servlet Context () in any servlet (). getAttribute (name); I like to use it for daemon, that is, to implement a Timer in the contextInitialized (Servlet ContextEvent event) method, then let the application make the Timer work at each startup:

Program code:

 
 
  1. Public void contextInitialized (ServletContextEvent event ){
  2. Timer=NewTimer ();
  3. Timer. schedule (new TimerTask (){
  4. Public void run (){
  5. // Do any things
  6. }
  7. }, 0, time interval );
  8. }

Some people say that Timer can only specify how long it takes to do things or when it takes to do things from now on, so how do I do a job on the first day of every month or at 12 o'clock every day?

You only need to set an interval and then judge whether the time period is the same. For example, if you do this on the first day of every month, you can set the interval to a day, that is, a cycle of 24 hours, then, judge the Date new Date () in the run method (). getDate () = 1. if it is 12 o'clock every day, set the time interval to hour, and then judge new Date () in run (). getHour () = 12. Just do something again.

Servlet Context AttributeListener:

This listener monitors events of Servlet Context objects in setAttribute () and removeAttribute (). Note that a "global variable" is being added (set for the first time ), replace (re-assign values to existing variables) and remove.

  1. Servlet sharing links in sessions
  2. Configure the Servlet Development Environment
  3. Future Response Servlet features
  4. Tomcat Java Servlet Engine Security Settings
  5. Jetty-Additional Servlet container Functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.