Although servlets does the task of sending the HTML code to the client programmers weight JSP over servlets for that. Even a JSP code is compiled into servlet before giving the response to the browser then why is JSP preferred? If servlet is used then a lot of computation work can be saved.
Why Choose JSP Over Servlets for View in Java EE?
java-eejsp
Related Solutions
You have 2 options :
- Consuming the REST API using AJAX requests. In this case, your web client will return to the web browser a "static" page, that will retrieve all the needed data using AJAX requests to the REST API. Using this approach, you will transport the main processing in the client side (client meaning the web browser of end users). For an easier implementation of AJAX requests and related concepts, such as client-side validation and transformation of data, you can leverage various JS frameworks (such as jQuery, Angular JS etc).
Disadvantages: processing load in client instead of server might degrade application's performance, security issues in client in case of bad design
- Consuming the REST API using back-end requests. In this case, you will make the necessary data loading using the REST API from back-end calls. After you have loaded necessary data and dynamically generated the whole page, you will return it to the web browser. In this case, the Javascript used will be minimal and only used for animations (and not data loading). There are various libraries-frameworks that will make your job easier for creating the API calls and consuming the responses. Some of them are JAX-RS client, Unirest, Resty
In order to avoid the architectural pattern Client -> Servlet -> REST API, you could design your application in such a way, that the service is returning various forms of responses (JSON-REST, HTML etc.). In this way, after loading the data, depending on the type of the client, you could choose the format of the response and use the suitable service.
Primarily, the people who created JSX disagreed with the people who disliked JSP. See their discussion here: Why did we build React? as well as Displaying Data
Templates is based on the idea of creating a division between the logic and presentation of a page. On this theory your javascript (or java) code shouldn't be concerned with what markup gets displayed, and your markup shouldn't be concerned with any of the logic involved. This division is essentially why people criticize the various template languages that readily allowed mixing code in with your template (PHP/JSP/ASP).
React is based on components. The authors of react argue that the logic and presentation of a component are tightly connected, and attempting to divide them doesn't make any sense. Instead, a page should be broken by logical pieces. So you might break out the header bar, comments, post, related questions, etc into seperate components. But it doesn't make sense to try and divide the logic for the related questions from the presentation.
The primary difference between something like JSX and something like JSP is that JSP is a template language that includes a bit of java for the logic. JSX is javascript with a syntax extension to make it easy to construct fragments of html. The emphasis is different. Since JSX embraces this approach, it ends up producing a nicer, cleaner approach then done by JSP or friends.
But ultimately, it comes down to the fact that the people who made react didn't like templates. They think they are a bad idea, and that you should put your markup and presentation logic in the same place.
Best Answer
You can look at JSP as just another more friendly syntax for writing servlets. So, it's not so much a choice between JSP and servlets: the choice is between writing servlets in JSP syntax and writing them in raw Java. As to why would someone prefer the first over the latter, for many (not all) situations raw Java syntax is very inconvenient since:
entire page is inside the
out.println()
callsusually means lots of markup within strings
for non trivial pages this is insanely difficult to read not to mention debug
the increased development cost is much much greater than the lowered performance cost (which isn't lowered at all, see the next point)
No, not really. Well, only computation at deploy time, when servlets are generated from the JSPs, but during run time it's the same (assuming you disable the option to generate servlets in every request).