title: How to get values from JSP to Servlet in <option>
body: <p>I'm writing a jsp file code that creates dropdown menus dynamically; the entries on the menus are dinamically inserted after a query executed in <code>dao.QueriesDAO</code> java class. Additionally, there is a search bar.</p>
  
  <p>I want all the selected voices from the menus, plus the string inserted in the search bar, are sent to the servlet <code>SearchServelt.java</code>, contained in <code>src/controller/SearchServlet.java</code>, after the <code>Search</code> button it's clicked.</p>
  
  <p>JSP file (in <code>WebContent/jsp/homeView.jsp</code>):</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.util.List, java.util.Iterator" %&gt;
  
  &lt;!DOCTYPE html&gt;
  
  &lt;html&gt;
  
      &lt;head&gt;&lt;/head&gt;
  
      &lt;body&gt;
  
          &lt;jsp:include page="_header.jsp"&gt;&lt;/jsp:include&gt;
          &lt;jsp:include page="_menu.jsp"&gt;&lt;/jsp:include&gt;
  
          &lt;div style = "text-align: center"&gt;
  
              &lt;form action="/Search" method="post"&gt;
  
              Search &lt;input name="search"&gt; &lt;input type="submit" value="Search"/&gt;
  
              &lt;/form&gt;
  
          &lt;/div&gt;
  
          &lt;div style = "text-align: center"&gt;
  
              &lt;%-- select value brand from drop-downlist --%&gt;
              &lt;div style = "display: inline-block"&gt;
              &lt;%
                  List&lt;String&gt; brands = dao.QueriesDAO.getBrands();
                  Iterator&lt;String&gt; iterBrands = brands.iterator();
              %&gt;
              &lt;form name="f1" method="post" action="/Search"&gt;
                  Select brand:
                  &lt;select name="brand"&gt;
                      &lt;option value="All"&gt;All&lt;/option&gt;
                      &lt;%  while(iterBrands.hasNext()){ %&gt;
                      &lt;option&gt;&lt;%= (String) iterBrands.next() %&gt;&lt;/option&gt;
                       &lt;% } %&gt;
                  &lt;/select&gt;
              &lt;/form&gt;
              &lt;/div&gt;
  
              &lt;%-- select value of instrument type from drop-downlist --%&gt;
              &lt;div style = "display: inline-block"&gt;
              &lt;%
                  List&lt;String&gt; instrumentTypes = dao.QueriesDAO.getInstrumentType();
                  Iterator&lt;String&gt; iterInstrumentTypes = instrumentTypes.iterator();
              %&gt;
              &lt;form name="f2" method="post" action="/Search"&gt;
                  Select instrument type:
                  &lt;select name="instrumentType"&gt;
                      &lt;option value="All"&gt;All&lt;/option&gt;
                      &lt;%  while(iterInstrumentTypes.hasNext()){ %&gt;
                      &lt;option&gt;&lt;%= (String) iterInstrumentTypes.next() %&gt;&lt;/option&gt;
                       &lt;% } %&gt;
                  &lt;/select&gt;
              &lt;/form&gt;
              &lt;/div&gt;
  
              &lt;%-- select value used from drop-downlist --%&gt;
              &lt;div style = "display: inline-block"&gt;
              &lt;form name="f3" method="post" action="/Search"&gt;
                  Select used status:
                  &lt;select name="used"&gt;
                      &lt;option value="0"&gt;All&lt;/option&gt;
                      &lt;option value="false"&gt;Not used&lt;/option&gt;
                      &lt;option value="true"&gt;used&lt;/option&gt;
                  &lt;/select&gt;
              &lt;/form&gt;
              &lt;/div&gt;
  
              &lt;%-- select value product type from drop-downlist --%&gt;
              &lt;div style = "display: inline-block"&gt;
              &lt;form name="f4" method="post" action="/Search"&gt;
                  Select product type:
                  &lt;select name="productType"&gt;
                      &lt;option value="All"&gt;All&lt;/option&gt;
                      &lt;option value="2"&gt;Professional product&lt;/option&gt;
                      &lt;option value="1"&gt;Scholastic product&lt;/option&gt;
                      &lt;option value="0"&gt;Classic product&lt;/option&gt;
                  &lt;/select&gt;
              &lt;/form&gt;
              &lt;/div&gt;
  
          &lt;/div&gt;
  
          &lt;jsp:include page="_footer.jsp"&gt;&lt;/jsp:include&gt;
  
      &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>Servlet file:</p>
  
  <pre><code>package controller;
  
  import java.io.IOException;
  
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  @WebServlet(urlPatterns = { "/search"})
  public class SearchServlet extends HttpServlet {
  
      private static final long serialVersionUID = -1953084286713579746L;
  
      public SearchServlet() {
          super();
      }
  
      @Override
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
  
          String searchParameters= request.getParameter("search"); 
  
          String brandSelected= request.getParameter("brand"); 
          String selectedInstrumentType= request.getParameter("instrumentType"); 
          String selectedUsedStatus= request.getParameter("used"); 
          String selectedProductType= request.getParameter("productType");
  
          System.out.println("Inserted: " + searchParameters + ", "
                              + brandSelected + ", "
                              + selectedInstrumentType + ", "
                              + selectedUsedStatus + ", "
                              + selectedProductType + ".");
  
  
      }
  
  }
  </code></pre>
  
  <p>I want to be able to work with the values from the servlet and then eventually call other java methods and/or jsp files.</p>
  
  <p>I don't know what is wrong, because I've seen similar questions on stackoverflow and I utilized the solution proposed.</p>
  
  <p>I'll like to know what do I do of wrong and what should be a good approach to a problem like this, thank you very much.</p>
  
  <p>The <code>homeView.jsp</code> file is called from a different servlet, <code>HomeServlet.java</code>. Should I use that servlet instead of a new servlet <code>SearchServlet.java</code>? What is better?</p>
  
  <p>EDIT:</p>
  
  <p>I resolved with <code>&lt;form action="${pageContext.request.contextPath}/search" method="get"&gt;</code> in the JSP page (and modified in having a single <code>form</code>), and accordingly I changed the <code>SearchServlet.java</code> method from <code>doPost</code> to <code>doGet</code>.</p>