• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP with JavaScript and Comments

 
Mark Wuest
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JSP page that has Javascript in it.

When I comment out a line of Javascript code that, in turn, contains a JSP expression, the compiler (Weblogic) still attempts to compile the expression.

e.g.:


I think I realize what is happening: the jsp compiler doesn't know javascript, so why should it pay attention to javascript comments? If this is so, you guys are gonna send me to the HTML/Javascript forum.

So, why am I bugging you guys?

Well, I'm relatively new to the Javascript land so I'm thinking that imbedding an expression in Javascript like that must be a no-no in somebody's Best Practices or Effective Javascript book. What do others do?

Thanks,

Mark
[ September 27, 2005: Message edited by: Mark Wuest ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct in that any Javascript in your page is seen by the JSP translator as just template text without any semantic meaning.

It's perfectly acceptable to embed JSP dynamic elements in Javascript as long as you realize that the JSP elements are evaluated on the server and do not become part of the client-side mechanisms.

For example:



is perfectly fine. The expression is evaluated on the server, and the resulting value is placed into the text buffer that will be sent to the client, resulting in something perhaps like:

 
Mark Wuest
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
You are correct in that any Javascript in your page is seen by the JSP translator as just template text without any semantic meaning.

I think I was surprised - you get used to commenting out sections (in this case, to see what happened if the expression returned a 1) and it doesn't just work like that with this.

I thought (to myself): "why not just comment out the Java in the expression element?" No cigar - that compiles to

which is, to the compiler

So, for now, a commented out like like this:

looks like this:

And I'm certainly open to any other techniques, since I marf around like this quite a bit.

Thanks!

Mark
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only way to comment out JSP dynamic elements is with the JSP comment mechansim: <%-- comment --%>
 
Mark Wuest
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
The only way to comment out JSP dynamic elements is with the JSP comment mechansim: <%-- comment --%>

Thanks. I have (ahem, I should say "had") been trying to make things as well-formed xml as possible. This still got compiled when inside an xml (<!-- -->) comment.

Thanks again,

Mark
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why on earth would you be writing JSPs in the XML format? That format is intended as an intermediary format for JSP cotnainers, and for machine-generated JSPs where serializing a DOM makes sense. It is not intended for "hand-written" JSPs and you're going to find yourself continually fighting notation to the point where your JSPs are incomprehensible gobbley-dee-gook.
 
Mark Wuest
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
Why on earth would you be writing JSPs in the XML format?

Well, I just have a suspicion that eventually some standard will say we're supposed to.

Mark
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Extremely doubtful. In fact, I'd venture to say 100% doubtful. Unless it's some riduculous internal "standard" imposed by some over-zealous, but clueless, CTO type. (Which happens -- look though this forum for posts along the lines of "my boss won't let us use beans" and "we are not allowed to use the session").
[ September 27, 2005: Message edited by: Bear Bibeault ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!