title: Access java <List> from from jsp / jstl MVC app
body: <p>I have been finding it difficult to understand where I am going wrong.  I understand that we should move with the times but I am not sure how to replace a scriptlet, that I can get to do the job, with the JSTL taglibrary functions.</p>
  
  <p>I have a Model class called Ranges.class which contains a rangeName (String) and a startRange (BigDecimal) and an endRange (BigDecimal) value.  I have a DAO class that handles all the db queries (crud type methods).  I have a method in RangesDao.class called getAllRanges() which will get all the potential ranges from a mysql db and return a List.  This will fetch all the Range objects which include the rangeName, startRange and endRange.</p>
  
  <p><strong>Q:</strong> <em>So basically what I want to do is from my jsp page when a text input is selected I want to check if it is between the start and end value of each Range object (that was returned in the List) and when I have a match I want to update a different text input with that objects rangeName value.</em></p>
  
  <p>This is what I have so far.</p>
  
  <p><strong>Range.class</strong></p>
  
  <pre><code>package za.co.zs6erb.model;
  import java.math.BigDecimal;
  
  public class Range {
  
      private int range_id;
      private String rangeName;
      private BigDecimal startRange;
      private BigDecimal endRange;
  
      //...getters and setters for each of the above variables ...
  
      @Override
      public String toString() {
          return "Ranges [range_id=" + range_id + ", Range Name=" + rangeName + ", Start Range=" + startRange  
          + ", End Range=" + endRangeBand + "]";
      } 
  }
  </code></pre>
  
  <p><strong>DAO Class:</strong> This is the getAllRanges() method</p>
  
  <pre><code>public List&lt;Range&gt; getAllRanges() {
      List&lt;Range&gt; rangeList = new ArrayList&lt;Range&gt;();
      try {
          Statement statement = connection.createStatement();
          ResultSet rs = statement.executeQuery("select * from ranges order by range_id");
          while (rs.next()) {
              Range lRange = new Range();
              lRange.setID(rs.getInt("range_id"));
              lRange.setRangeName(rs.getString("rangeName"));
              lRange.setStartRange(rs.getBigDecimal("start_range"));
              lRange.setEndRange(rs.getBigDecimal("end_range"));
              rangeList.add(lRange);
          }
      } catch (SQLException e) {
          e.printStackTrace();
      }
      return rangeList;
  }
  </code></pre>
  
  <p><strong>Controller:</strong>  This class has a doGet() and a doPost() method the piece I am busy with is the doGet().  The doPost() is used when adding a "plannedRoute" object (just an object that has several other attributes that need to be added to a different table. - not the issue here)  From the doGet() I list all the "plannedRoute" objects which all have one Range associated with each.  When adding a new route I launch a jsp page from the doGet() method.  The following part should make things a little clearer.</p>
  
  <p><strong>doGet():</strong></p>
  
  <pre><code>private static String LIST_PLANNEDROUTES = "plannedroutes.jsp";
  private RangeDao rDao;    
  
  //Constructor
  public PlannedRouteController() {
      super();
      rDao = new RangeDao();
  }
  
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      String forward = "";
      String action = request.getParameter("action");
  
      if (action.equalsIgnoreCase("new")) {
          forward = LIST_PLANNEDROUTES;
          request.setAttribute("rd", rDao);
      }
      RequestDispatcher view = request.getRequestDispatcher(forward);
      view.forward(request, response);
  }
  </code></pre>
  
  <p>So now we have the crux of the issue ...
  <strong>plannedRoutes.jsp</strong></p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%&gt;
  &lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%&gt;
  &lt;%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%&gt;
  
  &lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
      &lt;head&gt;
          &lt;meta charset="utf-8"&gt;
          &lt;title&gt;MyTest&lt;/title&gt;
          &lt;script src="sj/jquery.js"&gt;&lt;/script&gt;
          &lt;script type="text/javascript"&gt;
              //&lt;!--
              $(document).ready(function(){
                  $("#range").change(function() {
  
                  alert("A Range was entered");
  
                  &lt;c:forEach var="entry" items="${rd}"&gt;
                      alert( "HERE&gt;" + ${entry.rangeName});
                  &lt;/c:forEach&gt;
  
                  document.getElementById("testName").value =  $("#range").val();
              });                
          });
          //--&gt;
          &lt;/script&gt;
      &lt;/HEAD&gt;
      &lt;BODY&gt;
          &lt;form method="POST" action='&lt;form method="POST" action='ContactController' name="frmAddContact"&gt;' name="frmAddRoute"&gt;
              Range: &lt;input type="text" id="range" name="range" /&gt;
              Name:  &lt;input type="text" id="testName" name="testName" readonly /&gt;
          &lt;!-- a whole bunch of other stuff here --&gt;
  
          &lt;/form&gt;
      &lt;/BODY&gt;
  &lt;/HTML&gt;
  </code></pre>
  
  <p><strong>Q:</strong> So when the value in the range input text field changes (I call the method in the  tag and there I want to step through the List and match what was typed in to see if it falls between the start and end range of any of the Range Objects.  When I find a match I want to be able to fetch the rangeName attribute of that object and add that to the testName input text area.</p>
  
  <p>I hope this is clear enough.  I have tried the  without success and am not sure where to go from here without using scriptlets ...  Any help would be appreciated.</p>
  
  <p>Kind Regards
  Sean</p>