File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Javascript Vs JSP Loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Javascript Vs JSP Loops" Watch "Javascript Vs JSP Loops" New topic
Author

Javascript Vs JSP Loops

s penumudi
Ranch Hand

Joined: Nov 17, 2004
Posts: 113
Hello,

I was asked this questions in an interview.

Which is one is best. Javascript looping to iterate through collection or use JSTL/any framework provided/for/while loops to loop through the collection to display information onto the browser.

At that point of fact I could not answer to that properly.
I was thinking in terms of code size of the JSP because of its 64K limitation and Client browser setting when Javascript looping is used.

Thinking more and more of it, I feel Using JSP looping is better than Javascript.

Person who interviewed me says that the javascript code is much simpler to maintain and on time scale it is more faster than using java looping.

But I think using Java looping makes code simple and easer to maintain. May be first page will take time to load but subsequent page will be loaded faster.

It is not that I don't want to use javascript at all. I want to use it where I want to populate select option on selection of other form field or something like that.

I was wondering if I am missing something.

Please share your thoughts.
Thank you
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1029
Do you mean javascript, or java "scriptlet" code on a jsp (ie java code between <% and %>

javascript can not loop through a java collection.
You would have to download all the data to the client side, as javascript and then execute the loop on the client. That makes for some major processing on the client, and javascript can be comparatively slow to compile and run on the fly.

Using java code in a JSP with <% %> tags, you can use a standard for loop.
But that introduces scriptlet code into your JSP, which makes it harder to maintain.

You can use tag-based loops (eg JSTL forEach tag)
That has the benefit of being plain text in the jsp file. I find them much easier to understand and work with than scriptlet code. But it is less efficient than the pure scriptlet loop.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61769
    
  67

While I generally balk at "which is best" type of question since such issues usually require a great deal of knowledge about the problem space, and usually end up being a subjective call in any case, I had to smirk at

Person who interviewed me says that the javascript code is much simpler to maintain and on time scale it is more faster than using java looping.


Few people would agree that JS is simpler to maintain than Java -- I usually find it quite the opposite -- but in regards to "time savings", I wonder in what world it saves time to send a bunch of data over the network to be lopped over by interpeted code in the browser rather than not including it on the rendered page in the first place using server-side Java.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
s penumudi
Ranch Hand

Joined: Nov 17, 2004
Posts: 113
I agree with you Bear.
I really appreciate your responses
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Javascript Vs JSP Loops