views:

457

answers:

5

Why is it not advisable to use JavaScript in JSP? One rationale that I can think of is turning off the feature in browser would stop the code from executing. Is there any other reason behind this?

A: 

JSP is a server side technology. That is - Server parses/compiles all the information and then sends it to the client (i.e. your browser). And then..

If received data contains ANY javascript then browser interprets it in its own javascript VM and server is long forgotten by then since HTTP is stateless protocol.

Considering "not recommending JS in web" I wouldn't bother about it. Most of the sites use JS extensively thus turning it off on the client side would render them mostly useless.

Eimantas
+10  A: 
trex279
A: 

If you have many clients, then it may be a good idea to put calculations on the clients with JavaScript. If you do everything on the server, you are just going to waste the many processing powers provided by the client's browsers, and the speed advantage of Java would be for naught if too many clients make the server busy. (Thanks to @Subtenante in the comments for pointing this out!).

But on the other side, Java running on the server is faster than JavaScript on the client, and it won't suffer from redundant code on the client, for testing which browser the client runs.

JavaScript has its uses as trex pointed out, though. Since it runs on the client, it can for example validate form fields before sending it to the server.

Johannes Schaub - litb
Java is faster than JS, ok, but you have one server, and there may be LOTS of clients. Heavy functionalities may show a gain of performance when left out to the client, relieving the server from it.
subtenante
Thanks for the comment. I agree with you, and amended my answer. I feel today that i don't in particular like my answer very much. If i could, i would delete my answer and tell the questioner to select another one from below :) But i hope you agree with my answer now :)
Johannes Schaub - litb
+1  A: 

I know this is a very old question but I thought I would pipe in anyways. I think you should use javascript in this current time period as it allows for a very rich user experience and with js libraries like jQuery (my personal favorite), prototype and mootools it has become very easy to integrate javascript into your application with little effort.

If you design your application right, you can add javascript that enriches the user's experience (and can make it easier for them to use your site/application) without additional server overhead and very little effort.

How ever - your site should not rely on the javascript for function as the user's browser may not support it.

Javascript should be unobtrusive and provide a richer experience to those user's who support it.

Here is a good article about UI and upgrading gracefully instead of designing for the failure of the user's browser. http://realtech.burningbird.net/javascript/accessibility/gracefully-upgrading

Dan
A: 

He's got to mean "don't use java scriptlets", which is the stuff between <% %>.

The biggest reason has got to be maintainability and debugging; scriptlets make everything make both very difficult.

On the other hand, if you implement taglibs, you can extract any logic to a real java class, which is easily debugged, and you will still be able to open things up in a visual xml/html editor, since taglibs are a valid xml structure.

Now, it is a bad idea to do validation on the client side (in javascript). Users can disable javascript or even access a url directly to get around validation, which opens you up to exploits.

"Now, it is a bad idea to do validation on the client side (in javascript)." It's bad to *only* validate on the client side. Doing it in *addition*, however, makes for better responsiveness (no need to wait for the server to reject it and reduction of load of the server).
Johannes Schaub - litb
I would not disagree with this.