title: How to retrieve multiple values from servlet in jsp using AJAX
body: <p>I have this <code>ajax</code> javascript code that calls a <code>servlet</code> to retrieve two values (firstname, telephone). I know how to get a single value but not multiple values from the servlet.</p>
  
  <p>Here's my <code>ajax</code></p>
  
  <pre><code>    &lt;script&gt;
          function getCustomerDetailsAjax(str) {
              str = $('#customerId').val();
  
              if (document.getElementById('customerId').value &lt;= 0) {
                  document.getElementById('firstName').value = " ";
                  document.getElementById('telephone').value = " ";
                  document.getElementById('vehicleMake').value = " ";
                  document.getElementById('vehicleModel').value = " ";
                  document.getElementById('vehicleColor').value = " ";
              } else {
                  $.ajax({
                      url: "GetCustomerDetails",
                      type: 'POST',
                      data: {customerId: str},
                      success: function (data) {                       
                          alert(data); //I want to get 2 servlet values and alert them here. How can I do that?
                      }
                  });
              }
          }
      &lt;/script&gt;
  </code></pre>
  
  <p>And this is my <code>servlet</code></p>
  
  <pre><code>public class GetCustomerDetails extends HttpServlet {
  
  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
      PrintWriter out=response.getWriter();
      int customerId = Integer.valueOf(request.getParameter("customerId"));
      try {
          Class.forName("com.mysql.jdbc.Driver");
          Connection con = DriverManager.getConnection("jdbc:mysql://localhost/Vehicle", "root", "");
          PreparedStatement ps = con.prepareStatement("SELECT fistname,telephone FROM customers WHERE customerid=?");
          ps.setInt(1, customerId);
          ResultSet result=ps.executeQuery();
          if(result.next()){
              out.print(result.getString("firstname")); //I want to send this value
              out.print(result.getString("telephone")); //and this value
  
          }
  
      } catch (ClassNotFoundException ex) {
          Logger.getLogger(GetCustomerDetails.class.getName()).log(Level.SEVERE, null, ex);
      } catch (SQLException ex) {
          Logger.getLogger(GetCustomerDetails.class.getName()).log(Level.SEVERE, null, ex);
      }
  
  }
  
  @Override
  public String getServletInfo() {
      return "Short description";
  }// &lt;/editor-fold&gt;
  </code></pre>
  
  <p>}</p>
  
  <p>This is the part that retrieves the data from the <code>servlet</code>,how to get multiple values from it and alert?</p>
  
  <pre><code>       success: function (data) {                       
              alert(data); //I want to get 2 servlet values and alert them here. How can I do that?
         }
  </code></pre>
  
  <p>Thank you! </p>