码迷,mamicode.com
首页 > Web开发 > 详细

响应json数据之发送ajax的请求

时间:2019-12-23 13:44:23      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:where   cti   null   ror   new   ted   stack   sonar   pos   

一、前端异步请求代码:

<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>

 


运行成功

响应json数据之发送ajax的请求

标签:where   cti   null   ror   new   ted   stack   sonar   pos   

原文地址:https://www.cnblogs.com/almm/p/12082954.html

(0)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!