title: How to get select value in a JSP to a servlet ? JAVAEE
body: <p>first pic here after month reading posts to help me so, kinda excited !  :D
  I'm currently learning JavaEE in scholl and have a problem to get values from a select box in my JSP file.</p>
  
  <p>Here is what I want to do : get the value from the select box when I click "add to card", put that in a list (in the servlet) et show that list back in my jsp file. I give you the code I have for now (probably kin of ugly, sorry, first days...)</p>
  
  <p>Servlet :</p>
  
  <pre><code>public class CommandeServlet extends HttpServlet {
  
      private List listePlatsCommandes = new ArrayList();
  
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          doWork(request, response);
      }
  
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          doWork(request, response);
      }
  
      private void doWork(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          CommandeModel beanSessionCommande = (CommandeModel) request.getSession().getAttribute("beanSessionCommande");
  
  
  
  
          if (request.getParameterMap().isEmpty()) {
              beanSessionCommande = new CommandeModel();
          } else if (request.getParameter("submit") != null) {
  
              listePlatsCommandes.add(request.getParameter("platCommande"));
              Commande com = new Commande(request.getParameter("libelle"), listePlatsCommandes);
  
              CommandeManagerSingl.getInstance().ajouter(com);
  
              beanSessionCommande.setCommande(com);
  
  
          }
          System.out.println(listePlatsCommandes);
          request.getSession().setAttribute("beanSessionCommande", beanSessionCommande);
          request.getRequestDispatcher("/WEB-INF/commande.jsp").forward(request, response);
      }
  }
  </code></pre>
  
  <p>Model : </p>
  
  <pre><code>public class CommandeModel {
  
      private List&lt;Plat&gt; plats;
      private Commande commande;
  
      public CommandeModel() {
  
          this.plats = new ArrayList&lt;Plat&gt;();
          this.commande = new Commande();
      }
  
      public List&lt;Plat&gt; getPlats() {
          return plats;
      }
  
      public void addPlat(Plat plat) {
          plats.add(plat);
      }
  
      public void setPlats(List&lt;Plat&gt; plats) {
          this.plats = plats;
      }
  
      public Commande getCommande() {
          return commande;
      }
  
      public void setCommande(Commande commande) {
          this.commande = commande;
      }
  }
  </code></pre>
  
  <p>JSP file :</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
           pageEncoding="ISO-8859-1" %&gt;
  &lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt;
  &lt;%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Commande&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;a href="/PlatServlet"&gt;Plats&lt;/a&gt; - &lt;a href="/CommandeServlet"&gt;Commandes&lt;/a&gt;
  &lt;h1&gt;Gestion des commandes&lt;/h1&gt;
  
  &lt;div&gt;
      &lt;form action="CommandeServlet"&gt;
  
          &lt;select name="listePlats"&gt;
  
              &lt;c:forEach var="plat" items="${beanSessionPlat.listePlats}"&gt;
                  &lt;option name="platCommande" value=" ${plat.libelle} " selected="selected"&gt; ${plat.libelle} &lt;/option&gt;
              &lt;/c:forEach&gt;
  
          &lt;/select&gt;
  
          &lt;input type="submit" value="Add to cart" name="submit"&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  
  &lt;div&gt;
      &lt;!-- ICI LA LISTE DE LA COMMANDE --&gt;
  &lt;h3&gt;Résumé de la commande :&lt;/h3&gt; &lt;br&gt;&lt;br&gt;
      &lt;table border="1"&gt;
  
          &lt;c:forEach var="com" items="${beanSessionCommande.plats}"&gt;
              &lt;tr&gt;
                  &lt;td&gt; ${com} &lt;/td&gt;
              &lt;/tr&gt;
          &lt;/c:forEach&gt;
      &lt;/table&gt;
  &lt;/div&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Forgot to say : everything is in Session for now, and i get the select list filled up with a list i get from another JSP file</p>
  
  <p>Thanks a lot for helping :)</p>