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 in jsp : Bad Practice ?

 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for safer side duplicate the java script logic[in terms of validation] on server side also!
 
Paul Clapham
Sheriff
Pie
Posts: 20161
23
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 17616
38
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 17616
38
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20161
23
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 384
Java MyEclipse IDE Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

Thank you all guys for sharing the opinion.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 17616
38
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic