给web 服务器后台加线程

来源:互联网 发布:网站一般使用cms吗 编辑:程序博客网 时间:2024/05/17 14:19

一。新建一个servlet{

Java代码 复制代码
  1. package com.ibox.init;   
  2. import java.io.IOException;   
  3. import java.io.PrintWriter;   
  4. import javax.servlet.ServletContext;   
  5. import javax.servlet.ServletException;   
  6. import javax.servlet.http.HttpServlet;   
  7. import javax.servlet.http.HttpServletRequest;   
  8. import javax.servlet.http.HttpServletResponse;   
  9. import com.ibox.util.ServerTimer;   
  10.   
  11. public class InitServlet extends HttpServlet {   
  12.  public InitServlet() {   
  13.   super();   
  14.  }   
  15.  public void destroy() {   
  16.   super.destroy(); // Just puts "destroy" string in log   
  17.   // Put your code here   
  18.  }   
  19.  public void doGet(HttpServletRequest request, HttpServletResponse response)   
  20.    throws ServletException, IOException {   
  21.   doPost(request, response);   
  22.  }   
  23.  public void doPost(HttpServletRequest request, HttpServletResponse response)   
  24.    throws ServletException, IOException {   
  25.  }   
  26.  public void init() throws ServletException {   
  27.      
  28.   ServletContext application = getServletConfig().getServletContext();   
  29.   String realpath = this.getServletContext().getRealPath("/");   
  30.   new ServerTimer(application,realpath).start();   
  31.      
  32.  }   
  33. }  

}
二。在web.xml配这个servlet{

Java代码 复制代码
  1. <!-- 初始化信息 -->   
  2. <servlet>   
  3.   <servlet-name>InitValue</servlet-name>   
  4.   <servlet-class>com.ibox.init.InitServlet</servlet-class>   
  5.   <load-on-startup>7</load-on-startup>   
  6. </servlet>  

}
三。新建serverTimer线程类{

Java代码 复制代码
  1. package com.ibox.util;   
  2. import java.util.Date;   
  3. import javax.servlet.ServletContext;   
  4. public class ServerTimer extends Thread {   
  5.  ServletContext application = null;   
  6.  String realpath = "";   
  7.  int step = 1000;   
  8.  public ServerTimer(ServletContext application,String realpath){   
  9.   this.application = application;   
  10.   this.realpath = realpath;   
  11.  }   
  12.  public ServerTimer(ServletContext application,String realpath,int step){   
  13.   this.application = application;   
  14.   this.realpath = realpath;   
  15.   this.step = step;   
  16.  }   
  17.  public void run(){   
  18.   while(true){   
  19.    ServerTimerInterface st = new GetServetTime();   
  20.    st.timerStart(application);   
  21.    try{   
  22.     Thread.sleep(step);   
  23.    }catch (Exception e) {   
  24.     e.printStackTrace();   
  25.    }   
  26.   }   
  27.  }   
  28. }  


四。新建执行接口{

Java代码 复制代码
  1. package com.ibox.util;   
  2. import javax.servlet.ServletContext;   
  3.   
  4. public interface ServerTimerInterface {   
  5.  public void timerStart();   
  6.  public void timerStart(ServletContext application);   
  7. }  



五。实现执行方法{

Java代码 复制代码
  1. package com.ibox.util;   
  2. import java.util.Date;   
  3. import javax.servlet.ServletContext;   
  4.   
  5. public class GetServetTime implements ServerTimerInterface {   
  6.  public void timerStart() {   
  7.  }   
  8.  public void timerStart(ServletContext application) {   
  9. //  String cpumsg = new GetCPU().getCpu(realpath);   
  10.   Date date = new Date();   
  11.   int year = date.getYear() + 1900;   
  12.   int month = date.getMonth()+1;   
  13.   int d = date.getDate();   
  14.   int day = date.getDay();   
  15.   int hour = date.getHours();   
  16.   int min = date.getMinutes();   
  17.   int sec = date.getSeconds();   
  18.   String timeStr = ""+year+" "+month+" "+" "+addZero(hour)+":"+addZero(min)+":"+addZero(sec);   
  19.   application.setAttribute("nowtime", timeStr);   
  20.      
  21.  }   
  22.     
  23.  private String addZero(int a){   
  24.   String s = "";   
  25.   if(a < 10){   
  26.    s = "0"+a;   
  27.   }else{   
  28.    s = ""+a;   
  29.   }   
  30.   return s;   
  31.  }   
  32. }  



本例为获取服务器时间并存入application内
本例只开了一个线程,基本够用,如需更多线程,要改源码
(按提示顺序建类会报错,但都建完就好了,也可倒序建)

原创粉丝点击