3

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?

4
  • Its a very good thinking.Hope there is no other reason other than this.
    – Warrior
    Commented Nov 29, 2008 at 7:59
  • 1
    downvoted - questions like this should give references to any such assertion
    – Alnitak
    Commented Nov 29, 2008 at 8:43
  • Are you possibly thinking of the assertion that you shouldn't use Java in JSP? It is considered best practice to keep Java business logic in servlets/separate classes and only put display information/html/js in the jsp file.
    – mtruesdell
    Commented Nov 29, 2008 at 14:11
  • @Alnitak - could you be more specific
    – Omnipotent
    Commented Dec 1, 2008 at 5:10

5 Answers 5

11

Afaik, there is no problem in using javascript in jsp, and many sites do use it. Especially if your site is a web app, you will have to use both side by side. They are completely independent. You just have to make sure that what you are using each for is appropriate.

As for the part about turning off javascript in a browser making the site unusable, it is true whenever you use javascript, with or without jsp.

1

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

1

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.

2
  • 1
    "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). Commented Jul 24, 2009 at 17:19
  • I would not disagree with this. Commented Jul 27, 2009 at 14:58
0

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.

2
  • 1
    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.
    – glmxndr
    Commented Jul 24, 2009 at 12:59
  • 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 :) Commented Jul 24, 2009 at 13:55
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.