Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

rogue JSP code!

 
Winston Smith
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HeLLo All,
Assume I have a session attribute "test" which is set to some value upon page load. Now in this page, wrapped in a javascript function, is the following:
<%= session.removeAttribute("test") %>
The problem is, even though this javascript function is not being called, this JSP code is executing! I understand that all the JSP is compiled on the server side, then sent to the client however, this specific code should not execute unless I call the javascript function, right??? Well, I guess that's not right, so my question is, why is it executing?
NOTE: I've tested this attribute on other pages so the attribute itself is being bound correctly, and I've also tested it in the page in question and find that it is null (because of some rogue JSP code!)
Any help is greatly appreciated, thanks --
WS
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65542
110
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this specific code should not execute unless I call the javascript function, right???

Wrong.
The JSP code not only is compiled on the server, it is executed on the server and the result is sent to the client as a plain-old HTML page. All the JSP code is run on the server.
The only utility in embedding Java scriptlets into a Javascript function is to cause pieces of the Javascript to be generated dynamically. But these scriptlets run on the server to generate text to be sent to the browser. Their processing is not deferred in any way.
hth,
bear
[ August 29, 2003: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65542
110
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, the very best way to understand what is going on, as well as to get a handle on what JSP is all about, is to look at the Java code for the servlet generated for your JSP page. Where this is written on disk depends upon your container (Tomcat, resin, etc).
By looking at this code, you can see exactly what will happen when your JSP is executed.
Take the following JSP fragment:

What you will see in the generated code is something like:

So you can see that the Java code to remove the attribute is executed on the server side as the page code is running to generate the response to be sent to the browser.
A 'view source' of the page will reveal:

This is a fundamental concept of JSP technology. Many people are initially confused into thinking that the Java in the JSP page can be executed after the page is loaded into the browser. Not so. The purpose of the Java in the page is to render the page with dynamic data to be sent to the browser as a normal, everyday HTML page.
hth,
bear
[ August 29, 2003: Message edited by: Bear Bibeault ]
 
Winston Smith
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear! I understand completely now, that JSP is a mean, html-generating machine that does all its work on the server side, and sends the client nothing more than a pretty HTML page. Thanks again,
WS
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65542
110
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly!
bear
 
That which doesn't kill us makes us stronger. I think a piece of pie wouldn't kill me. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!