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:
Thanks in advance for any help!
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.
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.
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.
JSP Scriptlet tag (Scripting elements) In JSP, java code can be written inside the jsp page using the scriptlet tag.
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.
This is my personal opinion, of course. I say scriptlets are:
What to use instead?
Here's my take on this.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With