Exemplo n.º 1
0
  /** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String prenom = request.getParameter("prenom");
    String pseudo = request.getParameter("pseudo");
    String mdp = request.getParameter("mdp");

    Map<String, Object> config = new HashMap<String, Object>();
    config.put("javax.json.stream.JsonGenerator.prettyPrinting", Boolean.valueOf(true));
    JsonBuilderFactory factory = Json.createBuilderFactory(config);
    JsonObject value;

    try {
      gestionJoueurs.creerJoueur(prenom, pseudo, mdp);
    } catch (JoueurDejaExistantException e) {
      value =
          factory.createObjectBuilder().add("success", "0").add("message", e.getMessage()).build();
      response.setContentType("application/json");
      response.getWriter().write(value.toString());
      return;
    }

    HttpSession session = request.getSession();
    Joueur nouveauJoueur = new Joueur(prenom, pseudo, mdp);

    session.setAttribute("joueur", nouveauJoueur);

    value =
        factory
            .createObjectBuilder()
            .add("success", "1")
            .add("message", "Inscription avec succès!")
            .build();
    response.setContentType("application/json");
    response.getWriter().write(value.toString());
  }
 private final JsonRestRequestHandlerResult createSuccessResult() {
   final JsonObjectBuilder jsonBuilder = jsonBuilderFactory.createObjectBuilder();
   jsonBuilder.add(JsonRestServer.JSON_RESPONSE_REASON_PHRASE_NVNAME, "Echo completed");
   final JsonObject httpResponseContent = jsonBuilder.build();
   return new JsonRestRequestHandlerResult(HttpResponseStatus.OK, httpResponseContent, "en", null);
 }