permaculture playing cards*
The moose likes JSP and the fly likes Why is EL so good, and scriptlets so bad Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Why is EL so good, and scriptlets so bad" Watch "Why is EL so good, and scriptlets so bad" New topic
Author

Why is EL so good, and scriptlets so bad

Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

I'm not seeing it. I know, and people who I trust tell me that EL is modern and scriptlets are bad, evil and should be banned.

While scriptlettes may be a bit ugly, I'm not seeing EL as all that much better. Calling a function with parameters is complex and requires all sorts of fancy stuff that is not obvious (like TLD files) and go in places unrelated to the Java code of your beans and the JSP pages of your front end.

Sure, its great to not get null pointer exceptions, but how does a front end developer learn what variables are available in $sessionScope or other magic places? What is there depends on the internals of the beans, not the public API of the beans that are visible just from looking at the Javadocs.

I'm trying to be modern, but I don't get it. The tutorials show a half page of stuff, not how to do real applications. Anything more, and you are pointed to the Sun JSR documents, which are opaque.

Why is this better? And how do I learn to love it?

thanks
-pat
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61094
    
  66

Most of what I have to say on the matter is captured in this article.

And I'm a lot better at technical explanations that motivation, but I'll address a few points...

Pat Farrell wrote:While scriptlettes may be a bit ugly, I'm not seeing EL as all that much better.

My reaction is 180. On top of the already crowded array of syntaxes in a page: HTML, JavaScript, CSS, and JSP actions, eliminating free form Java makes page so much easier to read and maintain.

A major benefit to me is that the JSTL and EL are limited. Generally, if you're finding something hard to do without having to resort to scriptlets, it's a red flag that it's something that should't be done in a view and should have been handled before even getting to the page.

Calling a function with parameters is complex and requires all sorts of fancy stuff that is not obvious (like TLD files)

I don't see TLD file as anything exotic. It's one of the first things I set up when creating a new app.

and go in places unrelated to the Java code of your beans and the JSP pages of your front end.

WEB-INF is hardly what I'd call "unrelated". It's the heart of the web app.

but how does a front end developer learn what variables are available in $sessionScope or other magic places?

That's immaterial and a situation exists whether you're using scriptlets or JSTL/EL. Unless of course you're arguing against Model 2 and arguing in favor of Model 1. That's a different matter entirely.

The tutorials show a half page of stuff, not how to do real applications. Anything more, and you are pointed to the Sun JSR documents, which are opaque.

No argument. The state of online and other docs are pitiful.

And how do I learn to love it?

I fell in love the very first time I saw it. So I'm not sure if I'm qualified to try and convince you...


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Bear Bibeault wrote:I don't see TLD file as anything exotic. It's one of te first things I set up when creating a new app.


Well, to start with, I have no idea how to make a TLD that does anything.

I can't get code to call a trivial function with a parameter. I just don't get it.

One of the things that I hate about how Java and webapps have evolved is that doing anything takes a half dozen inter related files.

To do anything in EL that is not trivial, I have to write the Bean and the JSP and now the TLD and while I grok the Bean and JSP, I have no clue what a TLD is, or how to make one.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61094
    
  66

Sounds like another FAQ is needed. I'll see what I can whip up.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Or enhance the EL function one that is there. Or link to something that ties it all together.

What I have found is parts but they are link "man pages" in that they all assume a larger understanding of stuff that is not covered.

Its a bit like ten blind men each grabbing a part of an Elephant and trying to describe it. Is it a tail? or tusk? or leathery ear? or ???
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3826

Of course EL is not everything, We need JSTL and perhaps standard & custom actions to fulfill our requirements. It's EL that provides standard way of accessing scoped variables etc... (appart from EL functions which can have some functionality inside). As Bear suggested making EL & JSTL limited is that making page authors work pretty easy and also JSP's main purpose more clearer (that it is for the view).


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Vijitha Kumara wrote:We need JSTL and perhaps standard & custom actions to fulfill our requirements.

I don't have a problem in principal with limited languages, Smalltalk is a very simple language.

But JSTL just doesn't do all that much.

Perhaps its the "custom actions" that is the hurdle that I can't get over.

The scriptlet of
<%= somebean.function(foo, baz) %> never struck me as being very hard to explain or show to the front end engineers.

It just calls the function(foo, baz) of the sombean.

So far, I have zero clue how to do that that with EL.

Perhaps Bear's soon to be written FAQ/example will help
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Pat Farrell wrote:I don't have a problem in principal with limited languages, Smalltalk is a very simple language.

Those are very, *very* different statements.

Pat Farrell wrote:But JSTL just doesn't do all that much.

That's the point: get the logic out of the view. It makes views easier to test and puts the code in Java files, where code belongs.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41600
    
  55
Pat Farrell wrote:So far, I have zero clue how to do that that with EL.

Maybe a gentle introduction like this one helps: http://www.orionserver.com/docs/tutorials/taglibs/18.html


Ping & DNS - my free Android networking tools app
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Ulf Dittmer wrote:Maybe a gentle introduction like this one helps


Thanks, but that one, as it seems all others, is partial. Perhaps If I started at #1 in that set it would be complete.

Specifically, they talk about editing the TLD, and then show only a portion of the TLD. They do not show a complete TLD.

I've tried TLD fragments like these, from @bear's example. and it is not enough to actually build and test code.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61094
    
  66

Pending the time to write up a complete FAQ, here's a skeleton for a TLD:


Substitute your own values, then add the tag, function, and tag file declaration fragments to this skeleton. Drop in WEB-INF. Profit!
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

I know this is a very old thread, but I wanted to know if Pat learned to love EL, as I am in the same position as he was then
I pass a bean back from my servlet and it seems to me that
is much neater than anything EL offered.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Love? nope. Perhaps we are at friends with benefits stage.

I use it all the time, no scriptlets for me. But a number of things are very klunky. The lack of a proper if/then/else structure drives me nuts on nearly every page.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61094
    
  66

Wendy Gibbons wrote:and it seems to me that
is much neater than anything EL offered.

How is that neater than ${bean.value}?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61094
    
  66

Pat Farrell wrote:The lack of a proper if/then/else structure drives me nuts on nearly every page.

The combination of <c:choose> and <c:when> works fine for me for conditionalizing complex situations.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

well, <chose> has the function, but every other language has if/then/else
and I see zero justification for making EL special.

To my eyes, EL is a poorly designed language.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4372
    
    8

Pat Farrell wrote:well, <chose> has the function, but every other language has if/then/else
and I see zero justification for making EL special.

Not necessarily a justification, but XSLT uses <choose> as well. I suspect it's a consequence of having a language that can exist in XML format: if/then/else just doesn't work.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61094
    
  66

I've seen a few frameworks that try to use if-then-else in tags, and the result is pretty much the same as choose-when-otherwise, just with different tag names. It comes down to the differences between declarative and procedural markup I imagine.

And this isn't an EL issue at all, but a JSTL issue, so using it to say that the EL is badly designed based upon if-then-else structuring isn't accurate. Your beef appears to be with the JSTL.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

I agree that it is just syntactic sugar, but when you have a distinction without difference, it raises the question: why?

EL and JSTL may technically be distinct, but they are effectively the same to me. And yes, I don't like them. Personal opinion, YMMV

I am ready for Java2 or some other language that starts from the ground up to deliver a clean, consistant compiler to run on the JVM. As I've written many times in this forum, I think Java has been hacked too many times, and pulled in too many directions. Its lost its charm and consistency.
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

Bear Bibeault wrote:
Wendy Gibbons wrote:and it seems to me that
is much neater than anything EL offered.

How is that neater than ${bean.value}?


your right, I should have said "as neat".
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2342
    
  28

Another thing I'd like to add is it seems like JSTL/EL are purposely designed so that things that you shouldn't be doing in the view are hard to do in JSTL/EL. For example, Pat mentioned earlier about the difficulty in calling a function using EL. Well, you shouldn't be calling functions in your view layer anyways. The View layer shouldn't change the state of the system, it should only render/display the current state
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2342
    
  28

Wendy Gibbons wrote:
Bear Bibeault wrote:
Wendy Gibbons wrote:and it seems to me that
is much neater than anything EL offered.

How is that neater than ${bean.value}?


your right, I should have said "as neat".


Yes you are right, but



is much neater than

Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

Jayesh A Lalwani wrote:Yes you are right, but



is much neater than



ahh I wrote neater beans than you, mine would always return the required display value, as this is a display bean.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Jayesh A Lalwani wrote:Pat mentioned earlier about the difficulty in calling a function using EL. Well, you shouldn't be calling functions in your view layer anyways. The View layer shouldn't change the state of the system, it should only render/display the current state


Huh? You have two separate concepts conflated.

Functions don't have to change state. How about a function to convert data to JSON? Or perform a fancy Date range calculation?

Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2342
    
  28

Pat Farrell wrote:
Jayesh A Lalwani wrote:Pat mentioned earlier about the difficulty in calling a function using EL. Well, you shouldn't be calling functions in your view layer anyways. The View layer shouldn't change the state of the system, it should only render/display the current state


Huh? You have two separate concepts conflated.

Functions don't have to change state. How about a function to convert data to JSON? Or perform a fancy Date range calculation?



If your functions aren't changing state then why aren't they just getter methods? Functions don't have to change state, but by convention, any function that is just returning state is implemented as a getter method, and it is assumed that any function that is not a getter method might change state.

Of course, you can always implement a getter method without following the convention ,and then complain that it's difficult to call your getter from EL. It's hard for me to see how this is a problem with EL though.

Conversion to JSON shouldn't be done by the data bean/controller anyways. It is the job of the view to render JSON, not the bean or the controller. Besides why would you render JSON in JSP when there are so many other utilites available that can render beans into JSON

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61094
    
  66

It's fairly easy to use EL functions to do thing like JSON conversion. (And, the newest version of the EL allows bean method calls.)
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Jayesh A Lalwani wrote:If your functions aren't changing state then why aren't they just getter methods?

By your definition, they are. But they are not bean specific, or even related to a bean.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Bear Bibeault wrote: (And, the newest version of the EL allows bean method calls.)

Please tell me more. Or links....
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61094
    
  66

Hmm, I may have spoken to soon. I thought sure I read that, but looking through the EL 2.2 Spec, I can't find anything. Let me delve further.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61094
    
  66

OK, I didn't think I was going crazy. Even though I can't find it in the Spec, I just downloaded Tomcat 7 (which supports JSP 2.2) and gave it a try. And calling a bean's general method as follows seems to work just fine.
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

Bear Bibeault wrote:OK, I didn't think I was going crazy. Even though I can't find it in the Spec, I just downloaded Tomcat 7 (which supports JSP 2.2) and gave it a try. And calling a bean's general method as follows seems to work just fine.


so you can get exactly the same functionality you could with a scriptlet, it looks almost identical, but you need an extra configuration file (the TLD)?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61094
    
  66

You're mixing things up. To define EL functions, you need a TLD entry. This has been true since JSP 2.0 and is a good thing. It defines high-level functions that can be used throughout the web app in a consistent manner.

What we're talking about here is a new capability with JSP 2.2 that allows calls to bean methods that accept parameters. No TLD entry necessary -- the method is inferred in the same way that properties are currently inferred.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3826

Bear Bibeault wrote:...What we're talking about here is a new capability with JSP 2.2 that allows calls to bean methods that accept parameters. No TLD entry necessary -- the method is inferred in the same way that properties are currently inferred.

Looks cool. That'd eliminate EL functions altogether? Or is it only for instance methods?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61094
    
  66

Not a replacement -- an augmentation.
Herman Bovens
Greenhorn

Joined: Sep 16, 2013
Posts: 3
Bear Bibeault wrote:
Wendy Gibbons wrote:and it seems to me that
is much neater than anything EL offered.

How is that neater than ${bean.value}?


At least in Eclipse: you get the benefits of compile-time checking, code completion, reference searching, auto refactoring and syntax coloring.
Herman Bovens
Greenhorn

Joined: Sep 16, 2013
Posts: 3
Bear Bibeault wrote:OK, I didn't think I was going crazy. Even though I can't find it in the Spec, I just downloaded Tomcat 7 (which supports JSP 2.2) and gave it a try. And calling a bean's general method as follows seems to work just fine.


1. I always hear the testability argument against scriptlets. Now how is the above any more testable than calling the method in a scriptlet? It's even more error prone since somGeneralMethod may not even exist.
2. When you're using tags for control flow, you're essentially writing code in your jsp. Only in a syntax that really wasn't designed for it.
3. I'm not advocating to write business logic in scriptlets (or in the view at all), just presentation logic.
4. I currently don't even write JSPs anymore :)
margaret gillon
Ranch Hand

Joined: Nov 12, 2008
Posts: 318
    
    5

I currently don't even write JSPs anymore :)


What do you write instead?
Herman Bovens
Greenhorn

Joined: Sep 16, 2013
Posts: 3
margaret gillon wrote:
I currently don't even write JSPs anymore :)


What do you write instead?


Currently, I use the Vaadin framework and write most of my GUI in pure Java.
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 542
    
    4

Look, it's pretty simple. With EL 3.0, there's not really much you can't do anymore. Functions with parameters, even closures I believe. So now it's mature, and a lot more concise than scriptlets.

Furthermore, with scriptlets you always have to watch out you don't destroy your JSP. There's always gotta be a generated servlet in the back of your mind.

With EL, it's easy work. You develop the backend, your HTML guy provides the purely HTML GUI, and you replace template data with simple, brief EL expressions and JSTL tags/JSF attributes. That's great for me. I'm a lazy guy.


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why is EL so good, and scriptlets so bad