コード例 #1
0
  /** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    // On récupère les entrées de l'utilisateur
    String name = request.getParameter("computerName");

    // Dates
    String introducedDate = request.getParameter("introducedDate");
    String discontinuedDate = request.getParameter("discontinuedDate");

    // Contrôle des dates
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    Date newDateInit = null;
    Date newDateDisc = null;
    try {
      newDateInit = df.parse(introducedDate);
      newDateDisc = df.parse(discontinuedDate);

    } catch (ParseException e) {

    }

    // On récupère l'id company renvoyé par le paramètre
    Long company_id = Long.parseLong(request.getParameter("company"));

    // On souhaite récupérer l'objet "Company"
    Company newCompany = monService.getCompany(company_id);

    RequestDispatcher rd;
    // Test de validite des champs nom de l'ordi, dates, et entreprise
    if (name != null
        && !name.trim().isEmpty()
        && introducedDate != null
        && !introducedDate.trim().isEmpty()
        && discontinuedDate != null
        && !discontinuedDate.trim().isEmpty()) {
      monService.addComputer(
          new Computer.Builder()
              .name(name)
              .introduced(newDateInit)
              .discontinued(newDateDisc)
              .companie(newCompany)
              .build());

      // Redirection vers la page principale
      rd = getServletContext().getRequestDispatcher(response.encodeURL("/index.jsp"));
      rd.forward(request, response);
    } else {
      // On reste sur la même page
      doGet(request, response);
    }
  }
コード例 #2
0
  /** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    // On récupère les ordinateurs
    request.setAttribute("companies", monService.getCompanies());

    // Affichage
    RequestDispatcher rd =
        getServletContext().getRequestDispatcher(response.encodeURL("/WEB-INF/addComputer.jsp"));
    rd.forward(request, response);
  }