获取当前项目的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<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

时间: 2024-08-01 17:13:56

获取当前项目的URL的相关文章

php获取301跳转URL简单实例

 这篇文章主要介绍了php获取301跳转URL简单实例,有需要的朋友可以参考一下  代码如下: /**  * get_redirect_url()  * Gets the address that the provided URL redirects to,  * or FALSE if there's no redirect.   *  * @param string $url  * @return string  */ function get_redirect_url($url){    

php获取当前页面完整URL地址_php技巧

使用PHP编写程序的时候,我们常常想要获取当前页面的URL.下面提供一个用于获取当前页面URL的函数以及使用方法:示例一: <?php // 说明:获取完整URL function curPageURL() { $pageURL = 'http'; if ($_SERVER["HTTPS"] == "on") { $pageURL .= "s"; } $pageURL .= "://"; if ($_SERVER[&qu

python通过urllib2获取带有中文参数url内容的方法_python

本文实例讲述了python通过urllib2获取带有中文参数url内容的方法.分享给大家供大家参考.具体如下: 对于中文的参数如果不进行编码的话,python的urllib2直接处理会报错,我们可以先将中文转换成utf-8编码,然后使用urllib2.quote方法对参数进行url编码后传递. content = u'你好 jb51.net' content = content.encode('utf-8') content = urllib2.quote(content) api_url =

Golang strings.Split获取字符串中的url/域名的简易方法

package main import ( "fmt" "strings" ) func main() { fmt.Println("Hello World!") a := "golang strings.Split获取字符串中的url/域名的简易方法http://www.waylau.com/golang-strings-split-get-url/" a1 := strings.Split(a, "//"

javascript-Python 爬虫如何获取onclick(非url链接)之后网页?

问题描述 Python 爬虫如何获取onclick(非url链接)之后网页? Python 爬虫如何获取onclick里面内容,不需要用selenium 模拟点击,而是直接获得哦你click返回参数?具体比如说https://www.tripadvisor.com/ShowUserReviews-g57592-d416577-r357988112-The_Ivy_Inn_Restaurant-Charlottesville_Virginia.html#REVIEWS Tripadvisor 网站

Javascript获取background属性中url的值_javascript技巧

前言 最近在做项目的时候遇到一个问题,需要获取一个动态预览的图片的地址,这其实不是什么问题,主要是该图片的路径是写在css的background-img属性中的,于是决定要用js获取它的url中的内容,下面给大家分享解决的方法,有需要的朋友们下面来看看. var avatar = $("#image-preview").css("backgroundImage"); alert(avatar); 获取的是包含url("xxx.jpg")形式的值,

kindeditor+ js-kindeditor上传视频 点击确定后怎么获取文本框的url并把它赋值到富文本中

问题描述 kindeditor上传视频 点击确定后怎么获取文本框的url并把它赋值到富文本中 解决方案 kindeditorpluginsmediamedia.js这个文件 大概56行.. yesBtn : { name : self.lang('yes'), click: function (e) { var url = K.trim(urlBox.val()),//url变量存储的就是url输入框内容 解决方案二: @showbo 怎么创建一个一个视频标签啊,var html = K.med

linq 默认连接数据库是当前项目的.config文件。我程序如何想更改这个连接

问题描述 linq默认连接数据库是当前项目的.config文件.我程序如何想更改这个连接信息,就是不用配置文件的连接,想指定一个新的连接信息,不放在配置文件里面.如何处理,谢谢. 解决方案 解决方案二:using(BathDataContextdb=newBathDataContext()){}这个是原来项目的写法,最好保持这个不变.指定新的连接后.解决方案三:publicBathDataContext(stringdatabase="默认database"):base("N

自定义带进度条的WebView , 增加获取web标题和url 回掉

1.自定义ProgressWebView package com.app.android05; import android.content.Context; import android.graphics.Bitmap; import android.util.AttributeSet; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.ProgressBar; /