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 in jsp : Bad Practice ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "JavaScript in jsp : Bad Practice ?" Watch "JavaScript in jsp : Bad Practice ?" New topic
Author

JavaScript in jsp : Bad Practice ?

naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

hi ,

i am creating a Demo application for learning the servlet and jsp. i am confused with the use of JavaScript within jsp.

Is that a Bad practice to use javaScript in jsp? Since javascript is considered client side technology.

when should one use javaScript in the servlet and jsp context ?


Please guide.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

naveen yadav wrote:
Is that a Bad practice to use javaScript in jsp? Since javascript is considered client side technology.

Nope. javascript is tightly coupled with jsp in modern web application. you need to write js[javascript] file and then include it in jsp[better practice]

naveen yadav wrote:
when should one use javaScript in the servlet and jsp context ?

when ever there is a need :-)
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

for safer side duplicate the java script logic[in terms of validation] on server side also!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

naveen yadav wrote:Is that a Bad practice to use javaScript in jsp? Since javascript is considered client side technology.


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. On the client side, as you said. 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. However I wouldn't expect to see that sort of thing in a tutorial for beginners, which is what you are talking about.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15951
    
  19

It is far worse to put java code (scriptlets) in JSPs.

JavaScript is an unholy mess, but it can make pages more interactive because it programs the client instead of the server. As Seetharaman pointed out, though, 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!


Customer surveys are for companies who didn't pay proper attention to begin with.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60766
    
  65

Seetharaman Venkatasamy wrote:for safer side duplicate the java script logic[in terms of validation] on server side also!


Clarification: validation only (in case anyone gets confused about the bazillion other things that JavaScript can be used for on the client).


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60766
    
  65

Tim Holloway wrote:JavaScript is an unholy mess

I'll disagree. JavaScript is actually a really cool language that gets maligned because most people using it don't take the time to actually learn it. This seems to be especially true of people (like myself) who came to it from the Java world and expect it to be Java (just because it's called JavaScript -- one example of the poorest naming choice ever).

Now the DOM API, yeah, that's a mess.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60766
    
  65

But to answer the original question: not at all. Remember, 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.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15951
    
  19

Bear Bibeault wrote:
Tim Holloway wrote:JavaScript is an unholy mess

I'll disagree. JavaScript is actually a really cool language that gets maligned because most people using it don't take the time to actually learn it.



Well, actually, I was thinking more of the train wreck that ensues from nulls not being the same thing as "nothing", class comparisons that don't, and booleans that aren't Boolean (or vice versa).

But, as noted, that's not related to JSPs.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60766
    
  65

As this is the JSP forum, we'll not debate it here, but that's all Java baggage that's being carried over. Sure there are rules that seem weird to Java folk, but in the context of the language itself make perfect sense.

It's certainly not perfect -- far from it -- but it's a good tool for its purpose.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Bear Bibeault wrote:
Tim Holloway wrote:JavaScript is an unholy mess

I'll disagree.


To reinterpret: JavaScript is an unholy mess if you try to generate it with JSTL and EL. Trying to get the quotes and escaping right can be a nightmare.
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384


now i gets the idea....use javaScript with pride in jsp.

Thank you all guys for sharing the opinion.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15951
    
  19

Paul Clapham wrote:
Bear Bibeault wrote:
Tim Holloway wrote:JavaScript is an unholy mess

I'll disagree.


To reinterpret: JavaScript is an unholy mess if you try to generate it with JSTL and EL. Trying to get the quotes and escaping right can be a nightmare.


Actually, that was never even on my radar. A little CDATA can go a long ways.

I do all my hair-tearing on trying to pick up strings on a page and getting 6 different ways of "it's not there". This kind of stuff: http://saladwithsteve.com/2008/02/javascript-undefined-vs-null.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JavaScript in jsp : Bad Practice ?
 
Similar Threads
Converting ResultSet String to ArrayList
IllegalStateException: Cannot forward after response has been committed
using javascripts in servlet?
How to exit from a jsp page
Need help on Passing parameters