Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

jsp code can contain inside javascript ?  RSS feed

 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, wondering whether the javascript function can contain jsp code ..like example :



can we put jsp code inside javascript function ..? thank you !
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65828
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, yes. But it won't do what you expect.

Try it and see if you can figure out why.
 
kumari Jain
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes,you can have a scriptlet inside a script.
Hence the session attribute can be accessed in testing.jsp

regards,
kumari
 
Malhar Barai
Author
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why can't it be...

You can write any html codes in JSP.

MB
 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bear, as your word, it really didn't save value of radioselection2 , why is it so ..

for others, yes..we can contains scriptlet ..but the problem is it display null to me when i try printout the variable
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alvin chew:
bear, as your word, it really didn't save value of radioselection2 , why is it so ..

for others, yes..we can contains scriptlet ..but the problem is it display null to me when i try printout the variable


Alvin,

As i know scriplets get executed on page load. the scriplet in your javascript function has been already executed on page load.

you can have jsp expressions instead, like <%= whatever%>, in your javascript function.
 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh....my god ....adeel , thanks for remind ...
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by adeel ansari:


you can have jsp expressions instead, like <%= whatever%>, in your javascript function.


which will ALSO execute serverside before the page is ever sent to the client...

You can NOT simply put JSP code inside Javascript and expect it to be executed when the Javascript is executed.
You CAN create Javascript that calls a Servlet or JSP on the server and does something with the result, but that's another thing entirely.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeroen Wenting:

You can NOT simply put JSP code inside Javascript and expect it to be executed when the Javascript is executed.


As far as I remember it was working. i have tried it once.
anyways, are you sure Jeroen? if yes then Ok, else try it out.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65828
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a fundamental misunderstanding about how JSPs work.

JSP is a templating language to create plain old vanilla HTML pages to send to the browser. All the Java scriplets, JSP tags, custom tags, EL and so on that are part of JSP technology execute on the server to create an HTML page to send to the browser. The browser sees only the renderred HTML.

So Alvin, in your first example, all that JSP that is inside the JS function is executed on the server before the page even gets sent to the browser.

Do this: when the page gets sent to the browser, do a 'View Source' to see the HTML that the browser got. You'll note that all JSP is gone! And any output that was produced by JSP scriptlet expressions is hard-coded into the page.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by adeel ansari:


As far as I remember it was working. i have tried it once.
anyways, are you sure Jeroen? if yes then Ok, else try it out.


yes, I'm quite sure. I think we've all wished to do what you want at some point but it's simply impossible.

The closest you can get is having Javascript do an Http request to a JSP and capture the output of that request in an HTML element (or whereever).
This is quite possible and I employ it regularly to for example emulate tabbed dialogues in HTML (where tabs depend on data in other tabs for their content or even existence) or to generate dropdown lists dependent on the content of other formfields on the same page dynamically.
It's a bit cumbersome, requiring about a hundred lines of javascript code, but it works.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Wenting and Bear for your quite detailed and helpful explaination.
 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to everybody for replying !!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!