使用Tomcat做一个简单的Web服务器很简单,使用Tomcat做一个大型的网站也是Tomcat能够胜任的。前几天我就一直在想,如果我想做一款App,实现同组的驴友在活动时自动共享自己lat和lon信息以供其他人查看,位于热爱户外运动的人来说是一个挺不错的想法。我打算采用json个数的数据进行简单的数据交换,那么,服务器该怎么选择呢?使用php?不不不,那是我想做的还有一件事,就是在一个服务器内不仅仅是做请求->相应的动作,我想新开启一个全局的线程用来维护一些信息,那么以往的Tomcat能胜任吗?

答案是肯定的,下面献上我的代码:

package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import core.RoomControllerMessageProcessor;
import core.SubServer;
import core.TcpServer;

/**
* Servlet implementation class CoreService
*/
// @WebServlet("/CoreService")
@WebServlet(name = "/CoreService", urlPatterns = "/CoreService", loadOnStartup = 1)
public class CoreService extends HttpServlet {
private static final long serialVersionUID = 1L;

private static TcpServer tcps = null;
// private static TaskPusher taskPusherThread = null;

@Override
public void init() throws ServletException {
System.out.println("Starting..");
// main service
tcps = new TcpServer(RoomControllerMessageProcessor.class);
tcps.start(7564);

// task pusher
// taskPusherThread = new TaskPusher(tcps.procList);
// taskPusherThread.start();
super.init();
}

/**
* @see HttpServlet#HttpServlet()
*/
public CoreService() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

public static int changeStatus(int serverId, int controllerId, int deviceType, int deviceId, String value) {
// step 1 : find tcp target by server id & controller id
if (null == tcps)
return -1; // service is not running
SubServer s = null; // tag if find this client or not
for (SubServer sub : tcps.procList) {
RoomControllerMessageProcessor rcmp = (RoomControllerMessageProcessor) sub;
if (rcmp.controllerId == controllerId) {
s = rcmp;
break;
}
}
if (null != s) {
JSONObject jo = new JSONObject();
try {
jo.put("code", 7);
jo.put("deviceid", deviceId);
jo.put("devicetype", deviceType);
jo.put("value", value);
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println("[Send Command] : " + jo.toString());
s.send(jo.toString());
return 1;
} else {
return -2; // do not find this client cause by offline
}
}

public static List<SubServer> getClients() {
return null == tcps ? null : tcps.procList;
}

}


重点在这一句:

@WebServlet(name = "/CoreService", urlPatterns = "/CoreService", loadOnStartup = 1)

他告诉Tomcat容器在初始化的时候自动加载这个Servlet并且执行Servlet中的init()方法,其中就会执行我自己编写的那一部分逻辑。这样就实现了当我们部署war的时候,这个小模块就会随着Tomcat容器加载工程进而加载起来。