title: Java Servlet Switches execution order
body: <p>I'm having a few problems with two switches from a servlet. I'm trying to load data from those switches on a jsp but it basically just gets on the first switch and the second one isn't executed. I've tried creating only one switch but it will give me errors like <code>Cannot forward after response has been committed</code> and so on. Do you guys know what is the correct approach for this problem?
  Thanks!</p>
  
  <p>This is the code:</p>
  
  <pre><code>public class UserControllerServlet extends HttpServlet {
  
      private static final long serialVersionUID = 1L;
  
      private UserDao userDao;
      private ReservationDao reservationDao;
      private DBConnection dbConnection;
  
  
      @Override
      public void init() throws ServletException {
          super.init();
  
          try {
              userDao = new UserDao(dbConnection);
              reservationDao = new ReservationDao(dbConnection);
          }catch(Exception e) {
              throw new ServletException(e);
          }
      }
  
  
  
  
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
      {
  
          try {
              // read the "command" parameter
              String theCommand = request.getParameter("command");
              String theCommand2 = request.getParameter("command2");
              if(theCommand == null) {
                  theCommand = "LIST";
              }
  
              if(theCommand2 == null) {
                  theCommand2 = "LIST";
              }
  
              switch(theCommand2) {
              case "LIST":
                  listReservations(request, response);
                  return;
  
              case "ADD":
                  addReservation(request, response);
                  return;
  
  
              case "LOAD":
                  loadReservation(request, response);
                  return;
  
  
              case "UPDATE":
                  updateReservation(request, response);
                  return;
  
  
  
              case "DELETE":
                  deleteReservation(request, response);
                  return;
  
  
              default:
                  listReservations(request, response);
          }
  
              //route to the appropriate method
              switch(theCommand) {
                  case "LIST":
                      listUsers(request, response);
                      break;
  
                  case "ADD":
                      addUser(request, response);
                      break;
  
  
                  case "LOAD":
                      loadUser(request, response);
                      break;
  
  
                  case "UPDATE":
                      updateUser(request, response);
                      break;
  
  
  
                  case "DELETE":
                      deleteUser(request, response);
                      break;
  
  
                  default:
                      listUsers(request, response);
              }
  
          }catch(Exception e) {
              throw new ServletException(e);
          }
  
      }
  
      private void deleteUser(HttpServletRequest request, HttpServletResponse response) 
      throws Exception {
          String theUserId = request.getParameter("userId");
  
          userDao.deleteUser(theUserId);
  
          listUsers(request, response);
      }
  
      private void updateUser(HttpServletRequest request, HttpServletResponse response) 
              throws Exception {
  
          int id = Integer.parseInt(request.getParameter("userId"));
          String username = request.getParameter("username");
          String password = request.getParameter("password");
          String role = request.getParameter("role");
          String nume = request.getParameter("nume");
          String prenume = request.getParameter("prenume");
          String email = request.getParameter("email");
          String adresa = request.getParameter("adresa");
          UserBean theUser = new UserBean(id, username, password, role,nume,prenume,email,adresa);
          userDao.updateUser(theUser);
          listUsers(request, response);
      }
  
      private void loadUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
          String theUserId = request.getParameter("userId");
          UserBean theUser = userDao.getUser(theUserId);
          request.setAttribute("THE_USER", theUser);
          RequestDispatcher dispatcher = 
                  request.getRequestDispatcher("/update-user-form.jsp");
          dispatcher.forward(request, response);
          return;
      }
  
      private void addUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
  
          String username = request.getParameter("username");
          String password = request.getParameter("password");
          String role = request.getParameter("role");
          String nume = request.getParameter("nume");
          String prenume = request.getParameter("prenume");
          String email = request.getParameter("email");
          String adresa = request.getParameter("adresa");
          UserBean theUser = new UserBean(username, password, role,nume,prenume,email,adresa);
          userDao.addUser(theUser);
          listUsers(request, response);
  
      }
  
      private void listUsers(HttpServletRequest request, HttpServletResponse response) 
      throws Exception {
          List&lt;UserBean&gt; users = userDao.getUsers();
          request.setAttribute("USER_LIST", users);
          RequestDispatcher dispatcher = request.getRequestDispatcher("/list-users.jsp");
          dispatcher.forward(request, response);
          return;
      }
  
  
      //Reservations Methods
  
      private void deleteReservation(HttpServletRequest request, HttpServletResponse response) 
              throws Exception {
                  String theReservationId = request.getParameter("reservationId");
                  reservationDao.deleteReservation(theReservationId);
                  listReservations(request, response);
              }
  
              private void updateReservation(HttpServletRequest request, HttpServletResponse response) 
                      throws Exception {
  
                  int idReservation = Integer.parseInt(request.getParameter("reservationId"));
                  int idUser = Integer.parseInt(request.getParameter("userId"));
                  String dataCheckin = request.getParameter("dataCheckin");
                  String dataCheckout = request.getParameter("dataCheckout");
                  int nrPersoane = Integer.parseInt(request.getParameter("nrPersoane"));
                  int nrCamere = Integer.parseInt(request.getParameter("nrCamere"));
                  ReservationBean theReservation = new ReservationBean(idReservation, idUser, dataCheckin, dataCheckout,nrPersoane,nrCamere);
                  reservationDao.updateReservation(theReservation);
                  listReservations(request, response);
              }
  
              private void loadReservation(HttpServletRequest request, HttpServletResponse response) throws Exception {
                  String theReservationId = request.getParameter("reservationId");
                  ReservationBean theReservation = reservationDao.getReservation(theReservationId);
                  request.setAttribute("THE_RESERVATION", theReservation);
                  RequestDispatcher dispatcher = 
                          request.getRequestDispatcher("/update-reservation-form.jsp");
                  dispatcher.forward(request, response);
                  return;
              }
  
              private void addReservation(HttpServletRequest request, HttpServletResponse response) throws Exception {
  
                  int userId = Integer.parseInt(request.getParameter("userId"));
                  String dataCheckin = request.getParameter("dataCheckin");
                  String dataCheckout = request.getParameter("dataCheckout");
                  int nrPersoane = Integer.parseInt(request.getParameter("nrPersoane"));
                  int nrCamere = Integer.parseInt(request.getParameter("nrCamere"));
                  ReservationBean theReservation = new ReservationBean(userId, dataCheckin, dataCheckout,nrPersoane,nrCamere);
                  reservationDao.addReservation(theReservation);
                  listReservations(request, response);
  
              }
  
              private void listReservations(HttpServletRequest request, HttpServletResponse response) 
                  throws Exception {
                      List&lt;ReservationBean&gt; reservations = reservationDao.getReservations();
                      request.setAttribute("RESERVATION_LIST", reservations);
                       request.getRequestDispatcher("/list-users.jsp").forward(request, response);
                      return;
                  }
  
  
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          doGet(request, response);
      }
  
  }
  </code></pre>