cl13142006 2010-11-16 10:13
浏览 632
已采纳

获取当前项目的URL

有一个定时器 需要每天定时访问该项目的一个链接,从而实现发送邮件提醒的功能 ,

但是不能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 timerList = new ArrayList();
// 间隔定时器列表

private List perList = new ArrayList();

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) ;

}

}

  • 写回答

1条回答

  • zzfolin 2010-11-16 12:45
    关注

    在Servlet里面我可以获取到application (不用new) request没琢磨出来 但是我给你找了一个很好的文章 希望对你有帮助
    [url]http://haofeng82.iteye.com/blog/456323[/url]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺