title: Sort select box items in a JSP scriptlet
body: <p>I'm working on a legacy Tomcat server that is no longer supported by the software developer that provided it.  web.xml doesn't tell me what version of JSP I'm working with, but all the .class files are Java 1.5.</p>
  
  <p>I have a customer who is upset, because the system has html  boxes that are auto-populated by Java, and it fills it straight the the (unordered) results of an XML database query.  I'm trying to sneak a bit of code into the JSP for the Selector to sort the list of fields before they are populated, but it doesn't work and I don't understand why.</p>
  
  <p>The (abridged) relevant code:</p>
  
  <pre><code>&lt;%@ page import="src.explorer.ObjectStateFactory"%&gt;
  &lt;%@ page language="java" %&gt;
  &lt;%@ taglib uri="/WEB-INF/c.tld" prefix="c" %&gt;
  
  
  &lt;jsp:useBean id="ExplorerViewContext" scope="session" type="src.explorer.ExplorerViewContext"/&gt;
  
  ...
  
  &lt;c:forEach var="nc" items="${ExplorerViewContext.networkControllers}"&gt;
      &lt;c:choose&gt;
          &lt;c:when test="${nc.name == ExplorerViewContext.networkControllerSelection.name}"&gt;
              &lt;option value="&lt;c:out value="${selectAction}${nc.objectKey}"/&gt;" selected&gt;&lt;c:out value="${nc.name}"/&gt;&lt;/option&gt;
          &lt;/c:when&gt;
          &lt;c:otherwise&gt;
              &lt;option value="&lt;c:out value="${selectAction}${nc.objectKey}"/&gt;"&gt;&lt;c:out value="${nc.name}"/&gt;&lt;/option&gt;
          &lt;/c:otherwise&gt;
      &lt;/c:choose&gt;
  &lt;/c:forEach&gt;    
          &lt;/select&gt;
  </code></pre>
  
  <p>The relevant code with my additions:   </p>
  
  <pre><code> &lt;%@ page import="src.explorer.ObjectStateFactory"%&gt;
      &lt;%@ page import="java.util.*"%&gt;
      &lt;%@ page language="java" %&gt;
      &lt;%@ taglib uri="/WEB-INF/c.tld" prefix="c" %&gt;
  
  
      &lt;jsp:useBean id="ExplorerViewContext" scope="session" type="src.explorer.ExplorerViewContext"/&gt;
  
      ...
  
     &lt;%
      final Comparator&lt;src.explorer.XmldbObjectState&gt; NC_ORDER = new Comparator&lt;src.explorer.XmldbObjectState&gt;(){
          public int compare(src.explorer.XmldbObjectState  nc1, src.explorer.XmldbObjectState nc2){
              return nc1.getName().compareTo(nc2.getName());
          }
      };
  
      List myList = ExplorerViewContext.getNetworkControllers();
      java.util.Collections.sort(myList,NC_ORDER);
      %&gt;
      &lt;c:forEach var="nc" items="${myList}"&gt;
          &lt;c:choose&gt;
              &lt;c:when test="${nc.name == ExplorerViewContext.networkControllerSelection.name}"&gt;
                  &lt;option value="&lt;c:out value="${selectAction}${nc.objectKey}"/&gt;" selected&gt;&lt;c:out value="${nc.name}"/&gt;&lt;/option&gt;
              &lt;/c:when&gt;
              &lt;c:otherwise&gt;
                  &lt;option value="&lt;c:out value="${selectAction}${nc.objectKey}"/&gt;"&gt;&lt;c:out value="${nc.name}"/&gt;&lt;/option&gt;
              &lt;/c:otherwise&gt;
          &lt;/c:choose&gt;
      &lt;/c:forEach&gt;    
              &lt;/select&gt;
  </code></pre>
  
  <p>Basically, I'm trying to grab the list and sort it before it gets sent to the HTML.  The problem is, myList always comes up empty, and I don't understand why.  I'm guessing that ExplorerViewContext.networkControllers in the original code is calling the getNetwrokControllers() method on an instance of ExplorerViewContext, yes?  Why can't I do the same thing in a scriptlet and reformat the output a little before serving it?</p>