How to listen to Servlet containers

Source: Internet
Author: User

1. Two methods for implementing the javax. servlet. ServletContextListener interface: contextInitialized () and contextDestroyed ()

ContextInitialized (): executed when the Servlet container starts
ContextDestroyed (): executed when the Servlet container is stopped

2. Add the program to be listened to in contextInitialized (), and control the execution frequency of the listener by using the schedule () method of java. util. Timer.

DEMO: This is a prototype of my text message reply listener. It is simplified)

ReplyListener. java

 
 
  1. package com.hanweb.jcms;  
  2.  
  3. import javax.servlet.*;  
  4.  
  5. public class ReplyListener implements ServletContextListener {  
  6. private ReplyTimer rt = null;  
  7. public void contextInitialized(ServletContextEvent event) {  
  8. String status = "[SYS] SMS reply listener start .";  
  9. event.getServletContext().log(status);  
  10. System.out.println(status);  
  11.  
  12. rt = new ReplyTimer(1);  
  13. rt.start();  
  14. }  
  15.  
  16. public void contextDestroyed(ServletContextEvent event) {  
  17. String status = "[SYS] SMS reply listener stop .";  
  18. event.getServletContext().log(status);  
  19. System.out.println(status);  
  20.  
  21. if (rt != null) {  
  22. rt.stop();  
  23. }  
  24. }  

ReplyTimer. java

 
 
  1. package com.hanweb.jcms;  
  2.  
  3. import java.util.*;  
  4.  
  5. public class ReplyTimer {  
  6. private final Timer timer = new Timer();  
  7. private final int min;  
  8.  
  9. public ReplyTimer(int minutes) {  
  10. min = minutes;  
  11. }  
  12.  
  13. public void start() {  
  14. Date date = new Date();  
  15. timer.schedule(new ReplyTask(), date, min * 60 * 1000);  
  16. }  
  17.  
  18. public void stop() {  
  19. timer.cancel();  
  20. }  

ReplyTask. java

 
 
  1. package com.hanweb.jcms;  
  2.  
  3. import java.util.*;  
  4.  
  5. public class ReplyTask extends TimerTask {  
  6. public void doSomething() {  
  7. System.out.println("[SYS] SMS reply listener running ");  
  8. }  
  9.  
  10. public void run() {  
  11. doSomething();  
  12. }  

Put the compiled class file into the WEB-INF/classes, and finally do not forget to add the listening statement to the WEB. xml of the current web application in the Servlet container:

 
 
  1. <listener> 
  2. <listener-class>com.hanweb.jcms.ReplyListener</listener-class> 
  3. </listener> 
  1. How to install Apache Servlet
  2. Optimize Servlet configuration for web. xml slimming
  3. How to configure Servlet in XML
  4. Solve the Problem of Servlet JSP page garbled
  5. Servlet and JSP security issues

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.