问题描述
有一个定时器 需要每天定时访问该项目的一个链接,从而实现发送邮件提醒的功能 ,但是不能new一个 HttpServletRequest new一个之后定时器失效, init方法中需要获得URL, 下面是代码 你懂的。package com.lawstar.mod.timer.serv;import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.lawstar.mod.timer.timers.AlarmClock;import com.lawstar.mod.timer.timers.LoadTimerConfig;import com.lawstar.mod.timer.timers.PerTimer;import com.lawstar.mod.timer.timers.TimerPojo;/** *@author 鸭梨 E-MAIL:togetyou@gmail.com *date and time:Oct 13, 2009 5:53:02 PM *comp:law-star */public class StartTimerServ extends HttpServlet implements ServletContextListener{/** * 销毁定时器 */public void contextDestroyed(ServletContextEvent sce) {System.out.println("des the timer");List<com.lawstar.mod.timer.timers.AlarmClock> listCL = (List)sce.getServletContext().getAttribute("timerList") ;System.out.println("listcl:"+listCL.size());for(Iterator<AlarmClock> it = listCL.iterator() ;it.hasNext();){AlarmClock al = it.next() ;al.cancel() ;System.out.println("---销毁定时器---"+al.getName());}List<PerTimer> listPer = (List)sce.getServletContext().getAttribute("perList") ;System.out.println("listPer:"+listPer.size());for(Iterator<PerTimer> it = listPer.iterator() ;it.hasNext();){PerTimer al = it.next() ;al.cancel() ;System.out.println("---销毁定时器---"+al.getName());}}public void contextInitialized(ServletContextEvent sce) {// 什么也不干}//每天执行的定时器的列表private List<AlarmClock> timerList = new ArrayList<AlarmClock>();//间隔定时器列表private List<PerTimer> perList = new ArrayList<PerTimer>();public StartTimerServ() {super();}/** * Destruction of the servlet. <br> */public void destroy() {super.destroy(); // Just puts "destroy" string in log// Put your code here}/** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");PrintWriter out = response.getWriter();out.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">");out.println("<HTML>");out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println(" <BODY>");out.print(" This is ");out.print(this.getClass());out.println(", using the GET method do nothing");out.println(" </BODY>");out.println("</HTML>");out.flush();out.close();}/** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");PrintWriter out = response.getWriter();out.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">");out.println("<HTML>");out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println(" <BODY>");out.print(" This is ");out.print(this.getClass());out.println(", using the POST method do nothing");out.println(" </BODY>");out.println("</HTML>");out.flush();out.close();}/** * servlet 初始化 启动定时器 */public void init() throws ServletException { LoadTimerConfig ltc = new LoadTimerConfig(); List<TimerPojo> ltList = ltc.getTimerList("timer.xml") ; System.out.println(ltList.size()) ; TimerPojo tpo = null ; String type = "" ; for(int i=0;i<ltList.size();i++) { tpo = ltList.get(i) ; type = tpo.getType() ; if("range".equals(type)) { PerTimer ptm = new PerTimer(tpo.getName()) ; ptm.setUrlList(tpo.getUrlList()) ; ptm.runTask(tpo.getRange()); perList.add(ptm) ; } else if("eday".equals(type)) { AlarmClock alm = new AlarmClock(tpo.getHour(),tpo.getMinute(),tpo.getSec(),tpo.getName()) ; alm.setUrlList(tpo.getUrlList()) ; alm.start() ; timerList.add(alm) ; } } this.getServletContext().setAttribute("perList", perList) ; this.getServletContext().setAttribute("timerList", timerList) ; }}
解决方案
在Servlet里面我可以获取到application (不用new) request没琢磨出来 但是我给你找了一个很好的文章 希望对你有帮助http://haofeng82.iteye.com/blog/456323