一。新建一个servlet{
二。在web.xml配这个servlet{
三。新建serverTimer线程类{
四。新建执行接口{
}
五。实现执行方法{
}
本例为获取服务器时间并存入application内
本例只开了一个线程,基本够用,如需更多线程,要改源码
(按提示顺序建类会报错,但都建完就好了,也可倒序建)
参考:无,个人项目用
package com.ibox.init;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ibox.util.ServerTimer;
public class InitServlet extends HttpServlet {
public InitServlet() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
public void init() throws ServletException {
ServletContext application = getServletConfig().getServletContext();
String realpath = this.getServletContext().getRealPath("/");
new ServerTimer(application,realpath).start();
}
}
}
二。在web.xml配这个servlet{
<!-- 初始化信息 -->
<servlet>
<servlet-name>InitValue</servlet-name>
<servlet-class>com.ibox.init.InitServlet</servlet-class>
<load-on-startup>7</load-on-startup>
</servlet>
}
三。新建serverTimer线程类{
package com.ibox.util;
import java.util.Date;
import javax.servlet.ServletContext;
public class ServerTimer extends Thread {
ServletContext application = null;
String realpath = "";
int step = 1000;
public ServerTimer(ServletContext application,String realpath){
this.application = application;
this.realpath = realpath;
}
public ServerTimer(ServletContext application,String realpath,int step){
this.application = application;
this.realpath = realpath;
this.step = step;
}
public void run(){
while(true){
ServerTimerInterface st = new GetServetTime();
st.timerStart(application);
try{
Thread.sleep(step);
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
四。新建执行接口{
package com.ibox.util;
import javax.servlet.ServletContext;
public interface ServerTimerInterface {
public void timerStart();
public void timerStart(ServletContext application);
}
}
五。实现执行方法{
package com.ibox.util;
import java.util.Date;
import javax.servlet.ServletContext;
public class GetServetTime implements ServerTimerInterface {
public void timerStart() {
}
public void timerStart(ServletContext application) {
// String cpumsg = new GetCPU().getCpu(realpath);
Date date = new Date();
int year = date.getYear() + 1900;
int month = date.getMonth()+1;
int d = date.getDate();
int day = date.getDay();
int hour = date.getHours();
int min = date.getMinutes();
int sec = date.getSeconds();
String timeStr = ""+year+" "+month+" "+" "+addZero(hour)+":"+addZero(min)+":"+addZero(sec);
application.setAttribute("nowtime", timeStr);
}
private String addZero(int a){
String s = "";
if(a < 10){
s = "0"+a;
}else{
s = ""+a;
}
return s;
}
}
}
本例为获取服务器时间并存入application内
本例只开了一个线程,基本够用,如需更多线程,要改源码
(按提示顺序建类会报错,但都建完就好了,也可倒序建)
参考:无,个人项目用