1.實現功能html
(1)接收http請求java
(2)獲取Android客戶端發送的參數對應的內容數據庫
(3)hibernate查詢數據庫json
(4)返回json數據url
2.java代碼spa
import EntityClass.EntryFirminfoEntity; 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 java.io.IOException; import java.io.PrintWriter; import java.util.*; @WebServlet(name = "BasicMessageServlet",urlPatterns = "/BasicMessageServlet") public class BasicMessageServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 設置響應內容類型 response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); try (PrintWriter out = response.getWriter()) { //得到請求中傳來的用戶名和密碼 String companyname = request.getParameter("Companyname").trim(); String realAddress = request.getParameter("RealAddress").trim(); String riskrank = request.getParameter("Riskrank").trim(); String firmtype=request.getParameter("firmtype").trim(); ArrayList<String> firmtypeList=StringToArrayListUtil.stringToArrayList(firmtype); //調用hibernate,根據用戶輸入查詢數據 List<EntryFirminfoEntity> firminfolist= HibernateConnectUtil.queryBasicMessageByUserInput(companyname,realAddress,riskrank,firmtypeList); //採用「阿里巴巴」的json jar包下的JSONObject、JSONArray(該jar包的優點是使得jsonObject1.put()方法放入的數據是有序排列的) //JSONobject按照put順序存儲和讀取 com.alibaba.fastjson.JSONObject jsonObject=new com.alibaba.fastjson.JSONObject(true); com.alibaba.fastjson.JSONArray array = new com.alibaba.fastjson.JSONArray(); int i=0; for (EntryFirminfoEntity entryFirminfoEntity : firminfolist) { com.alibaba.fastjson.JSONObject jsonObject1=new com.alibaba.fastjson.JSONObject(new LinkedHashMap<>()); String firmName=entryFirminfoEntity.getFirmName(); String address=entryFirminfoEntity.getAddress(); String riskrankStr=entryFirminfoEntity.getRank(); String region=entryFirminfoEntity.getRegion(); String mgtypea=entryFirminfoEntity.getMgTypeA(); String mgtypeb=entryFirminfoEntity.getMgTypeB(); jsonObject1.put("firmname",firmName); jsonObject1.put("address",address); jsonObject1.put("riskrank",riskrankStr); jsonObject1.put("region",region); jsonObject1.put("mgtypea",mgtypea); jsonObject1.put("mgtypeb",mgtypeb); array.add(i,jsonObject1); i++; } jsonObject.put("params",array); out.write(jsonObject.toString()); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }