Servlet

Jtwig Web project extends Jtwig Core with and eases the integration with Java Servlet API (version 3.1). It also adds the function path, a new resource WebResource and resource resolver WebResourceResolver.


The path function

The path function can be used to output the Servlet Context Path, for more information about this value, check the Servlet API documentation. It is expecting either none of one argument. If no argument is provided, as mentioned, it will only print the servlet context path, on the other hand, if the an argument is provided (expected to be a String), then it appends such value to the servlet context path.

{{ path('/index'}}

Assuming the servlet context path is /application, then the previous example will output /application/index.


The WebResource and WebResourceResolver

This combo of WebResource and WebResourceResolver allows Jtwig to support web (WEB-INF/ rooted) resources. Users can then reference such resources from their app. Note that, if a given path is prefixed with web:, it will be interpreted by this resource resolver, such can be used to disambiguate the source of those resources.


Combining with the Servlet API

To integrate Jtwig Web with the Servlet API the JtwigRenderer concept was introduced.

copy
public static class HelloWorldServlet extends HttpServlet {
    private final JtwigRenderer renderer = JtwigRenderer.defaultRenderer();

    @Override
    protected void service(
        HttpServletRequest request,
        HttpServletResponse response
    ) throws ServletException, IOException {
        request.setAttribute("variable", "Hello");

        renderer.dispatcherFor("/WEB-INF/templates/index.twig.html")
            .with("name", "Jtwig")
            .render(request, response);
    }
}

The example above can be seen in jtwig-examples. It will print Hello World!, as the /WEB-INF/templates/index.twig.html template is defined as {{ variable }} {{ name }}!. As one can see in the previous example, JtwigRenderer exposes all request variable as part of the JtwigModel passed to the render stage.

Note that, this JtwigRenderer also exposes an API call to specify the Jtwig template inline, as shown in the example below. Such example will produce the same output as the example above.

copy
public class HelloWorldServlet extends HttpServlet {
    private final JtwigRenderer renderer = JtwigRenderer.defaultRenderer();

    @Override
    protected void service(
        HttpServletRequest request,
        HttpServletResponse response
    ) throws ServletException, IOException {
        request.setAttribute("variable", "Hello");

        renderer.inlineDispatcherFor(" !")
            .with("name", "Jtwig")
            .render(request, response);
    }
}

The app variable

JtwigRenderer injects into the JtwigModel the app variable which exposes information extracted from the HttpServletRequest provided.

copy
public class Application {
    private HttpRequest request;
}
copy
public class HttpRequest {
    private final String path;
    private Map parameter;
    private Map query;
    private Map session;
    private Map cookies;
}

This allows one to access:

  • Current path being accessed (using app.request.path)
  • GET query parameters (using app.request.query.parameterName)
  • POST parameters (using app.request.parameter.parameterName)
  • Session parameters (using app.request.session.parameterName)
  • Cookie parameters (using app.request.cookies.parameterName)

Integration

Integration of Jtwig Web in your project will depend on the dependency management mechanism being used. Also, you will need to make sure jcenter is part of your repository list. The most recent version is 5.85.3.RELEASE.


Gradle

copy
repositories {
    jcenter()
}

dependencies {
    compile 'org.jtwig:jtwig-web:5.85.3.RELEASE'
}

Maven

copy
<repositories>
    <repository>
        <id>bintray</id>
        <url>https://jcenter.bintray.com/</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>org.jtwig</groupId>
        <artifactId>jtwig-web</artifactId>
        <version>5.85.3.RELEASE</version>
    </dependency>
</dependencies>