This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes JSP and the fly likes rogue JSP code! Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » JSP
Reply Bookmark "rogue JSP code!" Watch "rogue JSP code!" New topic
Author

rogue JSP code!

Winston Smith
Ranch Hand

Joined: Jun 06, 2003
Posts: 136
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


for (int i = today; i < endOfTime; i++) { code(); }
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56540
    
  14

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 ]

[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56540
    
  14

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

Joined: Jun 06, 2003
Posts: 136
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

Joined: Jan 10, 2002
Posts: 56540
    
  14

Exactly!
bear
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: rogue JSP code!
 
Similar Threads
JSP/Java/JavaScript Communication
Hide url in the titlebar of browser
JSP MySql
in jsp i want touse java script variable value
Testing javascript function that is not within an object