title: JSTL Pagination for Map inside Map
body: <p>I need to Implement simple pagination displaying 100 records per page and pagination data is sorted by date.Though I have sorted the information correctly,however I am unable to think about pagination logic since resultset is inside map which is again holding Map inside it,</p>
  
  <p>Below is my JSTL Code along with definition for TitleList/Title which I am iterating after adding resultset data in it.</p>
  
  <pre><code> Map&lt;Date, LinkedHashMap&lt;Integer, String&gt;&gt; TitleList = new LinkedHashMap&lt;Date, LinkedHashMap&lt;Integer, String&gt;&gt;();
  
  LinkedHashMap&lt;Integer, String&gt; Title = new LinkedHashMap&lt;Integer, String&gt;();
  
  &lt;c:forEach items="${TitleList}" var="column"&gt; 
              &lt;p&gt;&lt;c:out value="${column.key}"/&gt;&lt;/p&gt;                      
              &lt;c:forEach var="Title" items="${column.value}"&gt;
                  &lt;p&gt;&lt;a href="&lt;%=request.getContextPath()%&gt;/Test.do?id=${Title.key}"&gt;&lt;c:out value="${Title.value}"/&gt;&lt;/a&gt;&lt;/p&gt;
              &lt;/c:forEach&gt;
          &lt;/c:forEach&gt; 
  </code></pre>
  
  <p>Though I can paginate it by adding resultset directly to list ,however that will kill by sorting order and functionality I am looking for.Somehow for above code, I need to restrict the 100 records per page and then implement Next button which will again display 100 records.
  Appreciate some thoughts on this.</p>
  
  <p>Updated:Adding more information,this is how map looks like from servlet where I am creating titlelist and title maps,</p>
  
  <pre><code>map = {2012-07-12={160=tstng, 159=testing}, 2012-07-10={158=test, 157=test, 156=Testing, 155=Testing, 154=Testing, 153=Testing, 152=Testing, 151=Testing, 150=Testing, 149=Testing, 148=Testing, 147=Testing, 146=Testing, 145=Testing, 144=Testing, 143=Testing, 142=Testing, 141=Teasting, 140=Teasting, 139=Testing, 138=Testing, 137=Testing , 136=Testing for testing, 135=Testing for testing, 134=Testing for Test, 133=Testing}, 2012-07-04={132=Testing for Broker}, 2012-07-03={131=Testing for Post, 130=1200 TESTIN}, 2012-07-01={129=testing}, 2012-03-30={128=upload test}, 2012-03-28={127=test}, 2012-01-08={126=1BHK flat for 1000$ at Journal Square}, 2012-01-04={125=tseting by kiran, 124=tseting by kiran, 123=tseting by kiran, 122=tseting by kiran, 121=tseting by kiran, 120=tseting by kiran, 119=tseting by kiran, 118=tseting by kiran, 117=tseting by kiran, 116=tseting by kiran, 115=tseting by kiran, 114=tseting by kiran, 113=tseting by kiran, 112=tseting by kiran, 111=tseting by kiran, 110=tseting by kiran, 109=tseting by kiran, 108=tseting by kiran, 107=tseting by kiran, 106=tseting by kiran, 105=tseting by kiran, 104=tseting by kiran}, 2012-01-02={103=testing, 102=testing, 101=testing, 100=testing, 99=testing, 98=testing, 97=testing, 96=testing, 95=testing, 94=testing, 93=testing, 92=testing, 91=testing, 90=testing, 89=testing, 88=testing, 87=testing, 86=testing, 85=testing, 84=testing, 83=testing, 82=testing, 81=testing, 80=testing, 79=testing, 78=testing, 77=testing, 76=testing, 75=testing, 74=testing}, 2011-12-09={73=The monthly consumer price index rose 4.2% from the year-ago period, reflecting a huge easing from Octoberâ??s 5.5% increase. }, 2011-12-08={72=Testing for dispplay}, 2011-11-25={71=Apartment for Rent at Journal Square for 1000$.Please see now., 70=Apartment for Rent at Journal Square for 1000$.Please see now., 69=testing, 68=AS, 67=A, 66=A, 65=q, 64=q, 63=Testing, 62=Testing}, 2011-11-24={61=testing, 60=tEST, 59=TESTING, 58=TESTING, 57=Testing, 56=Testing, 55=Testing, 54=tseting, 53=testing, 52=testing, 51=testing, 50=teting, 49=tESTING, 48=tESTING, 47=tESTING, 46=Testing, 45=TESTING, 44=TESTING, 43=tESTING}, 2011-11-23={42=Testing, 41=testing, 40=Testing, 39=tESTING, 38=TETING, 37=testing}, 2011-11-22={36=Testing, 35=Testing, 34=Testing, 33=Testing by Kiran, 32=testing, 31=Testing, 30=Testing by Kiran, 29=Testing byt kiran, 28=Testing by Kiran, 27=testing by kiran, 26=testing by kiran, 25=Testing , 24=TSETING, 23=Testing by Kiran}, 2011-11-21={22=testing, 21=Testng, 20=TESTING, 19=TESTING, 18=ESTING, 17=ESTING, 16=ESTING, 15=ESTING, 14=testing by kiran, 13=testing by Kiran, 12=Testing by Kiran, 11=testing by Kiran, 10=Testing by Kian, 9=Testing by Kiran, 8=Testing by Kiran, 7=Testing byKiran, 6=testing, 5=Test by Kiran, 4=Test by Kiran}, 2011-11-20={3=testing, 2=testing, 1=testing}}
  </code></pre>
  
  <p>Below is servlet code I which will create map after reading from DB,</p>
  
  <pre><code> Map&lt;Date, LinkedHashMap&lt;Integer, String&gt;&gt; TitleList = new LinkedHashMap&lt;Date, LinkedHashMap&lt;Integer, String&gt;&gt;();
              String sqld = "Select Distinct DATE(timestamp) as DATE from table ORDER BY DATE DESC LIMIT 730 ;";
              ResultSet rsd = null;
              try {
                  rsd = stmt1.executeQuery(sqld);
              } catch (SQLException ex) {
                  Logger.getLogger(myclass.class.getName()).log(Level.SEVERE, null, ex);
              }
              try {
                  while (rsd.next()) {
                      LinkedHashMap&lt;Integer, String&gt; Title = new LinkedHashMap&lt;Integer, String&gt;();
              //        System.out.println("Testing");
                      Date Date = rsd.getDate("Date");
              //        System.out.println(Date);
                      String sql = "Select title,id from table Where DATE(timestamp) = '" + Date + "'ORDER BY id DESC";
             //         System.out.println(sql);
                      ResultSet rs = stmt.executeQuery(sql);
                      rs.last();
                      int numRows = rs.getRow();
            //          System.out.println(rs.getRow());
                      rs.beforeFirst();
                      while (rs.next()) {                                  
                          for (int i = 1; i &lt;= numRows; i++) {
                              String title = rs.getString("title");
                              Integer id = rs.getInt("id");
                                 Title.put(id, title);                         
                              TitleList.put(Date, Title);
                          }
                          System.out.println("Size of LinkedHashMap for Title  : " + Title.size());
           //               System.out.println(rs.getString("title"));
                      }
  
                  }
  </code></pre>
  
  <p>here is something I tried Richz,
  Here is what I am getting in dao class I did something like below,</p>
  
  <pre><code>string query ="select title,id,date(timestamp) as date from table";
  list&lt;TitleList&gt; list = new ArrayList&lt;Titlelist&gt;(); 
  try {
  
   resultset rs = stmt.executeQuery(query);
      while(rs.next()){
       myclass myc = new myclass();
       myc.setcid(rs.getInt("id");
       myc.settitle(rs.getString("title");
       myc.settimestamp(rs.gettimestamp("timestamp");
       list.add(myc);
      }
  </code></pre>
  
  <p>and in jsp</p>
  
  <pre><code>&lt;c:foreach var="Titlelist" items="${Titlelist}"&gt;
    EL for title,id and title
  &lt;/c:foreach&gt;
  With this approach I can do pagination date wise along pages and data looks like below along with pagination
  
  Date10 title10
  Date10 title09
  
  
  
  However I need data in this format,
  Date 10
  title10
  title09
  </code></pre>
  
  <p>I tried doing list of lists but for some reasons I am unable to do it.I am losing relations between columns.My internet search shows me pagination as done above for list.</p>
  
  <p>Updated Details on 31st August,</p>
  
  <p>Ok Richz here is my JSP, I have used exactly the same structure as yours.</p>
  
  <pre><code>&lt;%@page import="java.util.LinkedHashMap"%&gt;
  &lt;%@page import="java.util.Date"%&gt;
  &lt;%@page import="java.util.Map"%&gt;
  &lt;%@page import="java.util.List"%&gt;
  &lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
  &lt;%@ include file="/header.jsp" %&gt;
  &lt;%@ include file="WEB-INF/jspf/navdisplay.jspf" %&gt;
  &lt;%@page import="java.util.ArrayList"%&gt;
  &lt;%@page session="true" %&gt;
  &lt;%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %&gt;
  &lt;%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %&gt;
  &lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %&gt;
  &lt;%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %&gt; 
  
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;DB Test&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;c:set var="dateTotal" scope="session" value="${fn:length(list)}"/&gt;
          &lt;c:set var="currentDate" value="${param.date}"/&gt;
          &lt;c:if test="${empty currentDate or currentDate &lt; 0}"&gt;
              &lt;c:set var="currentDate" value="0"/&gt;
          &lt;/c:if&gt;
          &lt;c:if test="${dateTotal &lt;= currentDate}"&gt;
              &lt;c:set var="currentDate" value="${dateTotal - 1}"/&gt;
          &lt;/c:if&gt;
          &lt;c:set var="perPage" scope="session"  value="10"/&gt;
          &lt;c:set var="pageStart" value="${param.start}"/&gt;
          &lt;c:if test="${empty pageStart or pageStart &lt; 0}"&gt;
              &lt;c:set var="pageStart" value="0"/&gt;
          &lt;/c:if&gt;
          &lt;a href="?date=${currentDate - 1}"&gt;Previous Date&lt;/a&gt;  &lt;a  href="?date=${currentDate + 1}"&gt;Next Date&lt;/a&gt;&lt;br/&gt;
          Titles &lt;a href="?start=${pageStart - perPage}"&gt;&lt;&lt;&lt;/a&gt;${pageStart + 1} - ${pageStart + perPage} &lt;a  href="?start=${pageStart + perPage}"&gt;&gt;&gt;&lt;/a&gt;
          &lt;c:forEach items="${list[currentDate]}" var="item" &gt; 
              &lt;p&gt;&lt;c:out value="${item.key}"/&gt;&lt;/p&gt; 
              &lt;c:forEach var="Title" items="${item.value}" begin="${pageStart}" end="${pageStart + perPage - 1}" &gt;
                  &lt;p&gt;&lt;a href="&lt;%=request.getContextPath()%&gt;/Test.do?id=${Title.key}"&gt;&lt;c:out value="${Title.value}"/&gt;&lt;/a&gt;&lt;/p&gt;   
              &lt;/c:forEach&gt;                  
          &lt;/c:forEach&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Below is servlet which is generating data for list,</p>
  
  <p>List list = new ArrayList();</p>
  
  <pre><code>    Map&lt;Date, LinkedHashMap&lt;Integer, String&gt;&gt; TitleList = new LinkedHashMap&lt;Date, LinkedHashMap&lt;Integer, String&gt;&gt;();
      String sqld = "Select Distinct DATE(ctimestamp) as DATE from table ORDER BY DATE DESC LIMIT 730 ;";
      ResultSet rsd = null;
      try {
          rsd = stmt1.executeQuery(sqld);
      } catch (SQLException ex) {
          Logger.getLogger(sortResultshs.class.getName()).log(Level.SEVERE, null, ex);
      }
      try {
          while (rsd.next()) {
              LinkedHashMap&lt;Integer, String&gt; Title = new LinkedHashMap&lt;Integer, String&gt;();
      //        System.out.println("Testing");
              Date Date = rsd.getDate("Date");
     //         System.out.println(Date);
  
              String sql = "Select title,id from table Where DATE(ctimestamp) = '" + Date + "'ORDER BY id DESC";
      //        System.out.println(sql);
              ResultSet rs = stmt.executeQuery(sql);
              rs.last();
              int numRows = rs.getRow();
    //          System.out.println(rs.getRow());
              rs.beforeFirst();
              while (rs.next()) {
                  //        List&lt;String&gt; Title = new ArrayList&lt;String&gt;();               
                  for (int i = 1; i &lt;= numRows; i++) {
                      String title = rs.getString("title");
                      Integer id = rs.getInt("id");
                      Title.put(id, title);
                      TitleList.put(Date, Title);                         
  
                  }
  
   //               System.out.println(rs.getString("title"));
              }
  
               list.add(TitleList);
  
          }
  </code></pre>
  
  <p>and the list looks something like below,</p>
  
  <p>Total Row count is  : 160
  List is  : [{2012-07-12={160=tstng, 159=testing}, 2012-07-10={158=test, 157=test, 156=Testing, 155=Testing, 154=Testing, 153=Testing, 152=Testing, 151=Testing, 150=Testing, 149=Testing, 148=Testing, 147=Testing, 146=Testing, 145=Testing, 144=Testing, 143=Testing, 142=Testing, 141=Teasting, 140=Teasting, 139=Testing, 138=Testing, 137=Testing , 136=Testing for testing, 135=Testing for testing, 134=Testing for Test, 133=Testing}, 2012-07-04={132=Testing for Broker}, 2012-07-03={131=Testing for Post, 130=1200 TESTIN}, 2012-07-01={129=testing}, 2012-03-30={128=upload test}, 2012-03-28={127=test}, 2012-01-08={126=1BHK flat for 1000$ at Journal Square}, 2012-01-04={125=tseting by kiran, 124=tseting by kiran, 123=tseting by kiran, 122=tseting by kiran, 121=tseting by kiran, 120=tseting by kiran, 119=tseting by kiran, 118=tseting by kiran, 117=tseting by kiran, 116=tseting by kiran, 115=tseting by kiran, 114=tseting by kiran, 113=tseting by kiran, 112=tseting by kiran, 111=tseting by kiran, 110=tseting by kiran, 109=tseting by kiran, 108=tseting by kiran, 107=tseting by kiran, 106=tseting by kiran, 105=tseting by kiran, 104=tseting by kiran}, 2012-01-02={103=testing, 102=testing, 101=testing, 100=testing, 99=testing, 98=testing, 97=testing, 96=testing, 95=testing, 94=testing, 93=testing, 92=testing, 91=testing, 90=testing, 89=testing, 88=testing, 87=testing, 86=testing, 85=testing, 84=testing, 83=testing, 82=testing, 81=testing, 80=testing, 79=testing, 78=testing, 77=testing, 76=testing, 75=testing, 74=testing}, 2011-12-09={73=The monthly consumer price index rose 4.2% from the year-ago period, reflecting a huge easing from Octoberâ??s 5.5% increase. }, 2011-12-08={72=Testing for dispplay}, 2011-11-25={71=Apartment for Rent at Journal Square for 1000$.Please see now., 70=Apartment for Rent for 1000$.Please see now., 69=testing, 68=AS, 67=A, 66=A, 65=q, 64=q, 63=Testing, 62=Testing}, 2011-11-24={61=testing, 60=tEST, 59=TESTING, 58=TESTING, 57=Testing, 56=Testing, 55=Testing, 54=tseting, 53=testing, 52=testing, 51=testing, 50=teting, 49=tESTING, 48=tESTING, 47=tESTING, 46=Testing, 45=TESTING, 44=TESTING, 43=tESTING}, 2011-11-23={42=Testing, 41=testing, 40=Testing, 39=tESTING, 38=TETING, 37=testing}, 2011-11-22={36=Testing, 35=Testing, 34=Testing, 33=Testing by Kiran, 32=testing, 31=Testing, 30=Testing by Kiran, 29=Testing byt kiran, 28=Testing by Kiran, 27=testing by kiran, 26=testing by kiran, 25=Testing , 24=TSETING, 23=Testing by Kiran}, 2011-11-21={22=testing, 21=Testng, 20=TESTING, 19=TESTING, 18=ESTING, 17=ESTING, 16=ESTING, 15=ESTING, 14=testing by kiran, 13=testing by Kiran, 12=Testing by Kiran, 11=testing by Kiran, 10=Testing by Kian, 9=Testing by Kiran, 8=Testing by Kiran, 7=Testing byKiran, 6=testing, 5=Test by Kiran, 4=Test by Kiran}, 2011-11-20={3=testing, 2=testing, 1=testing}}]</p>