22

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.

Community
  • 1
  • 1
vikarjramun
  • 1,042
  • 12
  • 30
  • 2
    JSP can be considered as deprecated. If you have the choice, go for lightweight solutions that don't need to be compiled and have a better separation of logic and layout. – Jan B. Jul 12 '16 at 23:26

3 Answers3

17

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.
duffymo
  • 305,152
  • 44
  • 369
  • 561
  • I agree with quite a bit of what you say, but this is a terrible answer. "HTML5" isn't an answer to "how do I create dynamically-generated web pages on a server?" The answer is something like "JSTL or Thymeleaf". Maybe lead with that next time. Also, part of the question was "why not use scriptlets" which you didn't address. – Christopher Schultz Jul 14 '16 at 14:02
  • Terrible? That's not consistent with "I agree with quite a bit of what you say". There are two parts: (1) What's wrong with scriptlets, and (2) What else do I use? Did you not read item #3 in the second list? – duffymo Jul 14 '16 at 16:39
11

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.

Noir Antares
  • 242
  • 1
  • 10
  • I wish I could select two answers right now – vikarjramun Jul 13 '16 at 01:36
  • It's fine, duffymo actually put some good alternatives to JSP. If you want to keep the HTML code clean in the JSP, tinker about with the likes of CSS, jQuery and JS. – Noir Antares Jul 13 '16 at 01:38
  • 4
    +15 points for a user with '227k' reputation doesn't matter much. +15 points for a user with '1' reputation means a lot and encourages them. At least, upvote this answer if it helped. – rupinderjeet Jul 13 '16 at 12:16
6

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.

Amit Mahajan
  • 895
  • 6
  • 34
  • A couple of things: My JSP code is easy for non-Java designers to understand- It's basically HTML with values; I need Java code only for connecting to a database and reading/writing objects objects. How else can I achieve this if not through dynamic server-side languages like JSP? – vikarjramun Jul 13 '16 at 00:21
  • 1
    @vikarjramun just use jsf – 0x6C38 Jul 13 '16 at 00:32
  • @MrD Actually, that is exactly what I was looking for. However, I have gotten some good advice from duffymo's and amitmah's answers. I think duffymo's answer helped the best, and I will accept his. – vikarjramun Jul 13 '16 at 00:59
  • @vikarjramun his answer doesn't conflict with mine, jsf can run on top of html5, js, bootstrap, etc. – 0x6C38 Jul 13 '16 at 01:03
  • @MrD I understand that. It is just with Thymeleaf (his suggestion), I don't seem to need JSF. – vikarjramun Jul 13 '16 at 01:05
  • @vikarjramun in that case I recommend you take a look at [play framework](https://www.playframework.com/). – 0x6C38 Jul 13 '16 at 01:08
  • 4
    When you mention you need database connectivity code inside JSP, it looks like you did not understood separating display logic and code. Database can change and you will end up in changing JSP. – Amit Mahajan Jul 13 '16 at 01:30
  • FYI, 4 years later, I am now fully using Freemarker rather than JSP, properly seperating my UI code from buisness logic. – vikarjramun Apr 24 '20 at 04:35