예제 #1
0
  /** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException, MongoException, DuplicateKeyException,
          UnknownHostException {
    // TODO Auto-generated method stub
    doGet(request, response);

    StringBuilder buffer = new StringBuilder();
    BufferedReader reader = request.getReader();
    String uname = request.getParameter("username");
    String password = request.getParameter("password");
    String email = request.getParameter("email");
    System.out.println(uname + " " + password + " " + email);
    /*String line;
    while((line=reader.readLine())!=null){
     buffer.append(line);
    }
    String data = buffer.toString();
    System.out.println(data);
    data = "{\"p\":\"N\",\"c\":\"W\"}";*/
    // System.out.println(data);
    // JSONObject params = (JSONObject)JSON.parse(data);
    JSONObject params = new JSONObject();
    params.put("username", uname);
    params.put("password", password);
    BasicDBObject user1 = new BasicDBObject(params);
    for (Object key : params.keySet().toArray()) {
      user1.put(key.toString(), params.get(key));
    }
    // System.out.println(user1.toJson());
    MongoClientURI uri =
        new MongoClientURI("mongodb://*****:*****@ds035014.mongolab.com:35014/vikas2");
    MongoClient client = new MongoClient(uri);
    DB db = client.getDB(uri.getDatabase());
    DBCollection users = db.getCollection("users");
    users.insert(user1);
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "POST");
    response.setHeader("Access-Control-Allow-Headers", "Content-Type");
    response.setHeader("Access-Control-Max-Age", "86400");
  }
예제 #2
0
 // This method assumes that the JSON value being passed is a JSON
 // non-primitive:
 // either an object or an array.
 // This helps to make this implementation as efficient as possible.
 protected String jsonNonPrimitiveToPrettyString(JSONValue jsonValue, int indentLevel) {
   StringBuffer buffer = new StringBuffer();
   // If the value in question is an object
   JSONObject jsonValueObject = jsonValue.isObject();
   if (jsonValueObject != null) {
     boolean firstKey = true;
     for (String key : jsonValueObject.keySet()) {
       if (firstKey) {
         firstKey = false;
       } else {
         buffer.append(",\n");
       }
       for (int k = 0; k < indentLevel; ++k) {
         buffer.append("  ");
       }
       buffer.append(key).append(" : ");
       JSONValue jsonObjectValue = jsonValueObject.get(key);
       if (jsonObjectValue != null) {
         if (jsonObjectValue.isObject() != null) {
           buffer
               .append("{\n")
               .append(jsonNonPrimitiveToPrettyString(jsonObjectValue, indentLevel + 1))
               .append("}");
         } else if (jsonObjectValue.isArray() != null) {
           buffer
               .append("[\n")
               .append(jsonNonPrimitiveToPrettyString(jsonObjectValue, indentLevel + 1))
               .append("]");
         } else {
           // If the object value is a primitive, just prints it,
           // there is no need for a recursive call!
           buffer.append(jsonObjectValue.toString());
         }
       }
     }
   } else if (jsonValue.isArray() != null) {
     // If the value in question is an array
     JSONArray jsonValueArray = jsonValue.isArray();
     for (int i = 0; i < jsonValueArray.size(); ++i) {
       if (0 < i) {
         buffer.append(",\n");
       }
       for (int k = 0; k < indentLevel; ++k) {
         buffer.append("  ");
       }
       JSONValue jsonArrayValue = jsonValueArray.get(i);
       if (jsonArrayValue != null) {
         if (jsonArrayValue.isObject() != null) {
           buffer
               .append("{\n")
               .append(jsonNonPrimitiveToPrettyString(jsonArrayValue, indentLevel + 1))
               .append("}");
         } else if (jsonArrayValue.isArray() != null) {
           buffer
               .append("[\n")
               .append(jsonNonPrimitiveToPrettyString(jsonArrayValue, indentLevel + 1))
               .append("]");
         } else {
           // If the object value is a primitive, just prints it,
           // there is no need for a recursive call!
           buffer.append(jsonArrayValue.toString());
         }
       }
     }
   }
   return buffer.toString();
 }