Sunday, 1 January 2017

java - JSP - What is wrong with scriptlets, and what to use instead




I read about JSP in a book many years ago, and recently decided to learn on my own. I now know how to use JSP scriptlets, expressions, and declarations




    <%! String str = "Hello World" %>
<%= str.length() %>
<% str=str.substring(0,5) %>


But I have read in many places (on this site and elsewhere) that I shouldn't use scriptlets. This is one such question: Eclipse using Classes in JSP



My questions are:





  1. What is wrong with scriptlets?

  2. What do I use instead?



Thanks in advance for any help!



EDIT:



I do not use Servlets, but embed my JSP code onto an HTML page. UI designers with no knowledge of Java can easily modify my page.
Basically I use JSP as a front end for displaying from a database and making updates to it such as when a user makes an order.



Answer



This is my personal opinion, of course. I say scriptlets are:




  1. A 1998 vintage technology that needs to disappear; a failed response to Microsoft's ASP.

  2. Ugly

  3. Hard to read

  4. Hard to maintain

  5. Discourage reuse and encapsulation

  6. Encourage putting complex logic in pages




What to use instead?




  1. The world has gone in the direction of HTML5, CSS3, JavaScript, jQuery, Bootstrap, and web technologies talking to REST web services. It's a good direction.

  2. If you must stick with JSPs, start with the JSP standard template library. Keep your HTML pages looking like HTML - it'll make it easier for UI developers to maintain them.

  3. Try a more modern template solution like Thymeleaf to generate your HTML from the server side.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...