public void registrarUsuario() { String dato = ""; JSONParser parser = new JSONParser(); try { while (true) { dato = dis.readUTF(); String usuario = ""; String contraseña = ""; String contra = ""; if (dato.equals("Usuario")) { while (true) { usuario = dis.readUTF(); if (usuario != null) { JSONObject user = new JSONObject(); user.put(usuario, null); FileWriter escribir = new FileWriter("texto.txt"); BufferedWriter bw = new BufferedWriter(escribir); PrintWriter pw = new PrintWriter(bw); pw.write(user.toJSONString()); pw.close(); bw.close(); while (true) { contra = dis.readUTF(); if (contra.equals("Contraseña")) { while (true) { contraseña = dis.readUTF(); if (contraseña != null) { Object obj = parser.parse(new FileReader("texto.txt")); JSONObject asignaPass = (JSONObject) obj; asignaPass.put(usuario, contra); FileWriter escribir2 = new FileWriter("texto.txt"); BufferedWriter bw2 = new BufferedWriter(escribir2); PrintWriter pw2 = new PrintWriter(bw2); pw2.write(asignaPass.toJSONString()); bw2.newLine(); pw2.close(); bw2.close(); break; } } break; } } break; } } break; } } } catch (Exception e) { } }
/** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ @SuppressWarnings("unchecked") protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("application/json"); response.setHeader("Cache-Control", "nocache"); response.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); StringWriter result = new StringWriter(); // get received JSON data from request BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream())); String postData = ""; if (br != null) { postData = br.readLine(); } try { JSONObject json = (JSONObject) new JSONParser().parse(postData); JSONObject resultObj = new JSONObject(); JSONArray list = new JSONArray(); List<Tracking> trackingList = new ArrayList<Tracking>(); // get the website list if (json.get("type").equals("websiteslist")) { trackingList = trackingDao.websiteList(pool); for (Tracking item : trackingList) { list.add(item.getWebsite()); } } // render report else if (json.get("type").equals("submit")) { if (json.get("criteria").equals("date")) { // render repoty by date trackingList = trackingDao.getListByDate(pool, json.get("date").toString()); } else if (json.get("criteria").equals("daterange")) { // render repoty by date range trackingList = trackingDao.getListByDateRange( pool, json.get("fromdate").toString(), json.get("todate").toString()); } else if (json.get("criteria").equals("website")) { // render repoty by website String website = (json.get("website") == null ? "" : json.get("website").toString()); trackingList = trackingDao.getListByWebsite(pool, website); } for (Tracking item : trackingList) { JSONObject trackingObj = new JSONObject(); trackingObj.put("date", item.getDate()); trackingObj.put("website", item.getWebsite()); trackingObj.put("visit", item.getVisit()); list.add(trackingObj); } } resultObj.put("result", list); resultObj.writeJSONString(result); // finally output the json string out.print(result.toString()); } catch (ParseException | SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }