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

 


运行成功

上一篇:Dart语言学习(八) dynamic


下一篇:JavaScript对象 Object类型基础