-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetMapTest.java
More file actions
119 lines (91 loc) · 2.99 KB
/
getMapTest.java
File metadata and controls
119 lines (91 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package test;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import com.meetingroom.servlet.JsonDateValueProcessor;
import oracle.pool.database.DBConnect;
public class getMapTest extends HttpServlet {
/**
* Constructor of the object.
*/
public getMapTest() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ArrayList result=new ArrayList();
String sql="select trim(c.region) region,count(*) total from customer c where c.region is not " +
"null group by trim(c.region) order by total desc";
try {
DBConnect db=new DBConnect();
ResultSet res=null;
res= db.executeQuery(sql);
while(res.next()){
mapElement mp=new mapElement();
mp.setName(res.getString("region"));
mp.setValue(res.getInt("total"));
result.add(mp);
}
db.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//JsonConfig是net.sf.json.JsonConfig中的,这个为固定写法
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(Date.class , new JsonDateValueProcessor());
//将List转化为JSON
JSONArray json=JSONArray.fromObject(result,jsonConfig);
//设置编码
response.setCharacterEncoding("utf-8");
//写入到前台
response.getWriter().write(json.toString());
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}