1

Whenever I code in JSP and have inline Java code as follows:

  <%
    double num = Math.random();
    if (num > 0.95) {
  %>
      <h2>You'll have a luck day!</h2><p>(<%= num %>)</p>
  <%
    } else {
  %>
      <h2>Well, life goes on ... </h2><p>(<%= num %>)</p>
  <%
    }
  %>

It seems to drive people crazy and they pass comments such as:

Why are you using technology that is 20 years old?

This is not manageable code

You Should be using Servlets

You should be using JSTL.

Even JSTL is no longer a Good Practice.

And yet when it comes to other languages such as Python (Django) and C# (ASP.NET) it seems to be perfectly fine. The following snippets are from tutorials as early as just a few months.

So whats going on here? Only Java has found a way to manage code properly and other languages are sitting back with a 20 year old technology? Or Java developers are over reacting to a perfectly acceptable way of coding?

{% for x in range(5) %}
    <h1>Hello World</h1>
{% endfor %}

Same case in Razor when ASP.NET MVC initializes when you start a new project, the cshtml pages have inline c# codes. For example as follows:

@{ var theMonth = DateTime.Now.Month; }
<p>The numeric value of the current month: @theMonth</p>

Note: Please note that I am not asking how to avoid writing Java code in JSP. I am asking why is back end coding "Not an issue" in other languages less Java.

Community
  • 1
  • 1
ksv99
  • 65
  • 1
  • 1
  • 5
  • 1
    Fine, I've reopened. [This](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files) explains why scriptlets are discouraged in Java. – Sotirios Delimanolis Apr 28 '16 at 22:23
  • @SotiriosDelimanolis Thanks. I did saw that post prior to asking this question. I get why scriplets are discouraged. Still I don't get why this concept of writing back end code inside html files is perfectly acceptable in other languages and still manageable. – ksv99 Apr 28 '16 at 22:27
  • you can run logic in your django/flask templates ... I dont understand what you are asking – Joran Beasley Apr 28 '16 at 22:31
  • 2
    @JoranBeasley JSPs, which allow logic to be executed, have been semi-deprecated for a long time. I think OP is asking why that's the case for Java, but not for other languages that have styles that resemble JSPs. – Sotirios Delimanolis Apr 28 '16 at 22:41

1 Answers1

0

The problem isn't just scriptlets.

The problem is separation of presentation and business logic.

Sure, the presentation (your template) needs logic, like conditional sections and loops for repetitive values, but the gathering/preparation of the data (business logic) belongs in an action handler written in pure Java/C#/Python, not embedded (hidden) inside the presentation template.

For Java and JSP's, the JSP template supports scriptlets, but it also supports tag libraries, that basically deprecated the use a scriptlets, to the point that scriptlets are often banned (coding policy).

For other template languages, that don't have two ways of doing conditional and loop logic, you use what's available. You can't "ban" doing so when it's the only way, but you should still limit that logic to pure presentational logic.

E.g. C#, or rather ASP.NET, encourages the same with the code-behind model:

ASP.NET's code-behind model marks a departure from Classic ASP in that it encourages developers to build applications with separation of presentation and content in mind.

So, for JSP's, JSTL should be powerful enough to do anything you need for presentation. All other logic should be in action handlers. It should therefore never (rarely) be needed to write scriptlets.

Andreas
  • 154,647
  • 11
  • 152
  • 247