Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to listen to Servlet containers

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "how to monitor Servlet containers", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "how to monitor Servlet containers" this article.

1. Two methods to implement the javax.servlet.ServletContextListener interface: contextInitialized () and contextDestroyed ()

ContextInitialized (): executes when the Servlet container starts

ContextDestroyed (): executes when the Servlet container stops

two。 Add the program that needs to be listened to in contextInitialized (), and the frequency of execution of the listener is controlled by the schedule () method of java.util.Timer

DEMO (this is a prototype of my SMS response monitoring program, which has been streamlined)

ReplyListener.java

Package com.hanweb.jcms; import javax.servlet.*; public class ReplyListener implements ServletContextListener {private ReplyTimer rt = null; public void contextInitialized (ServletContextEvent event) {String status = "[SYS] SMS reply listener start."; event.getServletContext (). Log (status); System.out.println (status); rt = new ReplyTimer (1); rt.start ();} public void contextDestroyed (ServletContextEvent event) {String status = "[SYS] SMS reply listener stop."; event.getServletContext (). Log (status) System.out.println (status); if (rt! = null) {rt.stop ();}

ReplyTimer.java

Package com.hanweb.jcms; import java.util.*; public class ReplyTimer {private final Timer timer = new Timer (); private final int min; public ReplyTimer (int minutes) {min = minutes;} public void start () {Date date = new Date (); timer.schedule (new ReplyTask (), date, min * 60 * 1000);} public void stop () {timer.cancel ();}}

ReplyTask.java

Package com.hanweb.jcms; import java.util.*; public class ReplyTask extends TimerTask {public void doSomething () {System.out.println ("[SYS] SMS reply listener running");} public void run () {doSomething ();}}

Put the compiled class file into WEB-INF/classes, and * Don't forget to add a listening statement to the web.xml of the current WEB application in the Servlet container:

Com.hanweb.jcms.ReplyListener above is all the content of the article "how to listen to Servlet containers". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report