/*
 * The MIT License
 *
 * Copyright 2017 Wikimark.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.wikimark;

import java.io.IOException;
import java.util.List;
import static java.util.stream.Collectors.toList;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;

/**
 *
 * @author Martín Straus
 */
public class IndexServlet extends HttpServlet {

    private final TemplateEngine thymeleaf;
    private final Context context;
    private final Pages pages;

    public IndexServlet(ServletContext ctx, TemplateEngine thymeleaf, Context context, Pages pages) {
        this.thymeleaf = thymeleaf;
        this.context = context;
        this.pages = pages;
        ctx.addServlet(IndexServlet.class.getName(), this).addMapping("/index.html");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader("Content-Type", "text/html; charset=utf-8");
        WebContext ctx = new WebContext(req, resp, req.getServletContext());
        ctx.setVariable("pages", pages());
        thymeleaf.process("/index.html", ctx, resp.getWriter());
    }

    private List<PageContext> pages() {
        return pages.findLatest(5).stream().map((p) -> p.pageContext()).collect(toList());
    }

}

@@ -2487,9 +2487,10 @@
 est(
-5
+20
 ).st

Index shows 20 pages.