httpServlet,java web后台服务
生活随笔
收集整理的這篇文章主要介紹了
httpServlet,java web后台服务
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1,定時執(zhí)行的類
package com.utils;import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date;public class MyTimer extends Thread {// 間隔時間:小時private int intervalHours;// 誤差(操作所需時間可能導致誤差):分鐘private int deviationMinute;// 執(zhí)行時間private String runTime;public MyTimer(){// 參數(shù)取得intervalHours = 24;deviationMinute = 5;runTime= "19:30";}public void run() {while (!this.isInterrupted()) {// 線程未中斷執(zhí)行循環(huán)// 獲取當前時間:HH:mmDate now = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");String hours = dateFormat.format(now); // 時間到達if(hours.equals(runTime)){doRun();try {// 長時間休眠:間隔時間-誤差Thread.sleep(intervalHours * 3600 * 1000 - deviationMinute * 60 * 1000);} catch (InterruptedException e) {e.printStackTrace();}}else{try {// 等待間隔:30sThread.sleep(30000);System.out.println(hours);} catch (InterruptedException e) {e.printStackTrace();}}}}/*** 處理*/private void doRun() {} }
?
2,servlet
package com.utils;import javax.servlet.http.HttpServlet;public class MyServlet extends HttpServlet{/*** */private static final long serialVersionUID = 1L;private MyTimer myTimer; public MyServlet(){ } public void init(){ String str = null; if (str == null && myTimer== null) { myTimer= new MyTimer();myTimer.start(); }super.init(); } public void destory(){ if (myTimer!= null && myTimer.isInterrupted()) { myTimer.interrupt(); }
super.destroy(); } }
3,web.xml
</servlet>
?
轉載于:https://www.cnblogs.com/lckblog/p/4125678.html
總結
以上是生活随笔為你收集整理的httpServlet,java web后台服务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于解决Permission is on
- 下一篇: if ($this-getRequest