package Servlet; import Date.Dao; import Model.LineModel; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; 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.sql.SQLException; import java.util.ArrayList; import java.util.List; @WebServlet("/ServletXian") public class ServletXian extends HttpServlet { public Dao da = new Dao(); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { int no1 = Integer.parseInt( req.getParameter("Xid") ); System.out.println(no1); LineModel data = new LineModel(); //1.定义一个json数组 JSONArray jsonData = new JSONArray(); data = da.selectkluxian(no1); for (int i = 0; i < data.getStations().size(); i++) { //2.定义json对象 JSONObject object = new JSONObject(); String ms = data.getStations().get(i); System.out.println(ms); object.put("zname", ms); //3.将所有的json对象存放在json数组jsonData中 jsonData.add(object); } //4.返回json数组 resp.setCharacterEncoding("UTF-8"); resp.getWriter().write(jsonData.toString());; } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } }
标签:总结,27,java,resp,json,import,servlet,javax From: https://www.cnblogs.com/liucaizhi/p/17263351.html