我最近添加了一个名为“GetAveGoogleChart.Java”的servlet,它将通过hibernate从MySQLDB中获取值。
公共类GetAveGoogleChart扩展了HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/json");
response.setContentType("text/html;charset=UTF-8");
String applicationName = request.getParameter("appName");
String transactionName = request.getParameter("tranName");
TransactionAO myAO = new TransactionAO();
Iterator<Transactionentry> transactionEntries= myDAO.getTransactions(applicationName, transactionName).listIterator();
//System.out.println("Entries size = " + transactionEntries.size());
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd'T'HH:mm:ss");
SimpleDateFormat sdf1 = new SimpleDateFormat("YYYY,MM,dd,HH,mm,ss");
JSONObject rootNode = new JSONObject();
JSONArray colsArray = new JSONArray();
JSONObject col1Object = new JSONObject();
col1Object.put("id", "TestDate");
col1Object.put("label", "TestDate");
col1Object.put("type", "datetime");
col1Object.put("p", new JSONArray());
JSONObject col2Object = new JSONObject();
col2Object.put("id", "Utils");
col2Object.put("label", "CPU Average (in %)");
col2Object.put("type", "number");
col2Object.put("p", new JSONArray());
colsArray.put(col1Object);
colsArray.put(col2Object);
JSONArray rowsArray = new JSONArray();
while(transactionEntries.hasNext()){
Transactionentry te=transactionEntries.next();
Date entryDate = te.getTranendtime();
double value = te.getAvecpu().doubleValue();
JSONObject rowObject = new JSONObject();
JSONArray cArray = new JSONArray();
JSONObject myDateObject = new JSONObject();
myDateObject.put("v", "Date("+sdf1.format(entryDate)+")");
JSONObject valueObject = new JSONObject();
valueObject.put("v", value);
cArray.put(myDateObject);
cArray.put(valueObject);
rowObject.put("c", cArray);
rowsArray.put(rowObject);
}
//JSONObject colsObject = new JSONObject();
//colsObject.put("cols", colsArray);
//JSONObject rowsObject = new JSONObject();
//rowsObject.put("rows", rowsArray);
rootNode.put("cols", colsArray);
rootNode.put("rows", rowsArray);
PrintWriter out = response.getWriter();
// Assuming your json object is **jsonObject**, perform the following, it will return your json object
out.print(rootNode);
out.flush();
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
当我点击带有网址
的网页时http://localhost:8080/sample/GetAveGoogleChart?tranName=Login&appName=SVA%20-%20And
我收到的回复是
Servlet GetAveGoogleChart at / sample
我没有得到任何其他有效的JSON响应。
请帮我解决问题。
提前致谢。
答案 0 :(得分:3)
在上面的代码中,您有:
response.setContentType("application/json");
response.setContentType("text/html;charset=UTF-8");
如果删除第二个response.setContentType()调用,这可能会有所帮助:)