What is wrong with scriptlets, and what to use instead

This question already has an answer here:

  • How to avoid Java code in JSP files? 28 answers

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

  • A 1998 vintage technology that needs to disappear; a failed response to Microsoft's ASP.
  • Ugly
  • Hard to read
  • Hard to maintain
  • Discourage reuse and encapsulation
  • Encourage putting complex logic in pages
  • What to use instead?

  • 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.
  • 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.
  • Try a more modern template solution like Thymeleaf to generate your HTML from the server side.

  • Here's my take on this.

  • Although it's pretty simple to use Java code with it, Scriplets are hard to read, and it makes the code look a little cluttered imo.
  • Like duffymo and most people would recommend, using JSTL is a much better alternative to Scriplets when it comes to JSP.
  • There was a time when I would just stick to using Scriplets to use some Java code in JSP, but learning JSTL was pretty handy. It makes the code easier to read because it blends well with the HTML tags.


    Its not a clean design to mingle code with view logic. This is why JSP is not ideal solution.

    You should use templates like Velocity/Freemarker instead which does not allow mixing java code at all.

    Additional benefit of this is that non Java UI expert designers can contribute to UI without having to learn Java.

    链接地址: http://www.djcxy.com/p/76592.html

    上一篇: 使用scriptlet的缺点?

    下一篇: scriptlet有什么问题,以及使用什么