packageservlet1;importiface.RemoteServer;importiface.Thrift_TargetInfo;importnet.sf.json.JSONArray;importorg.apache.thrift.TException;importorg.apache.thrift.protocol.TBinaryProtocol;importorg.apache.thrift.protocol.TProtocol;importorg.apache.thrift.transport.TSocket;importorg.apache.thrift.transport.TTransport;importorg.apache.thrift.transport.TTransportException;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.io.IOException;importjava.io.PrintWriter;importjava.util.ArrayList;
@WebServlet(name= "Servlet1", urlPatterns = "/getTgs")public class Servlet1 extendsHttpServlet {private static final int SERVER_PORT = 16003;private static final String SERVER_IP = "192.168.31.235";public staticRemoteServer.Client Thrift_Client;public staticTTransport Thrift_transport;public void startClient() throwsTTransportException {int TIMEOUT = 30000;
Thrift_transport= newTSocket(SERVER_IP, SERVER_PORT, TIMEOUT);
TProtocol protocol= newTBinaryProtocol(Thrift_transport);
Thrift_Client= newRemoteServer.Client(protocol);
Thrift_transport.open();
System.out.println("Thrift16003端口已开启,客户端已创建...");
}protected void doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {
}protected void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {
JSONArray jsonArray= newJSONArray();try{
startClient();
ArrayList ships = (ArrayList) Thrift_Client.Thrift_GetTarget();
jsonArray=JSONArray.fromObject(ships);
System.out.println(jsonArray);
}catch(TException e) {
e.printStackTrace();
}//向前台的页面输出数据jsonArray
PrintWriter out =response.getWriter();
out.println(jsonArray);
out.close();
}
}