标签:javaee servletcontextlisten httpclient
通过监听器来实现
1.自定义一个类 CallLocationServelt 实现 ServletContextListener
并覆盖其 contextInitialized(ServletContextEventarg0)方法
public void contextInitialized(ServletContextEvent arg0) { // TODO Auto-generated method stub System.out.println("CallLocationServelt监听器启动了"); //利用定时器来执行每3秒访问一次 Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() { //初始化http请求客户端 HttpClient httpClient =new DefaultHttpClient(); //使用get方法进行请求 HttpGet httpGet = new HttpGet("http://localhost:8080/CallLocationServelt/servlet/LocalServlet"); try { //执行请求,并将结果存入Http响应对象中 HttpResponse response = httpClient.execute(httpGet); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }, 0, 3*1000);
<listener> <listener-class> CallLocationServelt </listener-class> </listener>
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("访问LocalServlet次数:"+(calltime++)); }
完整的项目可以下载: http://download.csdn.net/detail/ch717828/8394573
参考文献:http://blog.csdn.net/cai5/article/details/7528888
http://download.csdn.net/detail/aaayongaaa/5300578
http://download.csdn.net/detail/linzhijia0612/4965858
标签:javaee servletcontextlisten httpclient
原文地址:http://blog.csdn.net/ch717828/article/details/43113543