Is JSP superseeded and if so, how?

9.4k Views Asked by At

It was a while since I worked with JSP and servlet. Now I find some material that JSP is obsolete and should not be used. Is that so? Why? What should we use instead for templates if we start a new project with Spring Framework and wish to render web pages or similar output?

1

There are 1 best solutions below

1
On BEST ANSWER

Spring (as well as Struts, Apache Wicket and other frameworks) is based on servlets. When you're using Spring for web, you're using servlet as an underlying technology.

JSP is really a little bit outdated. And there are some inconveniences in it. For example, JSP is a real headache for web designers. Designers cannot just open a JSP file, make some changes, and check the result in the browser, because the JSP file contains tags that are invalid for HTML. The only way to see how the page will look like in the browser is to deploy the application and let the server render it.

Another inconvenience in JSP is that you can't externalize common layout into a dedicated file. All you can do is import one page into another with <jsp:include>. And if you have hundreds of files, you have to repeat the same <jsp:include> in all of them to copy common parts.


There are template engines that are more suitable for big projects when you have hundreds of complex dynamical pages. One popular template engine is Thymeleaf.

Thymeleaf's templates contain just valid HTML. That means designers and programmers can work in parallel. Also it has a good layout system. Moreover, Thymeleaf has much more readable and elegant syntax in contrast to JSP. Here is an example of the code for generating a simple table in Thymeleaf:

<html>
....
<table>
  <tr>
    <th>Name</th>
    <th>Price</th>
    <th>In stock</th>
  </tr>
  <tr th:each="prod : ${prods}">
    <td th:text="${prod.name}">Onions</td>
    <td th:text="${prod.price}">2.41</td>
    <td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
  </tr>
</table>
....
</html>

There are many other additional features like build-in support of internationalization, passing parameters to fragments and so on.

You can find more details on the comparison of Thymeleaf and JSP here and here.