JaveWeb 公司项目(3)----- 通过Thrift端口获取数据库数据
前面两篇博客的内容主要是界面搭建的过程,随着界面搭建工作的完成,网页端需要加入数据,原先的B/S架构中C#通过Thrift接口获取数据,所以在网页端也沿用这个设计
首先,新建一个Maven下的Web项目。
在Main文件夹下面新建一个Java的文件夹,在Java文件夹下面新建两个文件夹,此处我命名为com.thrift.iface(Thrift接口)和JSPServlet,在com.thrift.iface文件夹下导入.java类型的Thrift数据,在JSPServlet文件夹下面新建Servlrt类型的文件,将java文件夹改为Sources类型,在pom.xml中导入需要引用的jar包,代码在之后写的一篇idea创建maven项目的博客中有提到,网址如下:http://www.cnblogs.com/Liu30/p/7268138.html
首先定义一个Servlrt基类:
1 package com.fis.iface; 2 import com.fis.thrift.RemoteServer.Client; 3 import org.apache.thrift.protocol.TBinaryProtocol; 4 import org.apache.thrift.protocol.TProtocol; 5 import org.apache.thrift.transport.TSocket; 6 import org.apache.thrift.transport.TTransport; 7 import org.apache.thrift.transport.TTransportException; 8 9 import javax.servlet.ServletException; 10 import javax.servlet.annotation.WebServlet; 11 import javax.servlet.http.HttpServlet; 12 import javax.servlet.http.HttpServletRequest; 13 import javax.servlet.http.HttpServletResponse; 14 import java.io.IOException; 15 16 @WebServlet(name = "UBaseServlet") 17 public class UBaseServlet extends HttpServlet { 18 protected TTransport thrift_transport; 19 protected Client Thrift_Client; 20 21 protected void startClient() throws TTransportException { 22 thrift_transport = new TSocket("IP地址", 端口号,); 23 TProtocol protocol = new TBinaryProtocol(thrift_transport); 24 Thrift_Client = new Client(protocol); 25 thrift_transport.open(); 26 } 27 28 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 29 30 } 31 32 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 33 34 } 35 36 protected void closeClient() throws TTransportException { 37 thrift_transport.close(); 38 Thrift_Client = null; 39 } 40 }
然后新建其他的servlet文件,继承这个基类,例如,我要通过Thrift获取一个list数组,servlret文件下的代码如下:
package com.fis.iface; import com.fis.thrift.Thrift_Law; import com.fis.thrift.Thrift_Sign; import net.sf.json.JSONArray; import org.apache.thrift.TException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.util.List; //继承基类,设置servlet的名称和设置url路径 @WebServlet(name = "servler文件名",urlPatterns = "/路径名称") public class getlawsnames extends UBaseServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置编码的格式,避免乱码 request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=utf-8"); try { startClient(); //在这里定义thrift函数实例 closeClient(); JSONArray jsonArray = JSONArray.fromObject(Thrift实例名); PrintWriter out = response.getWriter(); out.println(jsonArray); out.flush(); out.close(); } catch (TException e) { e.printStackTrace(); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
值得一提的是,有时候我们从Thrift中获取的数据显示出来是乱码,因为编码方式不统一的缘故,只要在dopost函数前加这么两句话即可:
request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=utf-8");
新建一个JS文件,通过Ajax获取数据
function getLogInfo() { $.ajax({ dataType: "json", //数据类型为json格式 contentType: "application/x-www-form-urlencoded; charset=utf-8", type: "POST", url: "/刚才设置的thrift路径", statusCode: { 404: function () { alert('没有获取到数据'); } }, success: function (data) { alert('成功获取到数据'); //这个data就是获取到得数据 } });
在主页面中加载这个JS文件即可。