25 Dec 2011

JavaScript in JSP : Bad Practice

A JSP(Java Server Pages) is a server side technology whereas the JavaScript is a client side. Even after both these are work on different side , they work together to create more dynamic responsive pages on the world wide web. I had initiated a discussion ,whether  is it a bad practice to use JavaScript within JSP pages ,on the  javaranch for which i get great response. Here are some quotes of the discussion : 


Paul Clapham said
"As long as you realize that the Javascript code you put in your JSP is not going to be executed as part of the JSP, it's going to be executed only when it gets to the browser i.e. on the client side. That seems to be a concept that a lot of people aren't told about when they start learning JSP. As already pointed out, it's better to put your Javascript in a separate file and just provide a link to that file in your JSP. That way the browser can cache the Javascript instead of having to download it repeatedly as part of the JSP. There are cases, though, where you might want your JSP to generate some Javascript which varies based on input parameters or session data or whatever. "
 That seems reasonable if something is there to use  then use it ,as simple as that. But some fellow has different opinion about that.Lets see what Tim has said 


Tim Holloway said
"JavaScript is an unholy mess, but it can make pages more interactive because it programs the client instead of the server.You shouldn't depend on JavaScript to be your sole data validator. The final determination of what's valid should be in the server-side code, where it cannot be spoofed by a malicious client. Plus, people do sometimes turn JavaScript off in their browsers, in which case none of the client-side code will execute at all!"
a very important point is been noted here since JavaScript is run inside browser and browser can turn off the JavaScript so one must be aware of that and action must be taken on server side.  

Bear Bibeault said
a JSP is simply a template for an HTML page. To the JSP processor, anything that's not a JSP construct is just template text that is completely ignored by the JSP engine and sent to the client verbatim. So JavaScript is completely opaque to the JSP. Now, putting Java scriptlets into a JSP: that's not only a poor practice, but completely irresponsible at this point in time when we're coming up to the 10th, yes 10th, anniversary of JSP 2.0. Ten years is plenty of time for anyone to have come up to speed on the JSTL and EL and there is no excuse, none, nada, zero zilch, justification for still writing JSPs as if it were 2001. 



see full discussion  here

No comments:

Post a Comment