Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is JSP superseeded and if so, how?

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?

like image 564
Niklas Rosencrantz Avatar asked Dec 24 '16 01:12

Niklas Rosencrantz


People also ask

How does JSP work with a simple example?

In this article, we will learn how JSP works with a simple example. JavaServer page (JSP) is a template for a Web page that uses Java code to generate an HTML document dynamically. JSPs are run in a server-side component known as a JSP container, which translates them into equivalent Java servlets.

Why do we use JSP instead of JRE?

This is why a servlet container that supports JSP must have the full JDK available as opposed to only needing the JRE. So the primary reason for JSP is to reduce the amount of code required to render a page. If you don't have to render a page, a servlet is better.

What are the advantages of JSP over servlet?

No recompilation or redeployment is required. JSP has access to entire API of JAVA . JSP are extended version of Servlet. Coding in JSP is easy :- As it is just adding JAVA code to HTML/XML. Reduction in the length of Code :- In JSP we use action tags, custom tags etc.

Do I need to redeploy JSP?

No recompilation or redeployment is required. JSP has access to entire API of JAVA . JSP are extended version of Servlet. Coding in JSP is easy :- As it is just adding JAVA code to HTML/XML.


1 Answers

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.

like image 87
Ken Bekov Avatar answered Sep 23 '22 09:09

Ken Bekov