Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSP - What is wrong with scriptlets, and what to use instead [duplicate]

Tags:

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.

like image 955
vikarjramun Avatar asked Jul 12 '16 23:07

vikarjramun


People also ask

What is correct about JSP Scriptlets?

When the scripting language is set to java, a scriptlet is transformed into a Java programming language statement fragment and is inserted into the service method of the JSP page's servlet. A programming language variable created within a scriptlet is accessible from anywhere within the JSP page.

What is wrong in using JSP scriptlet 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.

Which tag is used for Scriptlets?

JSP Scriptlet tag (Scripting elements) In JSP, java code can be written inside the jsp page using the scriptlet tag.

What is Jstl what are its benefits over JSP Scriptlets?

The JSP Standard Tag Library (JSTL) is a very important component released by Oracle for JSP programming. JSTL allows us to program our JSP pages using tags, rather than the scriptlet code that most JSP programmers are already accustomed to. JSTL can do nearly everything that regular JSP scriptlet code can do.


2 Answers

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.
like image 85
duffymo Avatar answered Oct 07 '22 17:10

duffymo


Here's my take on this.

  1. 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.
  2. 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.

like image 38
Noir Antares Avatar answered Oct 07 '22 18:10

Noir Antares