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

What is the scope of Servlet Context

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

Share

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

This article introduces the relevant knowledge of "what is the scope of Servlet Context". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Servlet Context Listener:

Monitor an application globally. There are two main ways to start as the application starts and disappear as the application disappears:

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 done with the startup of the application, that is, many people say that I want to do it when the container is started, generally initializing "global variables", such as

Public void contextInitialized (ServletContextEvent event) {ServletContex sc = event.getServletContext (); sc.setAttribute (name,value);}

In the future, you can getServlet Context (). GetAttribute (name) in any servlet; my favorite thing to use for guardian work is to implement a Timer in the contextInitialized (Servlet ContextEvent event) method, and then let the application make the Timer work every time it starts:

Program code:

Public void contextInitialized (ServletContextEvent event) {timer = new Timer (); timer.schedule (new TimerTask () {public void run () {/ / do any things}}, 0, interval);}

Some people say that Timer can only specify how long from now on, how often or at what time do I want to do a job on the 1st of each month or 12:00 every day?

All you have to do is set an interval, and then you can judge whether it is that time period every time. For example, if you do it on the first day of each month, then your time interval is set to day, that is, a cycle of 24 hours, and then you can judge the date new Date (). GetDate () = = 1 in the run method. If it's 12:00 every day, set the interval to hours, then judge new Date (). GetHour () = = 12 in run, and then do something.

Servlet Context AttributeListener:

This listener mainly listens for events in setAttribute () and removeAttribute () of Servlet Context objects, that is, when a "global variable" is subjected to Add (* secondary set), replace (reassign existing variables) and remove.

This is the end of the content of "what is the scope of Servlet Context". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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