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.

1
  • 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. Commented Jul 12, 2016 at 23:26

3 Answers 3

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.
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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?
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.

3 Comments

I wish I could select two answers right now
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.
+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.
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.

8 Comments

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 just use jsf
@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 his answer doesn't conflict with mine, jsf can run on top of html5, js, bootstrap, etc.
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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.