servlet程序。。。高手进
麻烦高手帮我看下~~~~编写一个名为Greeting的Servlet程序:能根据用户访问服务器的时间给出不同的问候语:0―8时输出“早上好”,8―12时输出“上午好”,12―18时输出“下午好”,18―24时输出“晚上好”。
搜索更多相关主题的帖子:
servlet
----------------解决方案--------------------------------------------------------
没时间帮你写,这个很简单,给你个思路,写个线程,里边获取系统时间。0―8时输出“早上好”,8―12时输出“上午好”,12―18时输出“下午好”,18―24时输出“晚上好”。 可以利用线程的休眠来控制它的输出间隔时间!
----------------解决方案--------------------------------------------------------
抽了点时间给你写了,但没时间测。
每隔有分钟输出一次,你自己可以改。
public class Greeting extends HttpServlet {
public void init() throws ServletException {
new Thread(new GreetingThread(), "==>GreetingThread<==").start();
}
public class GreetingThread implements Runnable {
public void run() {
while (true) {
try {
doWork();
Thread.sleep(60000);
} catch (InterruptedException ex) {
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void doWork() {
Date zero = null;
Date eight = null;
Date sixteen = null;
Date twentyFour = null;
Date now = null;
Timestamp nowTime = new Timestamp(System.currentTimeMillis());
DateFormat allTimeFormat = new SimpleDateFormat("HH:mm:ss.SSS");
//Date allTime = null;
try {
zero = allTimeFormat.parse("00:00:00.000");
eight = allTimeFormat.parse("08:00:00.000");
sixteen = allTimeFormat.parse("16:00:00.000");
twentyFour = allTimeFormat.parse("24:00:00.000");
now = allTimeFormat.parse(allTimeFormat.format(nowTime));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (now.after(zero) && now.before(eight)) {
System.out.println("早");
} else if (now.before(sixteen)) {
System.out.println("中");
} else if (now.before(sixteen)) {
System.out.println("下");
} else {
System.out.println("晚");
}
}
}
}
}
----------------解决方案--------------------------------------------------------
你也不回来看看。。。。不管你了。
----------------解决方案--------------------------------------------------------
不错
----------------解决方案--------------------------------------------------------
看不懂啊
----------------解决方案--------------------------------------------------------
像这个问题叫我做我就用Timer做,每隔5分钟执行一次,取系统时间判断...
----------------解决方案--------------------------------------------------------
有点借鉴价值,收下了
----------------解决方案--------------------------------------------------------
写的很棒,但是用个线程似乎有点把问题弄大了,毕竟线程不是什么好主,轻易还是。。。
----------------解决方案--------------------------------------------------------
流星雨,谢谢你拉。。不过不是我不回来看,而且我写好了。不过我其实要的是获取到客户端的时间,比如你来访问我的服务器,我要获取的是你的时间而不是我服务器的时间。。
----------------解决方案--------------------------------------------------------