package com.hex.sms;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
public class CallbackServlet extends HttpServlet {
/**
* 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 {
request.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=UTF-8");
String randCode=request.getParameter("rand_code");
String identifier=request.getParameter("identifier");
PrintWriter out = response.getWriter();
Map<String,Object> map = new HashMap<String,Object>();
map.put("randCode", randCode);
map.put("identifier", identifier);
String res_code = "0";//0 获取成功
if(randCode==null) {
res_code = "1";// 1 获取失败
}
System.out.println("验证码:"+randCode);
map.put("res_code", res_code);
String jsonString = toJson(map);
out.print(jsonString);
out.flush();
}
public static String toJson(Map<String,Object> paramsMap) {
JSONObject json = new JSONObject();
try {
for (String key : paramsMap.keySet()) {
json.put(key, paramsMap.get(key));
}
return json.toString();
}catch(JSONException e) {
e.printStackTrace();
throw new RuntimeException("转换json时出现错误", e.getCause());
}
}
}