1.实现功能
(1)接收http请求
(2)获取Android客户端发送的参数对应的内容
(3)hibernate查询数据库
(4)返回json数据
2.java代码
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 { } }