响应json数据之发送ajax的请求
一、前端异步请求代码:
<span style="font-size:14px;">$.ajax ({ type: "POST", //请求的方式 url: "outputservlet3", //请求url data: {}, //前端向后端传送的数据 dataType: "json", //数据格式json error: function () //请求错误的回调函数 { }, success: function () //请求成功的回调函数 { } })</span>
二、WEB-INF下的web.xml的servlet-url的配置方法:
<span style="font-size:14px;"><servlet> <servlet-name>outputservlet3</servlet-name> <servlet-class>output.outputservlet3</servlet-class> //对应的Java类 </servlet> <servlet-mapping> <servlet-name>outputservlet3</servlet-name> <url-pattern>/outputservlet3</url-pattern> //注意这里开始的地方有“/” </servlet-mapping></span>
三、后端的Java代码:
//响应的是post请求
<span style="font-size:14px;">protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //doGet(request, response); OracleConnection myoracle=new OracleConnection (); Connection conn=null; PreparedStatement pre=null; ResultSet result = null; String sql="select RANK, POINT from MAPGIS.FIRERANK,MAPGIS.WUHAN where FIRERANK.WUHAN_ID=WUHAN.ID "; JSONObject jsonObject = new JSONObject(); PrintWriter out = response.getWriter(); try { conn = myoracle.getConnection1(); pre = conn.prepareStatement(sql); result = pre.executeQuery(); while(result.next()) { out o1=new out(); o1.setRank(result.getInt("rank")); JGeometry geom = JGeometry.load(result.getBytes(2)); point= geom.getLabelPointXYZ(); o1.setX(point[0]); o1.setY(point[1]); out1.add(o1); } JSONArray ja3=JSONArray.fromObject(out1); jsonObject.put("point", ja3); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { myoracle.close(result, pre, conn); } out.print(jsonObject.toString()); }</span>
运行成功
相关推荐
wujiajax 2020-06-14
kentrl 2020-11-10
结束数据方法的参数,该如何定义?-- 集合为自定义实体类中的结合属性,有几个实体类,改变下标就行了。<input id="add" type="button" value="新增visitor&quo
ajaxyan 2020-11-09
zndy0 2020-11-03
学留痕 2020-09-20
Richardxx 2020-11-09
learningever 2020-09-19
chongxiaocheng 2020-08-16
ajaxhe 2020-08-16
lyqdanang 2020-08-16
curiousL 2020-08-03
TONIYH 2020-07-22
时光如瑾雨微凉 2020-07-19
83510998 2020-07-18
坚持着执着 2020-07-16
jiaguoquan00 2020-07-07
李永毅 2020-07-05