📄 java 实现自动监听并更新配置文件内容.mht
字号:
style=3D"COLOR: =
#000000">;<BR> event.getSe=
rvletContext().log(status);<BR> =
System.out.println(status);<BR> =
<BR> </SPAN><S=
PAN=20
style=3D"COLOR: #008000">//</SPAN><SPAN=20
style=3D"COLOR: =
#008000"> =E5=81=9C=E6=AD=A2=E8=87=AA=E5=8A=A8=E7=9B=91=E5=90=AC=E6=97=
=B6=E9=92=9F</SPAN><SPAN=20
style=3D"COLOR: #008000"><BR></SPAN><SPAN=20
style=3D"COLOR: =
#000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">if</SPAN><SPAN=20
style=3D"COLOR: #000000"> (rt </SPAN><SPAN=20
style=3D"COLOR: #000000">!=3D</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">null</SPAN><SPAN=20
style=3D"COLOR: =
#000000">) {<BR> &nbs=
p; rt.stop();<BR> &nb=
sp; }<BR> }<BR>}<BR></SPAN></DIV><BR>=E8=87=AA=
=E5=8A=A8=E7=9B=91=E5=90=AC=E6=97=B6=E9=92=9F=20
PropertiesTimer<BR><BR>
<DIV=20
style=3D"BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; =
BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; =
PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; =
WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px =
solid; BACKGROUND-COLOR: #eeeeee"><SPAN=20
style=3D"COLOR: #0000ff">package</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> org.stephencat.listener;<BR></SPAN><SPAN=20
style=3D"COLOR: #0000ff">import</SPAN><SPAN=20
style=3D"COLOR: #000000"> java.util.</SPAN><SPAN=20
style=3D"COLOR: #000000">*</SPAN><SPAN=20
style=3D"COLOR: #000000">;<BR></SPAN><SPAN=20
style=3D"COLOR: #0000ff">import</SPAN><SPAN=20
style=3D"COLOR: #000000"> javax.servlet.</SPAN><SPAN=20
style=3D"COLOR: #000000">*</SPAN><SPAN=20
style=3D"COLOR: #000000">;<BR><BR><BR></SPAN><SPAN=20
style=3D"COLOR: #008000">/**</SPAN><SPAN=20
style=3D"COLOR: =
#008000"><BR> * =E8=87=AA=E5=8A=A8=E7=9B=91=E5=90=AC=E6=97=B6=E9=
=92=9F<BR> * </SPAN><SPAN=20
style=3D"COLOR: #808080">@author</SPAN><SPAN=20
style=3D"COLOR: =
#008000"> stephen<BR> *<BR> </SPAN><SPAN=20
style=3D"COLOR: #008000">*/</SPAN><SPAN=20
style=3D"COLOR: #000000"><BR></SPAN><SPAN=20
style=3D"COLOR: #0000ff">public</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">class</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> PropertiesTimer {<BR> <BR>&nbs=
p; </SPAN><SPAN=20
style=3D"COLOR: #0000ff">private</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">final</SPAN><SPAN=20
style=3D"COLOR: #000000"> Timer timer </SPAN><SPAN=20
style=3D"COLOR: #000000">=3D</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">new</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> Timer();<BR> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">private</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">final</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">int</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> sec;<BR> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">private</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> ServletContext context </SPAN><SPAN=20
style=3D"COLOR: #000000">=3D</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">null</SPAN><SPAN=20
style=3D"COLOR: =
#000000">;<BR> <BR> </SPAN>=
<SPAN=20
style=3D"COLOR: #0000ff">public</SPAN><SPAN=20
style=3D"COLOR: #000000"> PropertiesTimer(</SPAN><SPAN=20
style=3D"COLOR: #0000ff">int</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> seconds, ServletContext context) {<BR> =
; sec </SPAN><SPAN=20
style=3D"COLOR: #000000">=3D</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> seconds;<BR> &nbs=
p;</SPAN><SPAN=20
style=3D"COLOR: #0000ff">this</SPAN><SPAN=20
style=3D"COLOR: #000000">.context </SPAN><SPAN=20
style=3D"COLOR: #000000">=3D</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> context;<BR> }<BR>  =
; <BR> </SPAN><SPAN=20
style=3D"COLOR: #008000">/**</SPAN><SPAN=20
style=3D"COLOR: =
#008000"><BR> * =E5=90=AF=E5=8A=A8=E8=87=
=AA=E5=8A=A8=E7=9B=91=E5=90=AC=E4=BB=BB=E5=8A=A1<BR> &nb=
sp; </SPAN><SPAN=20
style=3D"COLOR: #008000">*/</SPAN><SPAN=20
style=3D"COLOR: #000000"><BR> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">public</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">void</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> start() {<BR> &nbs=
p; <BR> </SPAN><SPAN =
style=3D"COLOR: #008000">//</SPAN><SPAN=20
style=3D"COLOR: =
#008000"> =E5=8F=96=E5=BE=97=E5=BD=93=E5=89=8D=E6=97=A5=E6=9C=9F=E6=97=
=B6=E9=97=B4</SPAN><SPAN=20
style=3D"COLOR: #008000"><BR></SPAN><SPAN=20
style=3D"COLOR: =
#000000"> Date date&n=
bsp;</SPAN><SPAN=20
style=3D"COLOR: #000000">=3D</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">new</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> Date();<BR>  =
;<BR> </SPAN><SPAN=20
style=3D"COLOR: #008000">//</SPAN><SPAN=20
style=3D"COLOR: =
#008000"> =E6=89=A7=E8=A1=8C=E8=87=AA=E5=8A=A8=E7=9B=91=E5=90=AC=E8=AE=
=A1=E5=88=92</SPAN><SPAN=20
style=3D"COLOR: #008000"><BR></SPAN><SPAN=20
style=3D"COLOR: =
#000000"> timer.schedule(<=
/SPAN><SPAN=20
style=3D"COLOR: #0000ff">new</SPAN><SPAN=20
style=3D"COLOR: #000000"> PropertiesTask(</SPAN><SPAN=20
style=3D"COLOR: #0000ff">this</SPAN><SPAN=20
style=3D"COLOR: =
#000000">.context), date, sec </SPAN><SPAN=20
style=3D"COLOR: #000000">*</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #000000">1000</SPAN><SPAN=20
style=3D"COLOR: =
#000000">);<BR> }<BR> <BR>&=
nbsp; </SPAN><SPAN=20
style=3D"COLOR: #008000">/**</SPAN><SPAN=20
style=3D"COLOR: =
#008000"><BR> * =E5=81=9C=E6=AD=A2=E8=87=
=AA=E5=8A=A8=E7=9B=91=E5=90=AC=E4=BB=BB=E5=8A=A1<BR> &nb=
sp; </SPAN><SPAN=20
style=3D"COLOR: #008000">*/</SPAN><SPAN=20
style=3D"COLOR: #000000"><BR> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">public</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">void</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> stop() {<BR>  =
; timer.cancel();<BR><BR> }<BR><BR>}<BR></SPA=
N></DIV><BR>=E8=87=AA=E5=8A=A8=E7=9B=91=E5=90=AC=E4=BB=BB=E5=8A=A1=20
PropertiesTask<BR><BR>
<DIV=20
style=3D"BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; =
BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; =
PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; =
WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px =
solid; BACKGROUND-COLOR: #eeeeee"><SPAN=20
style=3D"COLOR: #0000ff">package</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> org.stephencat.listener;<BR></SPAN><SPAN=20
style=3D"COLOR: #0000ff">import</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> java.io.IOException;<BR></SPAN><SPAN=20
style=3D"COLOR: #0000ff">import</SPAN><SPAN=20
style=3D"COLOR: #000000"> java.io.</SPAN><SPAN=20
style=3D"COLOR: #000000">*</SPAN><SPAN=20
style=3D"COLOR: #000000">;<BR></SPAN><SPAN=20
style=3D"COLOR: #0000ff">import</SPAN><SPAN=20
style=3D"COLOR: #000000"> java.util.</SPAN><SPAN=20
style=3D"COLOR: #000000">*</SPAN><SPAN=20
style=3D"COLOR: #000000">;<BR><BR></SPAN><SPAN=20
style=3D"COLOR: #0000ff">import</SPAN><SPAN=20
style=3D"COLOR: #000000"> javax.servlet.</SPAN><SPAN=20
style=3D"COLOR: #000000">*</SPAN><SPAN=20
style=3D"COLOR: #000000">;<BR></SPAN><SPAN=20
style=3D"COLOR: #008000">/**</SPAN><SPAN=20
style=3D"COLOR: =
#008000"><BR> * =E8=87=AA=E5=8A=A8=E7=9B=91=E5=90=AC=E4=BB=BB=E5=
=8A=A1<BR> * </SPAN><SPAN=20
style=3D"COLOR: #808080">@author</SPAN><SPAN=20
style=3D"COLOR: =
#008000"> stephen<BR> *<BR> </SPAN><SPAN=20
style=3D"COLOR: #008000">*/</SPAN><SPAN=20
style=3D"COLOR: #000000"><BR></SPAN><SPAN=20
style=3D"COLOR: #0000ff">public</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">class</SPAN><SPAN=20
style=3D"COLOR: #000000"> PropertiesTask </SPAN><SPAN=20
style=3D"COLOR: #0000ff">extends</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> TimerTask {<BR> <BR> &nbs=
p; </SPAN><SPAN=20
style=3D"COLOR: #0000ff">private</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> ServletContext context </SPAN><SPAN=20
style=3D"COLOR: #000000">=3D</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">null</SPAN><SPAN=20
style=3D"COLOR: #000000">;<BR> </SPAN><SPAN =
style=3D"COLOR: #008000">/**</SPAN><SPAN=20
style=3D"COLOR: =
#008000"><BR> * =E9=85=8D=E7=BD=AE=E6=96=
=87=E4=BB=B6=E7=9A=84=E6=9C=80=E5=90=8E=E6=9B=B4=E6=96=B0=E6=97=B6=E9=97=B4=
<BR> </SPAN><SPAN=20
style=3D"COLOR: #008000">*/</SPAN><SPAN=20
style=3D"COLOR: #000000"><BR> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">private</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">long</SPAN><SPAN=20
style=3D"COLOR: #000000"> lastModified </SPAN><SPAN=20
style=3D"COLOR: #000000">=3D</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #000000">0</SPAN><SPAN=20
style=3D"COLOR: =
#000000">;<BR> <BR> </SPAN>=
<SPAN=20
style=3D"COLOR: #008000">/**</SPAN><SPAN=20
style=3D"COLOR: =
#008000"><BR> * =E6=9E=84=E9=80=A0=E4=B8=
=80=E4=B8=AA=E8=87=AA=E5=8A=A8=E6=9B=B4=E6=96=B0=E4=BB=BB=E5=8A=A1<BR>&nb=
sp; * </SPAN><SPAN=20
style=3D"COLOR: #808080">@param</SPAN><SPAN=20
style=3D"COLOR: =
#008000"> context<BR> </SPAN><SPAN=20
style=3D"COLOR: #008000">*/</SPAN><SPAN=20
style=3D"COLOR: #000000"><BR> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">public</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> PropertiesTask(ServletContext context){<BR> &nbs=
p; </SPAN><SPAN=20
style=3D"COLOR: #0000ff">this</SPAN><SPAN=20
style=3D"COLOR: #000000">.context </SPAN><SPAN=20
style=3D"COLOR: #000000">=3D</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> context;<BR> &nbs=
p;System.out.println(</SPAN><SPAN=20
style=3D"COLOR: #000000">"</SPAN><SPAN=20
style=3D"COLOR: =
#000000">A task instance is created now.</SPAN><=
SPAN=20
style=3D"COLOR: #000000">"</SPAN><SPAN=20
style=3D"COLOR: #000000">); </SPAN><SPAN=20
style=3D"COLOR: #008000">//</SPAN><SPAN=20
style=3D"COLOR: =
#008000"> =E4=BB=BB=E5=8A=A1=E5=9C=A8=E6=95=B4=E4=B8=AA applica=
tion =E5=91=A8=E6=9C=9F=E5=86=85=E5=8F=AA=E5=88=9B=E5=BB=BA=E4=B8=80=
=E6=AC=A1=E3=80=82</SPAN><SPAN=20
style=3D"COLOR: #008000"><BR></SPAN><SPAN=20
style=3D"COLOR: =
#000000"> }<BR> <BR> &=
nbsp; </SPAN><SPAN=20
style=3D"COLOR: #008000">/**</SPAN><SPAN=20
style=3D"COLOR: =
#008000"><BR> * =E6=AF=8F=E6=AC=A1=E6=89=
=A7=E8=A1=8C=E4=BB=BB=E5=8A=A1=E6=97=B6=E6=98=BE=E7=A4=BA=E4=B8=80=E4=B8=AA=
=E9=9A=8F=E6=9C=BA=E6=95=B0=E3=80=82<BR> </S=
PAN><SPAN=20
style=3D"COLOR: #008000">*/</SPAN><SPAN=20
style=3D"COLOR: #000000"><BR> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">public</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">void</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> todoTestRandom(){<BR> &=
nbsp; System.out.println(</SPAN><SPAN=20
style=3D"COLOR: #000000">"</SPAN><SPAN=20
style=3D"COLOR: #000000">Task running</SPAN><SPAN=20
style=3D"COLOR: #000000">"</SPAN><SPAN=20
style=3D"COLOR: =
#000000">);<BR> context.se=
tAttribute(</SPAN><SPAN=20
style=3D"COLOR: #000000">"</SPAN><SPAN=20
style=3D"COLOR: #000000">random</SPAN><SPAN=20
style=3D"COLOR: #000000">"</SPAN><SPAN=20
style=3D"COLOR: =
#000000">, String.valueOf(Math.random()));<BR> &nbs=
p; System.out.println((String)context.getAttribute=
(</SPAN><SPAN=20
style=3D"COLOR: #000000">"</SPAN><SPAN=20
style=3D"COLOR: #000000">random</SPAN><SPAN=20
style=3D"COLOR: #000000">"</SPAN><SPAN=20
style=3D"COLOR: =
#000000">));<BR> }<BR> <BR>=
</SPAN><SPAN=20
style=3D"COLOR: #008000">/**</SPAN><SPAN=20
style=3D"COLOR: =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -