给web 服务器后台加线程
来源:互联网 发布:网站一般使用cms吗 编辑:程序博客网 时间:2024/05/17 14:19
一。新建一个servlet{
- 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内
本例只开了一个线程,基本够用,如需更多线程,要改源码
(按提示顺序建类会报错,但都建完就好了,也可倒序建)
- 给web 服务器后台加线程
- 给服务器加一个关闭监听线程
- node.js web服务器开发 实践(二), 给web服务器加内存缓存
- c# 后台 给控件 加样式
- c# 后台给控件加样式
- Tomcat后台服务器+Apache的web服务器
- WEB后台传数据给前台
- WEB后台传数据给前台
- WEB后台传数据给前台
- web启动后台线程定时执行
- 简易线程池web服务器
- 验证码 web服务器后台报错
- 求给文章关键字加链接函数[WEB学习示例]
- EXTJS AJAX方式发送数据给后台服务器
- WEB客户端发送给WEB服务器的HTTP请求消息
- magento -- 给后台分类管理页的分类商品加一栏商品类型
- magento -- 给后台分类管理页的分类商品加一栏商品类型
- iOS 后台给的加签字符串无法调用支付宝
- 三巨擘合推Google电视开拓商机
- oracle 10g 控制台启动端口修改
- Visual Studio 2008常用小技巧
- 链表的插入操作及链表的倒置
- 在vb中使用Iphlpapi.dll获取网络信息 第二章 第一节 获取本机网络适配器的信息
- 给web 服务器后台加线程
- c# 直接打印pdf文件
- VS控件命名规范
- 海运提单和提货单有什么区别啊?
- 对话框和浮动Activity
- javascript 实现联动的下拉菜单
- 在vb中使用Iphlpapi.dll获取网络信息 第二章 第二节 获取MIB-II 接口表
- VS2008快捷键大全
- Studying note of GCC-3.4.6 source (44)