File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Javascript Vs JSP Loops

 
s penumudi
Ranch Hand
Posts: 113
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1640
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64196
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
s penumudi
Ranch Hand
Posts: 113
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with you Bear.
I really appreciate your responses
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic