, ,

Servlets and JSPs were important for long time, but some concepts are very important and curious and may not gain the proper attention. How a JSP page turns into an HTML page? How can we work directly with the HTML instead of JSP (like the Thymeleaf)?


  • JSP

Java Server Pages, that are template engines. On it, it’s possible to write Java code either directly (scriptlets) and with JSTL / taglibs, and you can get parameters / objects passed through a Servlet.

  • Servlets

Java classes that are responsible for dealing with requests from Web clients, such as the browsers.

  • Web Container (Servlet Container)

It is a component of a web server that interacts with Java Servlets. It is the container that delivers the servlet the request and the HTTP response, and calls servlet methods (such as doPost or doGet).

How JSP turns into HTML with filled data?

When a JSP is requested, the Web Server uses a JSP compiler and converts the JSP into a Servlet that implements the interface javax.servlet.jsp.JspPage.

O arquivo JSP é compilado somente quando a página é requisitada pela primeira vez ou quando o arquivo JSP é alterado, ou seja, o Servlet gerado a partir do JSP é reutilizado, fazendo as próximas chamadas serem mais rápidas.

The JSP file is compiled only when the page is first requested or when the JSP file is changed, i.e., the Servlet generated from the JSP is reused, making the next calls faster.

But how so? The Web Server that transforms the JSP? 

That’s right. The Web Server transforms the JSP into a Servlet.

But what about my Servlet? And what’s that getRequestDispatcher?

When your Servlet is a layer above your JSP (i.e. when your JSP is not called directly in the URL), usually you work with the data to put in the JSP, so it’s common to see something like:

req.getRequestDispatcher(jspPage).forward(req, res);

In fact what you did was pick up everything you did and forward to the next servlet (JSP to be compiled). Using Tomcat 8.0.23, with my project name livrariadigitalweb, JSPs turned into Servlets of the class: org.apache.jasper.runtime.HttpJspBase and I can see the code of the JSPs compiled in:


And how can we work with HTML instead JSP?

According to the official website, Thymeleaf is a modern server-side Java template engine and is well used in the Spring ecosystem (especially Spring MVC). Giving a little context:

With Thymeleaf it’s possible using HTML and use elements and attributes in HTML itself:

      <tr th:each="row,rowStat : ${sb.rows}">
        <td th:text="${rowStat.count}">1</td>
        <td th:text="${row.variety.name}">Thymus Thymi</td>
        <td th:text="${row.seedsPerCell}">12</td>

In the case of Thymeleaf, these attributes are built from Dialects and interpreted by Processors. Basically when you configure the Template Engine as Thymeleaf (org.thymeleaf.spring4.SpringTemplateEngine or org.thymeleaf.TemplateEngine) is already set the standard dialect, and you can also create your own and add it.

How Thymeleaf converts that?

The Thymeleaf converts these attributes / elements from DOM parser (Document Object Model). That’s right, a parser of XML tree, DOM tree.


That’s it folks, I hope you have understood and liked. Any doubt, you can ask me.